Use custom widget button in QMessageBox



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


  • Moderators

    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();
    


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


  • Moderators

    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?



  • 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
    
    

  • Moderators

    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.



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


  • Moderators

    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.



  • T H A N K.
    Happy new year! :-)


Log in to reply
 

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