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. Use custom widget button in QMessageBox
QtWS25 Last Chance

Use custom widget button in QMessageBox

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 3.1k Views
  • 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.
  • John_38J Offline
    John_38J Offline
    John_38
    wrote on last edited by John_38
    #1

    Hello everybody,
    I now appeal to your acquaintances because I find it difficult to achieve something.
    I have a custom button. I would like this button to be the button that is in the QMessageBox.
    I would like to promote the QMessageBox buttons by my custom buttons.
    For my custom button, I have already create a class (for custom widget).

    Thank you for your help.

    Regards,
    John.

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      There's no widget promotion involved because you can't use QMessageBox in the designer, but you can do it from code.
      QMessageBox supports all widgets derived from QAbstractButton as custom buttons. For example:

      class MyFancyButton : public QPushButton {
      public:
          MyFancyButton(const QString& text, QWidget* parent = nullptr) : QPushButton(text, parent) {}
      };
      
      QMessageBox box(QMessageBox::Warning, "Hello", "I'm using a fancy button!", QMessageBox::NoButton, parent);
      box.addButton(new MyFancyButton("Foo"), QMessageBox::AcceptRole);
      box.addButton(new MyFancyButton("Bar"), QMessageBox::RejectRole);
      box.exec();
      
      1 Reply Last reply
      2
      • John_38J Offline
        John_38J Offline
        John_38
        wrote on last edited by
        #3

        Thank for you answer.
        I have 3 errors now..
        Screenshot: http://prntscr.com/dq3mip

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I have 3 errors now.

          Sorry, but have you bothered to read them? ;)

          The first one: parent is a wrong identifier in this scope. I just used 'parent' as a placeholder. You need to put an actual parent in there. In your case it might be this for example.
          The other two - I have a constructor in MyFancyButton example that takes a string literal as a parameter. Do you have such a constructor in your MsgButton class? Can you show your constructor's signature?

          1 Reply Last reply
          0
          • John_38J Offline
            John_38J Offline
            John_38
            wrote on last edited by John_38
            #5

            Yes.
            My class "MsgButton" is here.

            #ifndef MSGBUTTON_H
            #define MSGBUTTON_H
            
            
            #include <QPushButton>
            #include <QPropertyAnimation>
            #include <QMessageBox>
            
            
            class MsgButton : public QPushButton
            {
                   Q_OBJECT
                   Q_PROPERTY( QColor   BackgroundColor    READ backgroundColor  WRITE setBackgroundColor DESIGNABLE true )
            
            public:
                  MsgButton(const QString& text, QWidget* parent) : QPushButton(text, parent)
                  {
                       m_Animation = new QPropertyAnimation(this, "BackgroundColor", this);
                       m_Animation->setDuration( 275 );
                       m_Animation->setEasingCurve( QEasingCurve::InCurve );
                       m_BackgroundColor = Qt::transparent;
                       this->setBackgroundColor( QColor(55,55,58) );
                  }
            
                   QColor backgroundColor() const
                   {
                        return m_BackgroundColor;
                   }
            
                   void setBackgroundColor( const QColor & color )
                   {
                       if( m_BackgroundColor != color )
                       {
                           m_BackgroundColor = color;
                           this->setStyleSheet( QString("MsgButton { border: 1px solid; color: rgb(200,200,200); background-color: rgb(55,55,58); border-color: %1;  }").arg(color.name()) );
                        }
                   }
            
            protected:
            
                   virtual void enterEvent( QEvent * event )
                   {
                        QPushButton::enterEvent(event);
            
                        if( m_Animation->state() == QAbstractAnimation::Running )
                            m_Animation->stop();
            
                        m_Animation->setEndValue( QColor(44,122,205) );
                        m_Animation->start();
                   }
            
                   virtual void leaveEvent( QEvent* event )
                   {
                        QPushButton::leaveEvent(event);
            
                        QVariant currentColor = m_Animation->currentValue();
            
                        if( m_Animation->state() == QAbstractAnimation::Running )
                            m_Animation->stop();
            
                        m_Animation->setEndValue( QColor(55,55,58) );
                        m_Animation->start();
            
                   }
            
                   QPropertyAnimation*     m_Animation;
                   QColor                   m_BackgroundColor;
            };
            
            
            #endif // MSGBUTTON_H
            
            
            1 Reply Last reply
            0
            • Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The difference is you didn't make your second parameter optional, so there' s no constructor taking only string literal.

              The fix is to either give the parameter default value (that's usually the case with Qt's widgets):

              MsgButton(const QString& text, QWidget* parent = nullptr) : QPushButton(text, parent)
              

              or actually specify the parameter at call site:

              box.addButton(new MsgButton("Foo", nullptr), QMessageBox::AcceptRole);
              

              Btw. You should move your implementation into a .cpp file or you'll get a lot of linker errors when you include your header in more than one place.

              1 Reply Last reply
              0
              • John_38J Offline
                John_38J Offline
                John_38
                wrote on last edited by
                #7

                Oh ok thanks.
                Now i have actually 1 error (2 but is the same)..
                Screenshot : http://prntscr.com/dq4rwf

                1 Reply Last reply
                0
                • Chris KawaC Online
                  Chris KawaC Online
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  nullptr is a C++11 keyword. To use it add CONFIG += c++11 in the .pro file and click Build->Run qmake. If you don't want to use C++11 (why not?) just change nullptr to Q_NULLPTR, or, as a last resort, to 0.

                  1 Reply Last reply
                  1
                  • John_38J Offline
                    John_38J Offline
                    John_38
                    wrote on last edited by
                    #9

                    T H A N K.
                    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