PDA

View Full Version : Positioning Child Windows on a QMainWindow



GTBuilder
12th February 2012, 14:48
I've created a QMainWindow upon which I want to be able to place 1 or 2 QDialog based child windows from a choice of 5. All the child windows are the same size and the workspace is slightly deeper than a child window and slightly wider than a pair of child windows.

When I activate the first child window, it appears in the upper left of the workspace. If I activate a second child window it appear next to the first. However, if I close the first child, the second still appears as if the first window was still there.

Is there any setting in QWorkspace, (or in the destructor of the children) that will always place a child in the left most position if the workspace is empty? The workspace seems to have memory of what happened previously.

ChrisW67
13th February 2012, 00:42
Firstly, you should be using QMdiArea not QWorkspace, which as the friendly docs say:


The QWorkspace widget provides a workspace window that can be used in an MDI application.

This class is deprecated. Use QMdiArea instead.


MDI child windows stay where the user places them (or leaves them in this case) just as the user would expect. Windows jumping around without user command is bad karma in my book. You could, of course, move the window yourself in response to some iAmClosing() signal you arrange.

GTBuilder
14th February 2012, 03:52
Thanks Chris. This looks like the right way to go. However, my implementation has some serious faults with it. Code is:

void MainWindow::newQC()
{
QC *qch = createQC();

qch->showNormal();
}

QC *MainWindow::createQC()
{
QC *qc = new QC;

mdiarea->addSubWindow( qc);
qc->setFixedSize( 470,400);

return qc;
}

When I initiate the subwindow with a menubar command, I get a minimized subwindow. Maximizing displays the subwindow filling the entire mdiarea. Clicking the window size button reduces the subwindow size to the 470X400 that was set in QtDesigner. However, the base color of the QDialog subwindow is the default color, not that set by QtDesigner. However, buttons, textEdits and LineEdits display correctly. I'm missing something important.

ChrisW67
14th February 2012, 07:49
Thanks Chris. This looks like the right way to go. However, my implementation has some serious faults with it. Code is:


void MainWindow::newQC()
{
QC *qch = createQC();

qch->showNormal();
}


As mentioned in the QWidget::showNormal() docs the function only affects windows, i.e. unparented widgets. Your QC object is a child of the QMdiSubWindow. I see no need to call this, it will be shown by default when added.



When I initiate the subwindow with a menubar command, I get a minimized subwindow.
No code, no ideas.

Maximizing displays the subwindow filling the entire mdiarea.
Correct operation.

Clicking the window size button reduces the subwindow size to the 470X400 that was set in QtDesigner.
You set that size in code not Designer. Seems reasonable enough that it would restore to its nominal size.


However, the base color of the QDialog subwindow is the default color, not that set by QtDesigner. However, buttons, textEdits and LineEdits display correctly. I'm missing something important.
I have no idea what a "QDialog subwindow" is. If you mean the "empty" area of the QMdiArea then you should be able to simply call setBackground() with the relevant brush (it doesn't use a style sheet).

GTBuilder
14th February 2012, 14:34
OK, let's go back to the beginning. I created a QC.ui file using QtDesigner to specify a QDialog widget with some pushbuttons, textEdits, etc. I then created the main.cpp, QC.cpp and QC.h files to control the widget that appears on the Windows desktop. I then created 4 additional programs all based upon the some QC.ui file. As my overall application only requires that 2 widgets appear at any one time I created an overall QMainWindow to manage the QDialog widgets, displaying them in a QWorkspace on the QMainWindow. The code:

void MainWindow::newQC()
{
QC *qch = createQC();
qch->show();
}

QC *MainWindow::createQC()
{
QC *qc = new QC;

workspace->addWindow( qc);

return qc;
}


where newQC() is a slot activated by a menubar QAction.

Etc for the other 4 modules.


The QC object appears in the workspace exactly as it appears on the desktop. I don't have to do anything in the QMainWindow code to contol it's size or color. The only issue, as discussed in my original post is that I can't control where the widgets appear in the workspace. If QmdiArea can't do likewise, then QWorkspace may not be deprecated at all.