PDA

View Full Version : Custom signals?



godot
14th January 2008, 18:31
Hello everyone. I'm a new Qt user and I'm trying to make a picture clickable. I know about putting an image onto a push button, but I do not want the extra border around the picture. I also know about putting the image onto a label-- but labels do not have a clicked signal.
So I made a custom class that inherits QLabel. It passes a pixmap to QLabel in the constructor. I want to make a clicked signal for this class, but the default "clicked()" signal uses QObjects, and this is a QPixmap. Is there a way for me to use mousePressEvent() to make my own signal?? Or should I have the class inherit both QLabel and QAbstractButton?
Thanks for the help.

jacek
14th January 2008, 18:35
So I made a custom class that inherits QLabel. It passes a pixmap to QLabel in the constructor. I want to make a clicked signal for this class, but the default "clicked()" signal uses QObjects, and this is a QPixmap.
You have subclassed QLabel, not QPixmap. You can add any signal you want without any problems, just don't forget about Q_OBJECT macro.

godot
14th January 2008, 18:42
You have subclassed QLabel, not QPixmap.

Correct, but I am using the pixmap() property of QLabel to display the image, which is a QPixmap.
I tried connecting the class itself to the clicked signal also, but that does not work either, even though I have the Q_OBJECT macro in the class.

jacek
14th January 2008, 18:44
Are there any errors on the console (you need "CONFIG += console" to see the console on windows)? How did you declare the signal? How do you make the connection? Could you post the relevant code?

godot
14th January 2008, 18:53
Course. Its very simple.




class ButtonLabel: public QLabel
{
Q_OBJECT

public:
ButtonLabel(QPixmap &pic);

Q_SIGNALS:
void clicked();


};





ButtonLabel::ButtonLabel(QPixmap &pic)
{
setPixmap(pic);
}





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

QPixmap pix(":/button.jpg");
ButtonLabel label(pix);
label.connect(label, SIGNAL(clicked()), label, SLOT(close()));
label.show();


return app.exec();
}

Edit: Ah, oops, I cut out the connect line... putting it back in, in the third code box

jacek
14th January 2008, 19:04
You don't emit that signal anywhere (and you should use &label in the connect statement).

godot
14th January 2008, 19:09
Thanks. It compiled alright when I added "&"s, but it did not close... probably because its not emitting a clicked signal, so it does not register as being clicked. So, I guess my question is now how do I emit a signal?

godot
14th January 2008, 19:13
Ooo. Nevermind. Never thought it'd be as easy as just adding "emit connect();" Thanks!