views:

35

answers:

2

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 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.

+2  A: 

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.

Fred
Yes, thanks and +1. Just I didn't know that toolbar can be placed in a layout :).
Narek
+1  A: 

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.

alt text

Exa
Yes, using a main window is a good way if you want a text editor. But my applicatin alrady has a mainWindow and I can't use QMainWindow, thus I can't use setCentralWidget. Anyway thanks and +1 for a reasonable answer.
Narek