Button inherit stylesheet for no reason
-
How do I implement this code way? I do my UI in code since that's what I prefer. This is what I've got so far and it isn't working...
QButtonGroup *g_btns = new QButtonGroup(this); /* all buttons are created and I add the buttons in about this way*/ btn1->setCheckable(true); //etc.. g_btns->addButton(btn1); g_btns->addButton(btn2); g_btns->addButton(btn3); // etc ... connect(g_btns, SIGNAL(buttonClicked(int)), this, SLOT(onGroupButtonClicked(int))); void MainWindow::onGroupButtonClicked(int btn_id) { qDebug() << "clicked!"; }
And this gives me nothing. The onGroupButtonClicked does not activate.
-
@legitnameyo I never used QButtonGroup, but a quick look into the documentation tells me, you never gave your added buttons an ID
void QButtonGroup::addButton(QAbstractButton *button, int id = -1)
I'm guessing here and say, that buttons with the ID of -1 do not emit the buttonClicked(int) signal
-
@legitnameyo It should work. Do you get any warnings at runtime (related to connect)?
Is g_btns in connect() really the one you're creating at the beginning of the code snippet you provided? -
@J.Hilk
Was a good guess,
but it seems, it will use negative values for auto numbering and does, in fact, emit them.QButtonGroup *buttonGroup = new QButtonGroup(this); buttonGroup->addButton( ui->pushButton_2); buttonGroup->addButton( ui->pushButton_3); buttonGroup->addButton( ui->pushButton_4); buttonGroup->addButton( ui->pushButton_5); connect(buttonGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), [ = ](int id) { qDebug() << id; });
outputs.
-2
-2
-3
-5
-4 -
There is NO error such as this one below
QMetaObject::connectSlotsByName: No matching signal for onGroupButtonClicked(int)
And yes g_btns is defined only ONCE. Everything is in the order I got it in my code. However one thing that might change things up is that I use a custom class for my buttons called QClickAgnostic, which inherits from QPushButton as such above
and the buttons are created like this
QClickAgnostic *btn = new QClickAgnostic(ui->frame_of_btns);
-
@legitnameyo
You need to notify the ButtonGroup about the click event.I took a look at the source code of the abstractbutton and noticed the button directly notifies the group that it was clicked. But you broke this step when you reimplemented the MouseReleaseEvent function.
See a snippet of this step.
void QAbstractButton::mouseReleaseEvent(QMouseEvent *e) { ... if (hitButton(e->pos())) { d->repeatTimer.stop(); d->click(); // call click function e->accept(); } else { setDown(false); e->ignore(); } } void QAbstractButtonPrivate::click() { ... if (guard) emitReleased(); if (guard) emitClicked(); // call emitClicked } void QAbstractButtonPrivate::emitClicked() { ... #if QT_CONFIG(buttongroup) if (guard && group) { emit group->buttonClicked(group->id(q)); // emit the signal if (guard && group) emit group->buttonClicked(q); // emit the signal } #endif }
So, you can solve this problem calling the click() function inside MouseReleaseEvent.
void QClickAgnostic::mouseReleaseEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { click(); // called emit leftClicked(); } else if(e->button() == Qt::RightButton) { click(); // called emit rightClicked(); } }
-
@KillerSmath
Good digging ! -
@KillerSmath fix did the work, combined with @mrjj QGroupButton! I added this code to style the pressed button
void MainWindow::onGroupButtonClicked(int btn_id) { g_btns->button(btn_id)->setStyleSheet(QString("QPushButton {color: #444444; background: #cccccc; border: 2px solid #cccccc; border-width: 0px 0px 2px 0px;} QPushButton:hover {border: 2px black solid; color: #333333; background-color: #F7F7F7;} QPushButton:pressed {background: #cccccc;}")); }
Thanks a lot guys!