Results 1 to 7 of 7

Thread: QMessageBox that blocks only the current QMdiSubWindow?

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

    Question QMessageBox that blocks only the current QMdiSubWindow?

    Here is another question that I was not able to answer myself or find a solution in the net...
    I'm working with QMdiArea and I have multiple tabs opened (web browser like application). I'm trying to display a message box in a single tab, in a way that it does not block the whole application, but only blocks the parent widget (the current tab/QMdiSubWindow). This message box should not block the other tabs and the user should be able to switch to another tab, even if the message box in the current tab is still opened.
    What I have tried so far is:
    Qt Code:
    1. QMessageBox *box=new QMessageBox(qobject_cast<QWidget *>(parent())); //The cast here produces a pointer to my custom widget, which is contained in the tab/QMdiSubWindow.
    2. box->setAttribute(Qt::WA_DeleteOnClose);
    3. box->setStandardButtons(QMessageBox::Ok);
    4. box->setWindowModality(Qt::WindowModal); //setting modality to Qt::NonModal does not lock the current tab, so it is not a solution
    5. box->setWindowTitle("Message:");
    6. box->setText("My test message.");
    7. connect(box,SIGNAL(destroyed()),this,SLOT(Continue()));
    8. box->show();
    To copy to clipboard, switch view to plain text mode 

    The description of the Qt::WindowModal constant says:
    The window is modal to a single window hierarchy and blocks input to its parent window, all grandparent windows, and all siblings of its parent and grandparent windows.
    and if I understand it correctly, "...parent window, all grandparent windows, and all siblings of its parent and grandparent windows", basically means the whole application. And that's what happens in reality - it blocks the whole application, just like Qt::ApplicationModal.
    I'm tackling this for a few hours now and I feel frustrated. Can someone please give any other ideas how I can achieve my goal???

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QMessageBox that blocks only the current QMdiSubWindow?

    basically means the whole application. And that's what happens in reality - it blocks the whole application, just like Qt::ApplicationModal.
    An Application can also be multi window application, (e.g. 2 QMainWindows), in such case only the window in which the QMessageBox is will be blocked, and the other window can still be operated on.

    You cannot use standard QMessageBox to achieve what you wanted.

    A standard QMessageBox may not be a good solution, as it is a top level window (independent Dialog), you will need somthing which will not be shown when other tabs are selected. For that it will be better solution to show a custom message-widget over the tab page widget (not adding it in layout), and paint it over the rest of the siblings, and obviously manually setting the message-widget position
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    Default Re: QMessageBox that blocks only the current QMdiSubWindow?

    Thanks Santosh, for the insights!
    I'm currently trying to implement your idea and it is working nicely, but there is one issue I can't get passed.
    I have made the following simple class:
    Qt Code:
    1. namespace Ui {
    2. class MyMessageBox;
    3. }
    4.  
    5. class MyMessageBox : public QWidget
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. explicit MyMessageBox(QWidget *parent = 0, QString Title="Message box", QString Content="");
    11. ~MyMessageBox();
    12.  
    13. private:
    14. Ui::MyMessageBox *ui;
    15. };
    To copy to clipboard, switch view to plain text mode 
    which visually resembles the original message box. It contains an Icon, a label to display the message in and an OK button.

    What I'm doing to visualize it is:
    Qt Code:
    1. MyMessageBox *msg=new MyMessageBox(*MyCustomTabWidget,Title,Message);
    2. connect(msg,SIGNAL(destroyed()),this,SLOT(Continue()));
    3. msg->setAttribute(Qt::WA_DeleteOnClose);
    4. msg->setWindowFlags(Qt::SubWindow); //I've found that Qt::SubWindow flag is giving me the desired behavior
    5. msg->show();
    6. msg->raise();
    To copy to clipboard, switch view to plain text mode 
    This message box is displayed over a QWebView which takes almost all the MyCustomTabWidget space. However, when it is displaying only the Icon, the Message text and the OK button are visible over the QWebView widget. The body and the title bar are not visible. I've made countless tests with different window flags, but nothing seams to help. Any suggestions, why this is happening and how I can fix it?

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

    Default Re: QMessageBox that blocks only the current QMdiSubWindow?

    OK, I've moved the attributes and flags lines in the constructor, so here is what I have now:
    Qt Code:
    1. #include "mymessagebox.h"
    2. #include "ui_mymessagebox.h"
    3. MyMessageBox::MyMessageBox(QWidget *parent,QString Title,QString Content) :
    4. QWidget(parent),
    5. ui(new Ui::MyMessageBox)
    6. {
    7. ui->setupUi(this);
    8. this->setAttribute(Qt::WA_DeleteOnClose);
    9. this->setWindowFlags(Qt::SubWindow | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowStaysOnTopHint);
    10. this->setWindowTitle(Title);
    11. ui->labelMessage->setText(Content);
    12. this->show();
    13. this->move(50,50);
    14. }
    15. MyMessageBox::~MyMessageBox()
    16. {
    17. delete ui;
    18. }
    To copy to clipboard, switch view to plain text mode 

    I'm creating the widget like this:
    Qt Code:
    1. MyMessageBox *msg=new MyMessageBox(*MyCustomTabWidget,"Title","Message");
    To copy to clipboard, switch view to plain text mode 
    And here is what I see:
    msg.png
    I added a QFframe container with border (for testing purposes) and that border is seen on the picture. Without the QFrame no border is displayed and only the Icon, the Text and the OK button are visible.
    Please, anyone, I need some hints about what is causing this issue and any suggestions how can I fix it.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QMessageBox that blocks only the current QMdiSubWindow?

    I don't think you can get title bar, and window frame. I think you may need to draw them manually, but wait I not 100% sure of this, so wait and see someone else has anything for you.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    dv8 (1st February 2013)

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMessageBox that blocks only the current QMdiSubWindow?

    One can use the same code QMdiSubWindow uses to render its frame.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    dv8 (1st February 2013)

  9. #7
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    4

    Default Re: QMessageBox that blocks only the current QMdiSubWindow?

    This thought crossed my mind too, but I didn't want to over-complicate it, so I came up with a workaround. I added the message frame in my "MyCustomTabWidget" above the QWebView (that is visible on the screenshot) and hid it. Then, whenever I need to display a message, I set the text and un-hide the QFrame. This brings the QWebView down and my message widget is displayed at the top, which works perfectly for me. When the OK button is pressed, the message frame is set to hidden again and everything comes into place.
    Here is how it looks now:
    Attachment 8666
    I used a label with lightblue background color to make a kind of title for my message box. It still needs a little visual refinement, but the important thing is that it does what I want.
    Thank you both for your assistance.

Similar Threads

  1. dialog blocks other window
    By franknapoli in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2009, 20:15
  2. get QMdiSubWindow Pointer of Current SubWindow
    By pospiech in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2009, 05:40
  3. drag_drop blocks drag_select, please help
    By zl2k in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2008, 23:28
  4. Code Blocks
    By upton15 in forum Newbie
    Replies: 1
    Last Post: 17th December 2007, 09:13
  5. QThread blocks gui
    By skoegl in forum Newbie
    Replies: 6
    Last Post: 29th October 2007, 13:09

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.