PDA

View Full Version : Problem with QPushButton setDisabled SLOT



code_err
8th October 2011, 18:08
I was trying to find solution on forum but couldn't. The problem is i connected SIGNAL from QComboBox with QPushButton SLOT. I want it to be disabled after changing item from combo list but it doesn't work. When i connect this same SIGNAL with qApp quit() SLOT it works.


QObject::connect(&combo, SIGNAL(activated(int)), &button, SLOT(setDisabled()));

Button inherits this SLOT from QWidget but i don't know why in the Qt Designer i cannot see this SLOT.


#include <QApplication>
#include <QtCore>
#include <QtGui>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QWidget window;
QVBoxLayout *layout = new QVBoxLayout;

QStringList list;
list << "jeden" << "dwa" << "trzy" << "cztery" << QString::fromUtf8("pięć");

QComboBox combo;
combo.addItems(list);

QPushButton button(QString::fromUtf8("Wyłącz się do cholery"));

window.setLayout(layout);

layout->addWidget(&combo);
layout->addWidget(&button);

QObject::connect(&combo, SIGNAL(activated(int)), &button, SLOT(setDisabled(bool)));
QObject::connect(&button, SIGNAL(clicked()), qApp, SLOT(quit()));

window.show();
return app.exec();
}

it is complete main.cpp

Ok, what is wrong with this code?

Zlatomir
8th October 2011, 18:19
There are two issues with your connect statement:
1) Your parameters mismatch, read here (http://doc.qt.nokia.com/4.7/signalsandslots.html) about signal-slot connections.
And second
2) you can't use values in the connect (you can use only types) so you can't write false just bool

LE: i see you edited your post
with quit it works because the int parameter is not needed (since quit doesn't take any parameters the int is ignored) but the setDisable slot needs to be called with one bool parameter.

code_err
8th October 2011, 18:25
So i need to write my own SIGNAL? and will have to write a new class just for this little thing?

Zlatomir
8th October 2011, 18:36
Any way it's better to have your "gui"/widget encapsulated into a class (for example see the "pattern" for ui usage).
So you can derive a class from QWidget and add combo and button (and other widgets you need) and code the gui functionality you need (by coding signals, slots, make the needed connections) so that the object will be usable.

code_err
8th October 2011, 18:45
Thanks for quick response.