Results 1 to 7 of 7

Thread: Subclassing QLabel

  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Subclassing QLabel

    Hello All

    I am attempting to subclass QLabel in order to handle mouse click events. The x and y values of the mouse click will eventually be passed to QRubberBand() for further processing. Naturally, the QRubberBand will be in a separate class to the sub-classed QLabel.

    My main project is actually using QtDesigner to build the GUI but to make this simpler I have rewritten the code in question without Designer and without the none relevant code from my main project.

    The following code snippet compiles and works perfectly but how do I get the variables x and y from MyLabel() class into Communicate()?

    HEADER FILE CALLED: communicate.h

    Qt Code:
    1. // communicate.h
    2.  
    3. #ifndef COMMUNICATE_H
    4. #define COMMUNICATE_H
    5.  
    6. #include <QApplication>
    7. #include <QWidget>
    8. #include <QApplication>
    9. #include <QMouseEvent>
    10. #include <QLabel>
    11. #include <iostream>
    12.  
    13. class Communicate : public QWidget
    14. {
    15. Q_OBJECT
    16. public:
    17. Communicate(QWidget *parent = 0);
    18.  
    19. private:
    20.  
    21. };
    22.  
    23. class MyLabel : public QLabel
    24. {
    25. Q_OBJECT
    26. public:
    27. MyLabel(QWidget *parent = 0);
    28. void mousePressEvent(QMouseEvent *event);
    29.  
    30. private:
    31.  
    32. };
    33.  
    34. #endif /* COMMUNICATE_H */
    To copy to clipboard, switch view to plain text mode 

    SOURCE FILE CALLED: communicate.cpp

    Qt Code:
    1. //communicate.cpp
    2.  
    3. #include "communicate.h"
    4.  
    5. Communicate::Communicate(QWidget* parent)
    6. : QWidget(parent)
    7. {
    8. int WIDTH = 400;
    9. int HEIGHT = 400;
    10.  
    11. resize(WIDTH, HEIGHT);
    12.  
    13. MyLabel * label = new MyLabel(this);
    14. label->setGeometry(20,20, 100, 50);
    15. label->setText("CLICK AREA");
    16. label->show();
    17. }
    18.  
    19. MyLabel::MyLabel(QWidget* parent)
    20. : QLabel(parent)
    21. {
    22. }
    23.  
    24. void MyLabel::mousePressEvent(QMouseEvent* event){
    25. int x,y;
    26.  
    27. x = event->pos().x();
    28. y = event->pos().y();
    29.  
    30. /* ***DEBUGGING***
    31.   Check x and y are correct after mouse click on sub-classed label */
    32.  
    33. std::cerr << "\nX: " << x;
    34. std::cerr << "\nY: " << y;
    35.  
    36. return;
    37. }
    To copy to clipboard, switch view to plain text mode 

    FINALLY: main.cpp

    Qt Code:
    1. #include "communicate.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. Communicate w;
    8. w.setWindowTitle("Communication Test");
    9. w.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    As I said, I would like to get the variables x and y from MyLabel into the scope of the Communicate class. Any help would be well received.

    Regards
    Niola

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Subclassing QLabel

    You can create a signal that emits the x and y values directly from the mousePressEvent overload, or store them as a member of the class MyLabel and access when you will need them, there are many ways you can achieve that.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Subclassing QLabel

    You should be able to use signal-slot cmmunication, have a singal in MyLabel which is emited inside mousePressEvent(), and connect it to a slot in Communicate class, then process x & y in the slot. (you need to have x & y as parametes for both the signal & slot)

    This should be fairly strightforward, looks like you have some constraints, which are mentioned in the question.

  4. #4
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QLabel

    Hello Santosh Reddy & Zlatomir

    Thank you for the responses thus far.

    Unfortunately I am quite new to Qt Signals / Slots outside of what I normally do. (for example linking buttons to functions.) I have googled lots and read basic tutorials on Signals and Slots but am still unsure of how to implement this.

    Would you be so good as to help with some code fitting my example?

    Many Thanks
    Nicola

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Subclassing QLabel

    You can do this without signal and slot connections - just C++ code: store x and y as members is MyLabel and code a getter function (or maybe put them public - whatever is appropriate)

    To use signals and slots you declare a member signal in your class (you don't implement the signal):
    Qt Code:
    1. class MyLabel : public QLabel
    2. {
    3. Q_OBJECT
    4. signals:
    5. void mySignal(int x, int y);
    6. //...
    7. };
    To copy to clipboard, switch view to plain text mode 
    And then after you get the x and y you can emit the signal:
    Qt Code:
    1. //...
    2. x = event->pos().x();
    3. y = event->pos().y();
    4.  
    5. emit mySignal(x, y);
    To copy to clipboard, switch view to plain text mode 
    And in your Communicate class code a slot (just like a member function, only declared with public slots: access specifier) that take two integers as parameter.

    And in the Communicate constructor (after you create MyLabel object) connect the your signal with the slot:
    Qt Code:
    1. //...
    2. connect(label, SIGNAL(mySignal(int, int), this, SLOT(WhatEverSlotNameYouChose(int, int)));
    To copy to clipboard, switch view to plain text mode 

    Read more about signals and slots here

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Subclassing QLabel

    I tried to modify your code and added the signal and slots required, have a look at it, also I recommed to have look at the signal-slot examples to help your self, in figuring out how to use them effectively.

    communicate.h
    Qt Code:
    1. #ifndef COMMUNICATE_H
    2. #define COMMUNICATE_H
    3.  
    4. #include <QApplication>
    5. #include <QWidget>
    6. #include <QApplication>
    7. #include <QMouseEvent>
    8. #include <QLabel>
    9. #include <iostream>
    10.  
    11. class Communicate : public QWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. Communicate(QWidget *parent = 0);
    16.  
    17. // Added
    18. public slots:
    19. void newMousePos(int x, int y);
    20.  
    21. private:
    22.  
    23. };
    24.  
    25. class MyLabel : public QLabel
    26. {
    27. Q_OBJECT
    28. public:
    29. MyLabel(QWidget *parent = 0);
    30. void mousePressEvent(QMouseEvent *event);
    31.  
    32. // Added
    33. signals:
    34. void selected(int x, int y);
    35.  
    36. private:
    37.  
    38. };
    39.  
    40. #endif /* COMMUNICATE_H */
    To copy to clipboard, switch view to plain text mode 

    communicate.cpp
    Qt Code:
    1. //communicate.cpp
    2.  
    3. #include "communicate.h"
    4.  
    5. Communicate::Communicate(QWidget* parent)
    6. : QWidget(parent)
    7. {
    8. int WIDTH = 400;
    9. int HEIGHT = 400;
    10.  
    11. resize(WIDTH, HEIGHT);
    12.  
    13. MyLabel * label = new MyLabel(this);
    14. label->setGeometry(20,20, 100, 50);
    15. label->setText("CLICK AREA");
    16. label->show();
    17.  
    18. // Added
    19. connect(label, SIGNAL(selected(int , y), this, newMousePos(int, int));
    20. }
    21.  
    22. // Added
    23. void Communicate::newMousePos(int x, int y);
    24. {
    25. cout << x;
    26. cout << y;
    27. }
    28.  
    29. MyLabel::MyLabel(QWidget* parent)
    30. : QLabel(parent)
    31. {
    32. }
    33.  
    34. void MyLabel::mousePressEvent(QMouseEvent* event){
    35. int x,y;
    36.  
    37. x = event->pos().x();
    38. y = event->pos().y();
    39.  
    40. /* ***DEBUGGING***
    41.   Check x and y are correct after mouse click on sub-classed label */
    42.  
    43. std::cerr << "\nX: " << x;
    44. std::cerr << "\nY: " << y;
    45.  
    46. emit selected(x, y);
    47. return;
    48. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "communicate.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. Communicate w;
    8. w.setWindowTitle("Communication Test");
    9. w.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    looks like we are wokring in parallel

  7. #7
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QLabel

    A big thank you to everyone...

    I now have it working and have learned quite a bit too

Similar Threads

  1. Subclassing QLabel
    By Maluko_Da_Tola in forum Newbie
    Replies: 2
    Last Post: 28th August 2010, 01:35
  2. Replies: 2
    Last Post: 23rd November 2009, 14:03
  3. Replies: 1
    Last Post: 29th September 2009, 19:44
  4. Replies: 1
    Last Post: 2nd August 2008, 15:46
  5. [SOLVED] subclassing qlabel
    By mickey in forum Newbie
    Replies: 10
    Last Post: 4th June 2008, 14:43

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.