How to create a class that is a widget that has a QTextEdit and a QToolBar above the text edit
My intent is to create a QTextEdit with its reach text controls. The controls I want to put in a toolbar. But I have difficulties with controling开发者_StackOverflow中文版 the layout. The problem is that the overlap if I put a QTextEdit in a QWidget (my class inherits QWidget) which has a toolbar.
Another way I tried was the following: my class inherits QTextEdit, and it has a toolbar. Now the layout is different but not what I want. PLease help me to have a nice view.
Place your toolbar and QTextEdit in a layout inside your class which inherits QWidget. Layouts (see QVBoxLayout) positions items relative to each other making sure they don't overlap. If you don't use a layout, all child widgets will be created at position (0,0), meaning at the top-left corner of the parent widget.
QWidget* widget = new QWidget();
QToolBar* toolbar = new QToolBar(widget);
QTextEdit* textedit = new QTextEdit(widget);
QVBoxLayout* layout = new QVBoxLayout(widget);
layout->addWidget(toolbar);
layout->addWidget(textedit);
And voila, the widgets don't overlap anymore.
Some time ago I wrote my own text editor and I did it a little bit like you. I use a QMainwWindow as main_window and as my central widget a simple QWidget with a layout (QVBoxLayout) on it. In that layout I placed a QTabWidget in which I could add my own text_edit (derived from QTextEdit) as new tabs.
I had three toolbars which I simply added to the main window. So they can be moves freely around my self-wrote QTextEdit.
I also used DockWidgets to add a file explorer and a logging window.
精彩评论