How to addSubWindow to a QMdiArea (set to TAB mode) without taking the focus from the currently active Tab?

StackOverflow https://stackoverflow.com/questions/14564849

  •  05-03-2022
  •  | 
  •  

문제

I have a function that runs in the background and adds tabs in a QMdiArea. However, when a new tab is added it steals the focus from the currently active tab. Is there a way I can add a new inactive tab (sub window), so I keep the focus to the currently used tab?

I have searched the web but was not able to find anything related to this issue.

I have tried the following:

MyWidget *widget=new MyWidget();
QMdiSubWindow *sub=ui->mdiArea->addSubWindow(widget,Qt::SubWindow);
sub->setWindowState(Qt::WindowNoState);

but it does not have the desired effect.

도움이 되었습니까?

해결책

I got help in another forum and found the solution to that problem:

QMdiSubWindow *previous = ui->mdiArea->activeSubWindow();
QTextEdit *edit=new QTextEdit;
QMdiSubWindow *sub=ui->mdiArea->addSubWindow(edit,Qt::Window)
sub->show();
if (previous==0)
   ui->mdiArea->setActiveSubWindow(sub);
else if (previous->isWidgetType()) //I check if previous is widget, because if you close the previous tab, when the new one is opened the program crashes on the next line because previous no longer exists.
   ui->mdiArea->setActiveSubWindow(previous);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top