Solved Need to check the right-click event from a QButtonGroup
-
that should be easy enough:
#ifndef CBUTTON_H #define CBUTTON_H #include <QPushButton> #include <QMouseEvent> #include <QStyle> class cButton : public QPushButton { Q_OBJECT Q_PROPERTY(bool rightClicked READ wasRightClicked WRITE changeState) public: explicit cButton(QWidget * parent = 0) : QPushButton(parent){ } ~cButton(){} 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
the change in your code:
#include "cButton" void MyClass::Populate(QGridLayout *layout, const int rows, const int cols) { for ( int i = 0; i < rows; ++ i) { for ( int j = 0; j < cols; ++j) { cButton* btn = new cButton(" ") ; btn->setFixedWidth(20); btn->setStyleSheet("QPushButton{background-color:grey;}" "QPushButton[rightClicked = true]{background-color:red;}") ; layout->addWidget(btn,i,j) ; this->addButton(btn) ; } } }
-
I have issue using :
explicit MatrixButton(QWidget * parent = 0) : QPushButton(parent){}
error : prototype for 'MatrixButton::MatrixButton()' does not match any in class 'MatrixButton'
MatrixButton::MatrixButton()
^and not sure if the renaming of the clicked is correct, but it needed renaming as most have the same name. I think this could be ok :
#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
-
@doubitchou
you'll need to changeQ_PROPERTY(bool rightClicked READ wasRightClicked WRITE changeState)
to
Q_PROPERTY(bool _rightClicked READ wasRightClicked WRITE changeState)
to be comform to your renaming
this was intended as a header file only, class do you have a MatrixButton.cpp file ? that would explain a double MatrixButton::MatrixButton() definition
-
indeed better, anyway even if this is the same code I use for the default grey button color, nothing happen on right click.
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) { // QPushButton* btn = new QPushButton(" ") ; MatrixButton *btn= new MatrixButton(); btn->setFixedWidth(20); btn->setStyleSheet("QPushButton{background-color:grey;}" "QPushButton[rightClicked = true]{background-color:red;}") ; layout->addWidget(btn,i,j) ; this->addButton(btn) ; } } }
I have also tried :
btn->setStyleSheet("QPushButton{background-color:grey;}" "QPushButton:pressed{background-color:red;}") ;
-
@doubitchou
First of, you changed the variable name, so you'll need to change your stylesheet too,//from btn->setStyleSheet("QPushButton{background-color:grey;}" "QPushButton[rightClicked = true]{background-color:red;}") //to btn->setStyleSheet("QPushButton{background-color:grey;}" "QPushButton[_rightClicked = true]{background-color:red;}")
just tested it in a small sample project, with this btn construction:
MatrixButton *btn = new MatrixButton(); btn->setStyleSheet("QPushButton{background-color:red;}" "QPushButton[_rightClicked = true]{background-color:white;}"); btn->show();
works as expected. If this still does not work for you, than you probably have a stylesheet in a parent widget overwriting your background-color.
-
I see what you mean, in a way it's working. Until the click event, the white color is done.
But after the click who change the color, ie :void MyClass::buttonClick(QAbstractButton* button) { button->setStyleSheet("background-color: #ffa500;") ; }
the white from the right click doesn't appear anymore. even though this is not a parent, and this is done the same way (background-color) . Does your small example change the color on the click event and can put the white color back for the right-click ?
-
@doubitchou
Yes the example does that. The color is changed back when you click with any Mousebutton but the right one or when your call manuallychangeState
button->changeState(false);
with this
void MyClass::buttonClick(QAbstractButton* button)
{
button->setStyleSheet("background-color: #ffa500;") ;
}you overwrite the stylesheet you previously set, and
"QPushButton[_rightClicked = true]{background-color:white;}"
has no effect any longer because it is no longer appplied to the button. -
I see. Sorry, I don't really have much knowledge about Stylesheets.
enlightened by what you said, this is obvious now.
Anyway it's far from obvious to see how to be able to either change the color by click or by right click ?I try to put back the same code you provide, when the click event appears ?
-
Well it seems right, I don't know if this is the best implementation but it's working. I have to overwrite that way
QString ColorString = "QPushButton{background-color: " + QString("#%1").arg(BtnColorValue, 6, 16, QLatin1Char( '0' )) + ";} QPushButton[_rightClicked = true]{background-color:white;} ";
It seems a complicated way to build the string but BtnColorValue come from a color list (which is a QList) I have to cycle with
-
Thanks for your precious help, I still don't figure how to manage events with stylesheets as the docs are mostly about visual customization, but I'm sure this can be found somewhere.