Results 1 to 3 of 3

Thread: How to addSubWindow to a QMdiArea without taking the focus?

  1. #1
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    4

    Default 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:
    Qt Code:
    1. MyWidget *widget=new MyWidget();
    2. QMdiSubWindow *sub=ui->mdiArea->addSubWindow(widget,Qt::SubWindow);
    3. sub->setWindowState(Qt::WindowNoState);
    To copy to clipboard, switch view to plain text mode 
    but it does not have the desired effect.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to addSubWindow to a QMdiArea without taking the focus?

    Did you try...
    Qt Code:
    1. QMdiSubWindow *previous = ui->mdiArea->activeSubWindow();
    2. MyWidget *widget = new MyWidget(this);
    3. QMdiSubWindow *sub = ui->mdiArea->addSubWindow(widget);
    4. ui->mdiArea->setActiveSubWindow(previous);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    dv8 (30th January 2013)

  4. #3
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    4

    Default 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
    Qt Code:
    1. sub->show();
    To copy to clipboard, switch view to plain text mode 
    after the
    Qt Code:
    1. ui->mdiArea->setActiveSubWindow(previous);
    To copy to clipboard, switch view to plain text mode 

    So here is how it works for me now:
    Qt Code:
    1. QMdiSubWindow *previous = ui->mdiArea->activeSubWindow();
    2. QTextEdit *edit=new QTextEdit;
    3. QMdiSubWindow *sub=ui->mdiArea->addSubWindow(edit,Qt::Window);
    4. 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.
    5. sub->show();
    6. if (previous==0)
    7. ui->mdiArea->setActiveSubWindow(sub);
    8. 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.
    9. 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?
    Last edited by dv8; 30th January 2013 at 06:09. Reason: updated contents

Similar Threads

  1. Replies: 2
    Last Post: 17th October 2011, 09:58
  2. QSplitter taking parenthood over its widgets
    By mak_user in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2011, 21:40
  3. Replies: 2
    Last Post: 13th May 2010, 17:46
  4. qextserialport taking value 0 as null ?
    By dheeraj in forum Qt Programming
    Replies: 4
    Last Post: 11th August 2008, 10:55
  5. Replies: 1
    Last Post: 16th April 2007, 11:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.