Message box doesnt appear



  • this is my code:

    main.cpp

    #include <QApplication>
    #include <QWidget>
    #include "mybutton.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QWidget window1;
        window1.setFixedSize(640,480);
    
        QIcon openicon("/home/xatziaris/workspace/Qt/project8452/project8452/open.svg");
        QIcon saveicon("/home/xatziaris/workspace/Qt/project8452/project8452/save.png");
        QIcon closeicon("/home/xatziaris/workspace/Qt/project8452/project8452/close.png");
        QIcon abouticon("/home/xatziaris/workspace/Qt/project8452/project8452/about.png");
    
        mybutton openbutton;
        openbutton.setParent(&window1);
        openbutton.setText("Open");
        openbutton.setIcon(openicon);
        openbutton.setGeometry(1,1,80,45);
    
        mybutton savebutton;
        savebutton.setParent(&window1);
        savebutton.setText("Save");
        savebutton.setIcon(saveicon);
        savebutton.setGeometry(1,47,80,45);
    
        mybutton aboutbutton;
        aboutbutton.setParent(&window1);
        aboutbutton.parentwindow = &window1;
        aboutbutton.setText("About");
        aboutbutton.setIcon(abouticon);
        aboutbutton.setGeometry(1,93,80,45);
    
        mybutton closebutton;
        closebutton.setParent(&window1);
        closebutton.setText("Close");
        closebutton.setIcon(closeicon);
        closebutton.setGeometry(1,434,80,45);
    
    
        window1.show();
    
        QObject::connect(&closebutton,SIGNAL(clicked()),&a,SLOT(quit()));
    
        aboutbutton.setCheckable(true);
        QObject::connect(&aboutbutton,SIGNAL(clicked(bool)),&aboutbutton,SLOT(showinfo(bool)));
    
        return a.exec();
    }
    
    

    mybutton.cpp

    #include "mybutton.h"
    
    void mybutton::showinfo(bool a){
    
        if(a){
    
            this->setChecked(false);
    
            QMessageBox infobox;
            infobox.setText("\nA text editor by ME.\n\nCreated with open source Qt Creator.");
            infobox.show();
    
        }
    
    }
    
    

    mybutton.h

    #ifndef MYBUTTON
    #define MYBUTTON
    
    #include <QPushButton>
    #include <QWidget>
    #include <QMessageBox>
    
    class mybutton: public QPushButton{
        Q_OBJECT
    
    public:
        QWidget *parentwindow;
    
    public slots:
        void showinfo(bool a);
    };
    
    #endif // MYBUTTON
    
    
    

    in main.cpp a create a window with 4 buttons. i want to click the "About" button to make a message box appear. but nothing happens.

    the connection:

    QObject::connect(&aboutbutton,SIGNAL(clicked(bool)),&aboutbutton,SLOT(showinfo(bool)));
    

    the slot:

    void mybutton::showinfo(bool a){
    
        if(a){
    
            this->setChecked(false);
    
            QMessageBox infobox;
            infobox.setText("\nA text editor by ME.\n\nCreated with open source Qt Creator.");
            infobox.show();
    
        }
    
    }
    

    ty :)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    infobox is a local variable and currently will be destroyed at the end of showinfo if's true condition.


  • Qt Champions 2016

    Hi and welcome
    Adding to @SGaist info,
    You are using show() to show the dialog.
    Show() will not block and the next lines will be executed;
    So
    dialog.show();
    int a=10; << this is also executed while dialog is showing
    // and if this is end of function, the dialog is deleted.

    This is in contrast to
    exec()
    http://doc.qt.io/qt-5.5/qmessagebox.html#exec

    dialog.exec(); << exec blocks and it stays "in" there.
    int a=10; << this is NOT executed before dialog closes



  • thanks a lot guys :)

    Any similar method for widgets?


  • Qt Champions 2016

    @xatziaris
    Hi a QDialog is a widget. (also)
    But the exec is in Dialog class so other Widgets don't have it.
    You can how ever make a Widget "Dialog like"
    QWidget *yourWidget = new QWidget(this, Qt::Popup | Qt:: Dialog);
    yourWidget ->setWindowModality(Qt::WindowModal);
    yourWidget->show();

    But it can only use show(); not exec().

    If you need a dialog, just use a dialog.
    Nothing u cant do with Dialog that u can do with plain Widget.

    If time permits, you should really read the
    modal vs non modal (block vs non block)
    http://doc.qt.io/qt-5.5/qdialog.html
    as the use of exec() can give surprise if used inside
    event handlers and the like.

    The exec() is a message loop.
    So when you are in such loop,
    then normal mainwindow message loop is affected.



  • thanks a lot man :) happy new year


  • Qt Champions 2016

    @xatziaris
    happy new year :)


Log in to reply
 

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