Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Creating double Widgets



  • Hey,
    Im very new to Qt. Now I followed the Qt Code Editor Example and wanted adapt this example to an ui form. But now it is getting
    created twice. Once with the right logic and once just blank.
    Maybe someone can help me and teach me what I am doing wrong.
    I would really appreciate it.
    Here are the sources: https://github.com/tzAcee/qt_IDE Everything about the editor is in the editor.cpp/.h

    Greetings
    Maxim


  • Lifetime Qt Champion

    Hi and welcome to the forums

    Your custom editor is also a PlainTextWidget so i think maybe you were meant to use the promotion feature
    to have that editor in UI become your custom widget/editor

    The code you have simply create a new Editor and use the existing
    "Editor" as parent.
    QPlainTextEdit a = ui->centralwidget->findChild<QPlainTextEdit>("editor");
    _editor = new Editor(a);
    This just create a new one. And as you see is put inside the Editor you already have on UI form.

    Lets try Promotion. ( which is a Creator feature that lets you use custom widgets)
    First
    remove lines
    // QPlainTextEdit a = ui->centralwidget->findChild<QPlainTextEdit>("editor");
    // _editor = new Editor(a);
    then
    go to Editor.H, ctor

    Editor(QPlainTextEdit *parent = nullptr);

    This says parent will be QPlainTextEdit but that wont work. so
    Change it to
    Editor(QWidget *parent = nullptr);
    also change in .CPP ! ( Editor::Editor(QWidget *parent) : QPlainTextEdit(parent) )

    Then go to Mainwindow.ui
    right click the Editor widget and select "Promote To" menu item

    Then add the info (editor.h and Editor)
    alt text
    and press ADD
    Then Press Promote
    Then Press oK

    Then rebuild all
    and now when you run the Widget in UI becomes your custom widget
    alt text

    Hope this explains well enough, else ask :)

    changed project
    https://www.dropbox.com/s/j002mgft8iipml1/qt_IDE-master.zip?dl=0



  • @mrjj Thank you very much it just works fine.
    The hint with the promoting feature was just amazing!


  • Lifetime Qt Champion

    @FleeXo
    Super :)
    The docs just in case
    https://doc.qt.io/qt-5/designer-using-custom-widgets.html

    Also note we have dynamic properties.
    So you can add new properties in Designer and read at runtime in code.
    That makes it possible to be able to tweak custom widgets design time without having to write a full-blown
    plugin to have them truly as custom widgets as the other std. widgets.

    alt text

    I love the Promotion feature and use alot :)



  • @mrjj I will definitly give it a try, thank you :)


Log in to reply