[Qt-interest] widget in dockwidget do not get resized

Andreas Pakulat apaku at gmx.de
Thu Nov 3 23:52:49 CET 2011


On 03.11.11 17:10:00, Yifei Li wrote:
> 
> The following is a minimal example that shows my problem.  TreeWidget does not show up in the dock widget.
> 
> #include <QtGui/QApplication>
> #include <QMainWindow>
> #include <QDockWidget>
> #include <QVBoxLayout>
> #include <QTreeWidget>
> #include <QTextEdit>
> 
> class MainWindow : public QMainWindow {
> public:
>     MainWindow(QWidget* parent = 0)
>         :QMainWindow(parent)
>     {
>         QTextEdit* edit = new QTextEdit();
>         this->setCentralWidget(edit);
>         QDockWidget * dock = new QDockWidget(this);
>         QVBoxLayout* layout = new QVBoxLayout();
>         QTreeWidget* tree = new QTreeWidget();
>         layout->addWidget(tree);
>         dock->setLayout(layout);

This is your problem. You're not supposed to set a layout on a
dockwidget since QDockWidget will have its own layout (probably set when
using addDockWidget, but didn't find the place right away in the
sources). This is so it can have a maximize/minimize button etc. when
necessary. Thats the same situation as QMainWindow and its central
widget, so simply use the setWidget function and supply an intermediate
QWidget on which you set the layout:

        QWidget* tmpwidget = new QWidget();
        tmpwidget->setLayout(layout);
        dock->setwidget(tmpwidget);

You should also have gotten a warning on stderr (or Windows debug
channel) when running your example that a layout is being set on
QDockWidget while it already has one. This also hints at the fact that
Qt internally sets a layout on the dockwidget when adding it to a
mainwindow.

Andreas




More information about the Qt-interest-old mailing list