Search code examples
windows-10qt5qt-creatorqplaintexteditcustom-widgets

How can I make Qt use a user-defined class?


I'd like to user a user-defined class rather Qt's generated in the Ui_MainWindow class so that I can use that control on Qt's GUI Designer. For example, currently it's defined as this:

class Ui_MainWindow
{
public:
    QPlainTextEdit *list;
 // ...
};

I'd like to use my user-defined class:

class Ui_MainWindow
{
public:
    MyCustomQPlainTextEdit *list;
 // ...
};

I cannot just modify it by hand in the ui_mainwindow.h header file because that value will be lost every time the GUI designer generate its metadata. Can I prevent Qt to use that class so that I can use my own?


Solution

  • If your MyCustomQPlainTextEdit inherits from QPlainTextEdit you can "promote" the widget in your ui file to your custom class.

    Open your ui file in Qt Creator, right click the widget and select Promote To

    [enter image description here]

    Then in the dialog that opens up, add your custom class like so:

    [enter image description here]

    Be sure to alter the path to the header file if needed depending on how your project is set up. Then click Add.

    After you custom class has been added, click Promote and voila, your custom class in the generated ui_<blah>.h.