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
  •  | 
  •  

Pregunta

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.

¿Fue útil?

Solución

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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top