QRadioButton signals don't work
-
Hi, I'm trying to catch a signal from QRadioButtons, but I can't seem to get it working.
Here's what I have:
mainwindow.h:... private slots: void rAuto_clicked(); ... public: QRadioButton *rAuto;mainwindow.cpp:
void MainWindow::rAuto_clicked() { qDebug() << "rAuto"; //doesn't print anything } ... ui->setupUi(this); rAuto = new QRadioButton; QObject::connect(rAuto,SIGNAL(clicked()),SLOT(rAuto_clicked())); ...I mainly used
QObject::connect()for sending signals from threads, so I might be doing this wrong. I also looked on Stackoverflow, but the answer there didn't help me at all.I also tried using the
toggled(const bool&)signal (and modified the slot), but that also didn't work. -
Hi, I'm trying to catch a signal from QRadioButtons, but I can't seem to get it working.
Here's what I have:
mainwindow.h:... private slots: void rAuto_clicked(); ... public: QRadioButton *rAuto;mainwindow.cpp:
void MainWindow::rAuto_clicked() { qDebug() << "rAuto"; //doesn't print anything } ... ui->setupUi(this); rAuto = new QRadioButton; QObject::connect(rAuto,SIGNAL(clicked()),SLOT(rAuto_clicked())); ...I mainly used
QObject::connect()for sending signals from threads, so I might be doing this wrong. I also looked on Stackoverflow, but the answer there didn't help me at all.I also tried using the
toggled(const bool&)signal (and modified the slot), but that also didn't work.@Sucharek said in QRadioButton signals don't work:
QObject::connect(rAuto,SIGNAL(clicked()),SLOT(rAuto_clicked()));
QObject::connect(rAuto,SIGNAL(clicked()),this,SLOT(rAuto_clicked()));Better would be:
QObject::connect(rAuto, &QRadioButton::clicked, this, &MainWindow::rAuto_clicked);And of course make sure you actually put that
rAuto = new QRadioButton;radiobutton on a widget somewhere! -
@Sucharek said in QRadioButton signals don't work:
QObject::connect(rAuto,SIGNAL(clicked()),SLOT(rAuto_clicked()));
QObject::connect(rAuto,SIGNAL(clicked()),this,SLOT(rAuto_clicked()));Better would be:
QObject::connect(rAuto, &QRadioButton::clicked, this, &MainWindow::rAuto_clicked);And of course make sure you actually put that
rAuto = new QRadioButton;radiobutton on a widget somewhere! -
Hi @JonB, thanks for your reply, but this doesn't work either.
I did add the button to MainWindow. It's in a different void.
-
Hi,
Nothing in your code hints that you are showing that button.
Seeing that you have a call to setupUi I would venture that you are clicking on a button you added through Designer and connecting a button you created in code but not shown nor added to said Designer based part.
-
Hi,
Nothing in your code hints that you are showing that button.
Seeing that you have a call to setupUi I would venture that you are clicking on a button you added through Designer and connecting a button you created in code but not shown nor added to said Designer based part.
Hi @SGaist, I have a different void for adding that button.
It's like a setup wizard, so I have to change what's in the MainWindow. I have a gridLayout set up in designer, where I add what I need to add or remove.I'm adding the button this way:
... } else if (scene == 4) { ... ui->gridLayout_Content->addWidget(rAuto, 2, 0); ... } ... -
Then are you sure that it is really that button that is shown and connected and you did not replace the value in that variable later on ?
That said, since you mention a wizard, did you consider using QWizard ?
Also, since it is supposed to be a wizard showing different "scenes", rather than trying to make everything in one single widget, you should rather have one widget per "scene" and use something like QStackedWidget to switch between them.
-
Then are you sure that it is really that button that is shown and connected and you did not replace the value in that variable later on ?
That said, since you mention a wizard, did you consider using QWizard ?
Also, since it is supposed to be a wizard showing different "scenes", rather than trying to make everything in one single widget, you should rather have one widget per "scene" and use something like QStackedWidget to switch between them.
@SGaist I'm sure it's the same button.
I tested it too. I made a button in the editor and when I clicked it, I got if the radio button is toggled. If it was, it printed true, if it wasn't, false. So I'm sure it is that button.Thanks for the suggestions. I probably won't use QWizard, because I already have a lot of code written, but I might use QStackedWidget.
-
Can you try signal pressed()? You can add different text or background color to this radio button to make sure if it is the right one or not.
-
@Sucharek said in QRadioButton signals don't work:
qDebug()
does qDebug() work at other places? Set a break point at
qDebug() << "rAuto"; //doesn't print anything -
@JoeCFD yes,
qDebug()does work. I tried it on my temporary QPushButton.
I think I don't know how to use breakpoints. I set multiple poins, started the app in debug mode, but none of them done anything. Even the ones I know got executed. -
@Sucharek replace qDebug() << "rAuto"; with
std::cout << "========= rAuto" << std::endl;you need header
#include <iostream>It can not be true that the signal of QRadioButton does not work. So many people are using it.
@JoeCFD, nope, doesn't print anything.
I also tried commands likeclose()and they didn't work either.Well, just realised it was my fault all along.
Apparently I put therAuto = new QRadioButtonin a wrong place. I put it in a void that sets the buttons text, font size, etc. and I guess I shouldn't do that.Anyways, thanks for the help everyone.