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();
sub->show();
To copy to clipboard, switch view to plain text mode
after the
ui->mdiArea->setActiveSubWindow(previous);
ui->mdiArea->setActiveSubWindow(previous);
To copy to clipboard, switch view to plain text mode
So here is how it works for me now:
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);
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);
To copy to clipboard, switch view to plain text mode
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?
Bookmarks