Results 1 to 8 of 8

Thread: Custom signals?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Custom signals?

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom signals?

    Quote Originally Posted by godot View Post
    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.

  3. #3
    Join Date
    Jan 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom signals?

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom signals?

    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?

  5. #5
    Join Date
    Jan 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom signals?

    Course. Its very simple.


    Qt Code:
    1. class ButtonLabel: public QLabel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ButtonLabel(QPixmap &pic);
    7.  
    8. Q_SIGNALS:
    9. void clicked();
    10.  
    11.  
    12. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. ButtonLabel::ButtonLabel(QPixmap &pic)
    2. {
    3. setPixmap(pic);
    4. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QPixmap pix(":/button.jpg");
    6. ButtonLabel label(pix);
    7. label.connect(label, SIGNAL(clicked()), label, SLOT(close()));
    8. label.show();
    9.  
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Edit: Ah, oops, I cut out the connect line... putting it back in, in the third code box
    Last edited by godot; 14th January 2008 at 18:57.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom signals?

    You don't emit that signal anywhere (and you should use &label in the connect statement).

  7. #7
    Join Date
    Jan 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom signals?

    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?

  8. #8
    Join Date
    Jan 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom signals?

    Ooo. Nevermind. Never thought it'd be as easy as just adding "emit connect();" Thanks!

Similar Threads

  1. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 12:41
  2. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18
  3. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 15:38
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. KDE Signals
    By chombium in forum KDE Forum
    Replies: 1
    Last Post: 25th January 2006, 18:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.