How to addSubWindow to a QMdiArea without taking the focus?
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:
Code:
MyWidget *widget=new MyWidget();
QMdiSubWindow *sub=ui->mdiArea->addSubWindow(widget,Qt::SubWindow);
sub->setWindowState(Qt::WindowNoState);
but it does not have the desired effect.
Re: How to addSubWindow to a QMdiArea without taking the focus?
Did you try...
Code:
QMdiSubWindow *previous = ui->mdiArea->activeSubWindow();
MyWidget *widget = new MyWidget(this);
QMdiSubWindow *sub = ui->mdiArea->addSubWindow(widget);
ui->mdiArea->setActiveSubWindow(previous);
Re: How to addSubWindow to a QMdiArea without taking the focus?
Thank you ChrisW67. I did try it and it did not work for me at that time. But now that you suggested it again I thought this should work. So I decided to create a test application and try it again. Needless to say, I found why it was not working before... :)
The reason was that I called after the
Code:
ui->mdiArea->setActiveSubWindow(previous);
So here is how it works for me now:
Code:
QMdiSubWindow *previous = ui->mdiArea->activeSubWindow();
QMdiSubWindow *sub=ui->mdiArea->addSubWindow(edit,Qt::Window);
sub
->widget
()->setWindowTitle
(QString::number(count
++));
//the "count++" is to set a different title for each new tab, so I can see if it is active or not.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);
Thanks again!
EDIT: It seams I can not edit the title of my thread, so can someone of the moderators add "(SOLVED)" to the title please? :)