Results 1 to 10 of 10

Thread: Creating click events for label

  1. #1
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Creating click events for label

    Can someone point me in the right direction of making a click-able label? I'm working with some code I found on a site, but I'm not converting it right from key event to most events...

    Qt Code:
    1. class ClickLabel : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5.  
    6. protected:
    7. bool eventFilter(QObject *obj, QEvent *event);
    8. };
    9.  
    10. bool ClickLabel::eventFilter(QObject *obj, QEvent *event)
    11. {
    12. if (event->type() == QEvent::MouseButtonPress) {
    13. QMouseEvent *MouseEvent = static_cast<QMouseEvent *>(event);
    14. qDebug("MouseClicked", MouseEvent->clicked());
    15. return true;
    16. } else {
    17. // standard event processing
    18. return QObject::eventFilter(obj, event);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 


    errors

    error: invalid static_cast from type 'QEvent*' to type 'QMouseEvent*'
    error: invalid use of incomplete type 'struct QMouseEvent'
    error: forward declaration of 'struct QMouseEvent'

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Creating click events for label

    Qt Code:
    1. #include <QMouseEvent>
    2.  
    3. class ClickLabel : public QObject
    4. {
    5. Q_OBJECT
    6. ....
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating click events for label

    Is it easy to inherit the functionality of the QLabel class into the new class, so that I can easily use the same features like set text, and stuff of that nature?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating click events for label

    Yes, inherit from QLabel and not QObject.

  5. #5
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating click events for label

    It's discouraging QT hasn't added a simple clickable image, lol. A label should have a click slot by default, such a simple thing to over look....If I wasn't such a newb I could figure it out, but I need some help, lol.
    Last edited by InterFiction; 25th November 2011 at 01:01.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating click events for label

    That is a constructor for your class and the arguments are used to initialise the object at creation. In this case the first argument is mandatory (the text of the label), and the second has a default value but could point to a parent QObject. QLabel has two constructors, distinguished by their arguments lists, and this mirrors one of them in your derived class.

    Do yourself a favour an learn some basic C++ before you try anything too complicated with Qt.


    Edit:
    A clicked() slot would be generally useless in a label. What would a generic label do in response to the clicked() slot being called?
    I think you mean a clicked() signal, and you already have the 10 lines of code you need to add it if you need it. Most labels don't need it.
    Last edited by ChrisW67; 25th November 2011 at 01:12. Reason: spelling corrections

  7. #7
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating click events for label

    I know. It's for having a clickable image. I'm working on a piece of software, and need an image to be click able. I know some basic C++, just need to learn a little more, but I'm having fun doing it....

    A clickable image is a big deal, haha...it should be easier to create...

    are there simpler alternatives?

    I could use the above code if I could figure out how to make the new class do everything that the regular QTLabel class can. Then I could create my labels, add the pixmaps, and figure out how to use the slicked signal.

    You can see what I'm working on here..

    http://s1226.photobucket.com/albums/...GenBeta113.png

    I had it all set up, didn't know it would be so hard for me to make a qlabel image clickable...I figured qt would have thought of that one, lol.
    Last edited by InterFiction; 25th November 2011 at 01:26.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating click events for label

    Please don't edit posts in such a way that it breaks replies: it makes it hard to follow for others.

    Apart from the obvious alternative of putting an image on a QPushButton, which is designed for clicking, you could:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class ClickLabel: public QLabel
    5. {
    6. Q_OBJECT
    7. public:
    8. ClickLabel(QWidget * parent = 0, Qt::WindowFlags f = 0):
    9. QLabel(parent, f)
    10. { }
    11.  
    12. ClickLabel(const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0):
    13. QLabel(text, parent, f)
    14. { }
    15.  
    16. protected:
    17. void mousePressEvent ( QMouseEvent * ev ) {
    18. if (ev->button() == Qt::LeftButton) {
    19. emit clicked();
    20. qDebug() << "Click";
    21. }
    22. }
    23.  
    24. signals:
    25. void clicked();
    26. };
    27.  
    28. int main(int argc, char *argv[])
    29. {
    30. QApplication app(argc, argv);
    31.  
    32. ClickLabel w1("Some text to click");
    33. w1.show();
    34. ClickLabel w2;
    35. w2.setPixmap(QPixmap("test.png"));
    36. w2.show();
    37. return app.exec();
    38. }
    39. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating click events for label

    Oh bomb. You just pointed out something I didn't realize. That's one of the reasons I was having trouble with my new class, and inherited features from QLabel. I was trying to access functions via -> instead of .

    That takes me close, see I know where I want to be, just the little things that I need to understand to make it happen. Now I should be able to make my labels, set the pixmaps, and use the signals from my new class right?

    I'm going to have to delete the labels that I've already made to code the new ones. How can I take down their quardinates on the form, so that I don't have any guess work? I'm guess one of the properties in qtdesigner holds that information?

    thanks man.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating click events for label

    If you have already got a UI designed using Designer then you can use the promotion feature to convert your QLabels into ClickLabels. If you hard coded your UI then just create ClickLabels wherever you created QLabels before.

    BTW: You should be using a layout to manage placement of your widgets, in which case there are no coordinates to take down because they are all generated by layout rules.

Similar Threads

  1. Replies: 0
    Last Post: 17th March 2011, 19:38
  2. Click events
    By Maluko_Da_Tola in forum Newbie
    Replies: 0
    Last Post: 26th August 2010, 01:05
  3. how to get the position of mouse click on a label
    By qt_user in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2010, 09:14
  4. Detect click on QTableWidget label
    By Darthspawn in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2010, 10:50
  5. close window when click on a label
    By sabeesh in forum Qt Programming
    Replies: 3
    Last Post: 29th October 2007, 07:35

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.