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. Subclassing QMessageBox
Forum Updated to NodeBB v4.3 + New Features

Subclassing QMessageBox

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 531 Views 3 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.
  • posktomtenP Offline
    posktomtenP Offline
    posktomten
    wrote on last edited by
    #1

    Hi! I would like some advice and opinions.
    To make QMessageBox look the way I want, I have subclassed it.
    Using QShowEvent to change its size.
    It works, if you add a time margin with QTimer. It feels a bit unreliable. I have checked many times now, and it has always worked. Is it better to use QLayout?

    class MyMsgBox : public QMessageBox
    {
        Q_OBJECT
    public:
        explicit MyMsgBox(QWidget *parent = nullptr, const QSize &size = QSize(500, 500))
            : QMessageBox(parent), m_size(size)
        {
            // qDebug() << "MyMsgBox created with size:" << m_size;
        }
    protected:
        void showEvent(QShowEvent *event) override
        {
            QTimer::singleShot(100, this, [this]() {
                this->setFixedSize(m_size);
            });
        }
    
    private:
        QSize m_size;
    };
    

    subclasing_qmessagebox.png

    Thanks!

    posktomten

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Not near a dev environment to test myself...

      How does calling MyMsgBox::setFixedSize() or MyMsgBox::resize() before MyMsgBox::show() or exec() fail?

      1 Reply Last reply
      0
      • posktomtenP Offline
        posktomtenP Offline
        posktomten
        wrote on last edited by
        #3

        Nothing happens. QMessageBox doesn't have the properties to "resize" or "setFixedSize", as far as I understand.

        void messageBox(const QString &message, QString &languagefile, const QSize &size, QMessageBox::Icon icon)
        {
            MyMsgBox *msgBox = new MyMsgBox(nullptr, size);
            msgBox->setAttribute(Qt::WA_DeleteOnClose);
            msgBox->setIcon(icon);
            msgBox->setWindowTitle(DISPLAY_NAME " " VERSION);
            msgBox->setText(message);
            msgBox->setInformativeText(languagefile);
            QPushButton  *okButton = new QPushButton("Ok", msgBox);
            msgBox->addButton(okButton, QMessageBox::YesRole);
          //  msgBox->setFixedSize(size);
            msgBox->exec();
        }
        
        

        But it works great with zero lag!

        QTimer::singleShot(0, this, [this]() {
                    this->setFixedSize(m_size);
                });
        

        posktomten

        JonBJ 1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          QMessageBox is a QWidget and inherits both QWidget::resize() and QWidget::setFixedSize().

          posktomtenP 1 Reply Last reply
          0
          • C ChrisW67

            QMessageBox is a QWidget and inherits both QWidget::resize() and QWidget::setFixedSize().

            posktomtenP Offline
            posktomtenP Offline
            posktomten
            wrote on last edited by
            #5

            @ChrisW67 said in Subclassing QMessageBox:

            is a QWidget and inherits both QWidget::resize() and QWidget::setFixedSize().

            Yes that's true. but I've never managed to change its size. "resize" and "setFixedSize" have never worked.
            It's possible to influence indirectly by sizing the buttons.

                    QPushButton  *yesButton = new QPushButton(iconYes, tr("Restart Now"), msgBox);
                    yesButton->setFixedSize(QSize(150, 40));
            

            posktomten

            Pl45m4P 1 Reply Last reply
            0
            • posktomtenP posktomten

              @ChrisW67 said in Subclassing QMessageBox:

              is a QWidget and inherits both QWidget::resize() and QWidget::setFixedSize().

              Yes that's true. but I've never managed to change its size. "resize" and "setFixedSize" have never worked.
              It's possible to influence indirectly by sizing the buttons.

                      QPushButton  *yesButton = new QPushButton(iconYes, tr("Restart Now"), msgBox);
                      yesButton->setFixedSize(QSize(150, 40));
              
              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @posktomten

              Hi, I think this topic helps.
              Apparently QMessageBox has an internal QGridLayout that is configured to always be as compact as possible (makes sense for usual use cases of msg boxes).
              So subclassing QMessageBox directly only has little space for configurations.
              Since QMessageBox is also some kind of QDialog (through inheritance), subclassing QDialog might be better for your custom message dialog class.
              Then you have full control over size, layouts and the content.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              posktomtenP 1 Reply Last reply
              1
              • posktomtenP posktomten

                Nothing happens. QMessageBox doesn't have the properties to "resize" or "setFixedSize", as far as I understand.

                void messageBox(const QString &message, QString &languagefile, const QSize &size, QMessageBox::Icon icon)
                {
                    MyMsgBox *msgBox = new MyMsgBox(nullptr, size);
                    msgBox->setAttribute(Qt::WA_DeleteOnClose);
                    msgBox->setIcon(icon);
                    msgBox->setWindowTitle(DISPLAY_NAME " " VERSION);
                    msgBox->setText(message);
                    msgBox->setInformativeText(languagefile);
                    QPushButton  *okButton = new QPushButton("Ok", msgBox);
                    msgBox->addButton(okButton, QMessageBox::YesRole);
                  //  msgBox->setFixedSize(size);
                    msgBox->exec();
                }
                
                

                But it works great with zero lag!

                QTimer::singleShot(0, this, [this]() {
                            this->setFixedSize(m_size);
                        });
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @posktomten said in Subclassing QMessageBox:

                But it works great with zero lag!

                Then the implication is that internal QMessageBox code does some size changing of its own, perhaps as it is first shown. So if you want a solution what is wrong with your QTimer::singleShot() (this is often required in certain Qt cases, you won't be the first one to use this), or maybe same in override of showEvent() (after calling base method first) would also work and be a bit neater?

                Otherwise you might want to roll your own QDialog as @Pl45m4 writes.

                1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @posktomten

                  Hi, I think this topic helps.
                  Apparently QMessageBox has an internal QGridLayout that is configured to always be as compact as possible (makes sense for usual use cases of msg boxes).
                  So subclassing QMessageBox directly only has little space for configurations.
                  Since QMessageBox is also some kind of QDialog (through inheritance), subclassing QDialog might be better for your custom message dialog class.
                  Then you have full control over size, layouts and the content.

                  posktomtenP Offline
                  posktomtenP Offline
                  posktomten
                  wrote on last edited by
                  #8

                  @Pl45m4 said in Subclassing QMessageBox:

                  Since QMessageBox is also some kind of QDialog (through inheritance), subclassing QDialog might be better for your custom message dialog class.
                  Then you have full control over size, layouts and the content.

                  Is definitely a better solution. I was a little surprised that it was possible to use QEvent.

                  posktomten

                  1 Reply Last reply
                  0
                  • posktomtenP Offline
                    posktomtenP Offline
                    posktomten
                    wrote on last edited by posktomten
                    #9

                    Thanks for all the feedback!
                    My solution doesn't feel right. Even though it seems to work.
                    Making your own messagebox from QDialog feels much better.
                    The reason for me to make my own messagebox is that I often want more width.

                    And is probably a more stable solution that should work more securely with upcoming Qt versions.

                    posktomten

                    S 1 Reply Last reply
                    0
                    • posktomtenP posktomten

                      Thanks for all the feedback!
                      My solution doesn't feel right. Even though it seems to work.
                      Making your own messagebox from QDialog feels much better.
                      The reason for me to make my own messagebox is that I often want more width.

                      And is probably a more stable solution that should work more securely with upcoming Qt versions.

                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #10

                      @posktomten said in Subclassing QMessageBox:

                      The reason for me to make my own messagebox is that I often want more width.

                      I feel you. Sometimes we have error messages that include the full path to a file. It doesn't fit inside the message boxes...

                      1 Reply Last reply
                      0
                      • posktomtenP posktomten has marked this topic as solved on

                      • Login

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