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. Getting the right click signal from a custom button
Forum Updated to NodeBB v4.3 + New Features

Getting the right click signal from a custom button

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 1.4k Views 1 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.
  • D Offline
    D Offline
    doubitchou
    wrote on last edited by doubitchou
    #1

    hi, the following code creates a Qbuttongroup and an event when left clicking on each button.
    There should be different events for right clicks I want to to catch with an event where to put some code as C++ (not stylesheet), and while I use the same sender than for the left click, the event never rise why ?

    GuiMatrix::GuiMatrix(uint Rows, uint Cols, quint32 Led_colors, QWidget * parentWidget, QVector< QPair<QString, QRgb> >& ColorsList )
        : MatxRows(Rows),
          MatxCols(Cols),
          GUIMtx_BtnColorsArray(MatxRows * MatxCols, BTNCOLOR_GREY),
          Palette()
    {
        setParent(parentWidget)  ;
    
        QVBoxLayout *layout     = new QVBoxLayout()                             ;
        QHBoxLayout *Hlayout    = new QHBoxLayout()                                         ;
        Glayout    = new QGridLayout()                                         ;
        Glayout->setSpacing(0)                                                              ;
        Hlayout->addStretch(1)                                                              ;
        Hlayout->addLayout(Glayout)                                                         ;
        Hlayout->addStretch(1)                                                              ;
        layout->addLayout(Hlayout)                                                          ;
        layout->addStretch(1)                                                               ;
    
        Populate(Glayout,Rows,Cols);
        if ( ColorsList.size() ==0 )
        {
            for (uint ind; ind <Led_colors; ind++)
                Palette[ind]= ind ;
        }
        else
        {
             for (auto iter: ColorsList )
             {
                Palette<< iter.second ;
              }
        }
    
        parentWidget->setLayout(layout)                                                     ;
        connect(this , SIGNAL(buttonClicked(QAbstractButton*)),this,SLOT(buttonClick(QAbstractButton*)));
    
        // This is not working 
    connect(this , SIGNAL(rightClicked()),this,SLOT(rightClicked()));
    }
    
    
    void GuiMatrix::Populate(QGridLayout *layout, const int rows, const int cols)
    {
        for ( int i = 0; i < rows; ++ i)
        {
            for ( int j = 0; j < cols; ++j)
            {
                MatrixButton *btn= new MatrixButton();
                btn->setFixedWidth(20);
                btn->setStyleSheet("QPushButton{background-color:#A0A0A0;}"
                                   "QPushButton[_rightClicked = true]{background-color:#A0A0A0;}")      ;
    
                layout->addWidget(btn,i,j)                                ;
    
                this->addButton(btn)                              ;
            }
        }
    }
    
    void GuiMatrix::rightClicked()
    {
        qDebug() <<"right click" ;
    }
    
    
    void GuiMatrix::buttonClick(QAbstractButton* button)
    {
        int BtnID= abs(this->id(button)) -2 ;
    
        QRgb BtnColorValue = GUIMtx_BtnColorsArray[BtnID];
    
        int indexPalette = Palette.indexOf(BtnColorValue) ;
    
        if (BtnColorValue == BTNCOLOR_GREY)                                // The 1st time the let button is grey (off)
            GUIMtx_BtnColorsArray[BtnID]= Palette.first() ;
        else if (indexPalette != -1 )                                      // we have found the color in the list
        {
            if ( Palette[indexPalette] == Palette.last() )
                GUIMtx_BtnColorsArray[BtnID]= Palette.first() ;
            else
                GUIMtx_BtnColorsArray[BtnID]= Palette[++indexPalette] ;
        }
        else
            qDebug() << "buttonClick problem, the color is not in the Palette list: " <<BtnColorValue ; // if not: there is a problem
    
    
        BtnColorValue = GUIMtx_BtnColorsArray[BtnID] ;
        QString ColorString = "QPushButton{background-color: " +   QString("#%1").arg(BtnColorValue, 6, 16, QLatin1Char( '0' ))  + ";} "
                              "QPushButton[_rightClicked = true]{background-color:#A0A0A0;} ";
    
        button->setStyleSheet(ColorString)  ;
    }
    

    The right clicked event is coming for the emit in the base class :

    #ifndef MATRIXBUTTON_H
    #define MATRIXBUTTON_H
    
    #include <QPushButton>
    #include <QMouseEvent>
    #include <QStyle>
    
    class MatrixButton : public QPushButton
    {
        Q_OBJECT
        Q_PROPERTY(bool _rightClicked READ wasRightClicked WRITE changeState)
    
    public:
        explicit MatrixButton(QWidget * parent = 0) : QPushButton(parent){}
        ~MatrixButton(){}
    
        bool wasRightClicked(){return _rightClicked;}
    
    public slots:
        void changeState(bool b)
        {
            _rightClicked = b;
            style()->unpolish(this);
            style()->polish(this);
        }
    
    signals:
        void rightClicked();
    
    protected:
    
        virtual void mousePressEvent(QMouseEvent *e)
        Q_DECL_OVERRIDE
        {
            if(e->button() == Qt::RightButton)
            {
                emit rightClicked();
                changeState(true);
            }
            else
            {
                changeState(false);
            }
            QPushButton::mousePressEvent(e);
    
        }
    private:
        bool _rightClicked = false;
    };
    
    #endif // MATRIXBUTTON_H
    
    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