Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Message box doesnt appear
Qt 6.11 is out! See what's new in the release blog

Message box doesnt appear

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xatziaris
    wrote on last edited by xatziaris
    #1

    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 :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xatziaris
          wrote on last edited by
          #4

          thanks a lot guys :)

          Any similar method for widgets?

          mrjjM 1 Reply Last reply
          0
          • X xatziaris

            thanks a lot guys :)

            Any similar method for widgets?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @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.

            1 Reply Last reply
            1
            • X Offline
              X Offline
              xatziaris
              wrote on last edited by
              #6

              thanks a lot man :) happy new year

              mrjjM 1 Reply Last reply
              0
              • X xatziaris

                thanks a lot man :) happy new year

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @xatziaris
                happy new year :)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved