Positioning QMessageBox



  • Is there a way to make sure QMessageBox is displayed on top of and within the confines of the main window?

    Bart



  • @bart.hollis
    Can't you make it a modal dialog?



  • @bart.hollis that should be the case, if you give your QMessageBox the "Main Window" as parent.



  • Would you please explain how I do that? I can't seem to get the parameters correct.



  • @bart.hollis
    Too few information to give a concrete example. But something like this:

    void MainWindow::showMessageBox() {
        QMessageBox *msg = new QMessageBox::about(this, "MsgBoxTitel", "Display Text");
        msg->exec();
    }
    


  • This is the code I'm using. It is contained in the MainWindow.cpp file. I've tried all I can think of for arguments to the setParent() . What's there is the end of a long list of things I have tried. Would certainly appreciate being set straight!

    void MainWindow::on_tabWidget_tabBarClicked(int index)
    {
        static bool firstTime = true;
        if (firstTime) {
            firstTime = false;
            // If no data in files, ask to create sample.
            if (isEmpty())
            {
                QMessageBox msgBox;
                msgBox.setIcon(QMessageBox::Information);
                //msgBox.setParent(QWidget Ui->QMainWindow);
                msgBox.setGeometry(450, 450, 250, 200);
                msgBox.setText("Notice! \n There's nothing in the tables!");
                msgBox.setInformativeText("Wanna add some sample stuff?");
                msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                msgBox.setDefaultButton(QMessageBox::No);
                msgBox.setEscapeButton(QMessageBox::No);
                if (msgBox.exec() == QMessageBox::Yes)
                    addSampleData();
            }
        }
    


  • @bart.hollis
    I don't know of an easy way to confine the MessageBox inside the parent Window, you would have to subclass it or make your totaly own MessageBox.

    But here's the closest adjusment of a standard QMessageBox I could come up with:

    QMessageBox *msgBox = new QMessageBox(this);
        msgBox->setIcon(QMessageBox::Information);
        msgBox->resize(250, 200);
        msgBox->setText("Notice! \n There's nothing in the tables!");
        msgBox->setInformativeText("Wanna add some sample stuff?");
        msgBox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msgBox->setDefaultButton(QMessageBox::No);
        msgBox->setEscapeButton(QMessageBox::No);
        msgBox->setWindowFlag(Qt::FramelessWindowHint,true);
        msgBox->setStyleSheet("QMessageBox{border: 1px solid black; background-color:white}");
    
        if (msgBox->exec() == QMessageBox::Yes)
                    addSampleData();
    


  • @bart.hollis
    When you say "QMessageBox is displayed on top of and within the confines of the main window" do you mean,

    1. it should always appear above and remain on top of the parent window until the user dismisses it. Further, it should not go behind if the use happens to click on the parent window?
    2. the use should not be able to drag it out of "the confines" of the parent window?

    (1.) should happen if you give it the parent widgets pointer in the constructor. Message boxes are supposed to be modal by default.
    (2.) can be done if as @J-Hilk suggests you subclass the message box and override the mouse events. Do you really want go that far?

    Also why bother using pointer and new for the message box? It looks like having one on the stack is all you need.
    I find using resize() and move() is usually better that setGeometry() for positioning windows and dialogs :-).



  • @kenchan said in Positioning QMessageBox:

    @bart.hollis
    When you say "QMessageBox is displayed on top of and within the confines of the main window" do you mean,

    1. it should always appear above and remain on top of the parent window until the user dismisses it. Further, it should not go behind if the use happens to click on the parent window?
    2. the use should not be able to drag it out of "the confines" of the parent window?

    (1.) should happen if you give it the parent widgets pointer in the constructor. Message boxes are supposed to be modal by default.
    (2.) can be done if as @J-Hilk suggests you subclass the message box and override the mouse events. Do you really want go that far?

    Also why bother using pointer and new for the message box? It looks like having one on the stack is all you need.
    I find using resize() and move() is usually better that setGeometry() for positioning windows and dialogs :-).

    J. Hilk, Thank you for your reply. Yes to your point 1. It should always appear above and remain on top of the parent window. Don't really care if the user drags it out of the location, just want it to appear there. Your remark about giving it the parent wigets pointer is what I understand I should do. I just don't know how! I know this is a simple C++ problem, and I've studied and tried everything I can see, but just can't seem to figure this out. I guess my question should really be, How, exactly, do I give the pointer to the parent wiget? What is an example?



  • I Found it! The pointer it was looking for is "this"! "this" is the pointer to the parent widget. Thanks for all who helped with this simple, but frustrating problem. I hate being a noob! :)
    Bart



  • @bart.hollis Glad to hear you found it :-).
    Yes if the function from which you create the object is itself a member function of an object the pointer to itself is called "this" in c++.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.