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/.hGreetings
Maxim -
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/editorThe 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, ctorEditor(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 itemThen add the info (editor.h and Editor)
and press ADD
Then Press Promote
Then Press oKThen rebuild all
and now when you run the Widget in UI becomes your custom widget
Hope this explains well enough, else ask :)
changed project
https://www.dropbox.com/s/j002mgft8iipml1/qt_IDE-master.zip?dl=0 -
@FleeXo
Super :)
The docs just in case
https://doc.qt.io/qt-5/designer-using-custom-widgets.htmlAlso 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.I love the Promotion feature and use alot :)