PDA

View Full Version : create a custom slot, hints?



pledians
2nd October 2008, 15:16
Hi. I've read as much info as i could about creating custom slots, but i still cant get it work, maybe you could give me i hint about what im i doing wrong? The idea is, that when i priss button b, my custom slot 'print' shoud execute, but nothing happens! (no compilation errors or warnings either) :(


#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include <qmessagebox.h>

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);

public slots:
void print();
};

void MyWidget:: print()
{
QMessageBox::information(this, "Some window label here", "hello");
}

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(200, 120);

QPushButton *b = new QPushButton(tr("QT"), this);[/B]
quit->setGeometry(62, 40, 75, 30);

connect(b, SIGNAL(clicked()), this, SLOT(print()));[/B]
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}

caduel
2nd October 2008, 15:26
* add Q_OBJECT to your class
* add your header file to your .pro file's HEADERS section
(or include "#include "yourfile.moc" in yourfile.cpp if your class decl is in your .cpp file)

PS: at the moment you should get a runtime warning that this slot does not exist

HTH