Results 1 to 2 of 2

Thread: How to find in which cell mouse press event occured in QGridLayout

  1. #1
    Join Date
    Jul 2007
    Posts
    25
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to find in which cell mouse press event occured in QGridLayout

    Hi,

    I've a QGridLayout consisting of QLabel widgets in it. I want to know how to find out in which cell particularly the mouse Press even occured, as this is necessary for me to know which label widget has been clicked.
    Any suggestions, help will be appreciated

    Thanks and Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find in which cell mouse press event occured in QGridLayout

    Layouts are strictly for arrangement. You must not use them for user interaction. Create you own label class instead:
    Qt Code:
    1. //header
    2. class MyLabel : public QLabel
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. MyLabel(const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0);
    8. ~MyLabel();
    9.  
    10. signals:
    11. void clicked();
    12. protected:
    13. void mousePressEvent(QMouseEvent*);
    14. }
    15.  
    16. //cpp
    17. MyLabel::MyLabel(const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0)
    18. :QLabel(text, parent, f)
    19. {
    20. }
    21.  
    22. MyLabel::~MyLabel()
    23. {
    24. }
    25.  
    26. void MyLabel::mousePressEvent(QMouseEvent*)
    27. {
    28. emit clicked();
    29. }
    To copy to clipboard, switch view to plain text mode 


    All you have to do is to connect the clicked signal of each label to some slot(s).

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.