PDA

View Full Version : How to replace a QWidget in a QDockWidget? (Qt 4.2.0-rc1)



Anton
16th September 2006, 12:02
A document shows its properties in an associated QDockWidget. In a MDI-Application this QDockWidget should be reused from each document. So that the QDockWidget will allways contain the properties of the active document.

The properties of the document are stored in an XML-file. The XML-file is loaded into an object retrieved from class QTreeWidget using QDomDocument::setContent(QIODevice*, ...). Each document (MDI-Child) has now its own QTreeWidget populated with its properties. The parent of this QTreeWidges is the QDockWidget.

To set the QTreeWidget into the QDockWidget the methode setWidget(QWidget*) is used. This works fine when loading several files. The properties of the last loaded file is shown.

But when a previous file is activated (setWidget(...) is called with a QWidget which was previously set), the properties of the last loaded file are always shown on top of the QDockWidget. When enlarging the QDockWidgt the QTreeWidget on top is framed by small line and the QTreeWidget behind is resized as expected (s. screeshot).
584
Occures this behaviour in older versions of Qt too?
Is there a way to remove the previously set QWidget from an QDockWidget?
Should I try another way to find an adequate resolution?

Any help appreciated

yogeshm02
16th September 2006, 12:24
Reason behind such behaviour is that QDockWidget AFAIK is designed to handle one child at a time, and you are having many children.
My advice, if you want to retain your current method, is that you re-parent the old (unused) QTreeWidget while setting a new QTreeWidget to QDockWidget.

Anton
16th September 2006, 13:12
Dear yogeshm02

Many thanks to You! It's unbelievable, getting a working solution to my problem in such a short time :eek:

Following Your advice, I added three lines of code:


void MainWindow::replaceDockEquipment(ParameterTree* equipment)
{
ParameterTree* old = qobject_cast<ParameterTree*>(dockWdgtEqmt->widget());
if (equipment != old) {
if (old)
old->setParent(0); // in memorial yogeshm02 (Qt Center Forum)

equipment->setParent(dockWdgtEqmt);
dockWdgtEqmt->setWidget(equipment);
dockWdgtEqmt->update();
}
}

elcuco
16th September 2006, 14:17
another option might be using QStackedWidget in the dock widget and changing the current widgets as you move along and deleting widgets when the tab is closed.

jacek
16th September 2006, 14:24
Instead of creating dozens of widgets, you might consider using one QTreeView and several models.

Anton
16th September 2006, 15:09
Hi elcuco,
Hi jacek,

many thanks for Your advice.

I'm not only a beginner in this forum, but also to Qt . I havn't any experience with QStackedWidgets (up to now) :o I hope this will change in the near future.

A solution with one QTreeView and several models appears promising. I'll check them.