Reputation: 7505
I am learning QT and trying out some examples.
I am trying to make a dialog that disappears a label when a button is pressed and makes it appear when the same button is pressed again.
Below is the code.
#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QDialog>
#include <QObject>
#include <QHBoxLayout>
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
QDialog *dialog = new QDialog;
QPushButton *testButton = new QPushButton(QObject::tr("test"));
QLabel * testLabel = new QLabel (QObject::tr("test"));
QHBoxLayout * layout = new QHBoxLayout;
layout->addWidget(testButton);
layout->addWidget(testLabel);
QObject::connect(testButton, SIGNAL(toggled(bool)), testLabel, SLOT(setVisible(bool)));
dialog->setLayout(layout);
dialog->show();
return app.exec();
}
It is not working. Whenever i press the test button nothing happens. But if i change the signal slot connections as QObject::connect(testButton, SIGNAL(clicked(bool)), testLabel, SLOT(setVisible(bool)));
it makes the label disappear.
So, why it is not working with signal "toggled". What i am guessing is, it is not able to find that signal. Can you guys throw some light?
Upvotes: 0
Views: 744
Reputation: 8036
The problem is that QPushButton
's don't emit the toggled(bool)
signal. Only checkable widgets such as QCheckBox
do.
See the first line of the QAbstractButton::toggled
signal:
This signal is emitted whenever a checkable button changes its state.
Upvotes: 1
Reputation: 8516
You need to add:
testButton->setCheckable(true);
To enable toggling.
Upvotes: 3