PDA

View Full Version : Subclassing a widget created in Qt Designer



dmginc
19th January 2011, 05:55
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.



#include "ui_MainWindow.h"

class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
};

etc.

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

ChrisW67
19th January 2011, 06:04
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.

dmginc
19th January 2011, 06:30
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! ;)