PDA

View Full Version : How to addSubWindow to a QMdiArea without taking the focus?



dv8
29th January 2013, 15:26
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.

ChrisW67
29th January 2013, 21:57
Did you try...


QMdiSubWindow *previous = ui->mdiArea->activeSubWindow();
MyWidget *widget = new MyWidget(this);
QMdiSubWindow *sub = ui->mdiArea->addSubWindow(widget);
ui->mdiArea->setActiveSubWindow(previous);

dv8
30th January 2013, 06:05
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
sub->show(); after the
ui->mdiArea->setActiveSubWindow(previous);

So here is how it works for me now:


QMdiSubWindow *previous = ui->mdiArea->activeSubWindow();
QTextEdit *edit=new QTextEdit;
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? :)