Results 1 to 3 of 3

Thread: Subclassing a widget created in Qt Designer

  1. #1
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Subclassing a widget created in Qt Designer

    I have a QLabel within my Qt Designer-created MainWindow.

    However, I would like to add mouse tracking to this QLabel by subclassing QLabel and creating a new class called QLabelWithMouseTracking which will contains re-implementations of the appropriate protected functions: mousePressEvent(), mouseReleaseEvent() etc.

    My question is:

    How do I perform the subclassing and create a new class given that this QLabel is already created and initialized in the Designer UI header file?

    Also, I'm using the multi-inheritance approach. i.e.

    Qt Code:
    1. #include "ui_MainWindow.h"
    2.  
    3. class MainWindow : public QMainWindow, private Ui::MainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MainWindow(QWidget *parent = 0);
    9. };
    To copy to clipboard, switch view to plain text mode 

    etc.

    Thanks in advance to anyone who can steer me in the right direction!

  2. #2
    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: Subclassing a widget created in Qt Designer

    Code your derived class. Use the promotion feature in Designer to make the existing label an instance of your sub-class. The promotion dialog can be found on the right-click context menu in Designer.

  3. The following user says thank you to ChrisW67 for this useful post:

    dmginc (19th January 2011)

  4. #3
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Subclassing a widget created in Qt Designer

    Quote Originally Posted by ChrisW67 View Post
    Code your derived class. Use the promotion feature in Designer to make the existing label an instance of your sub-class. The promotion dialog can be found on the right-click context menu in Designer.
    Works perfectly! Thanks so much for your help!

Similar Threads

  1. Replies: 4
    Last Post: 8th April 2011, 23:17
  2. Replies: 5
    Last Post: 10th November 2009, 16:46
  3. Replies: 5
    Last Post: 7th May 2009, 15:29
  4. Replies: 2
    Last Post: 4th May 2009, 12:57
  5. Drawing on a widget created via QDesigner
    By jean in forum Qt Programming
    Replies: 5
    Last Post: 15th January 2009, 14:15

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.