3

I have QMdiArea object in QMainWindow which I did not have set with setCentralWidget() but as mentioned below.

MainWindow::createMdiArea() {
    QMdiArea mdiArea = new QMdiArea(this)
    mdiArea.setGeometry(0,0,1024,600);
    mdiArea.show(); 
}

I don't want to set this QMdiArea as the center widget. Now I am creating some child windows inside this QMdiArea, as following

MainWindow::createChildWindow()
{
     HelpWindow child1 = new HelpWindow(). // HelpWindow is a very simple class derived from QWidget and containing one QLineEdit. 
     mdiArea.addSubWindow(&child1);

     // Set the geometry of Child widget.
     child1.parentWidget()->setGeometry(0,0,400,100); // Setting geometry which I have saved.
     child1.show();
}

So far so good. But the problem is in the child window resize / close events I need to save the size of the HelpWindow and I am doing following to save the size of the child window in its resize / close events.

     saveHelpWindowSize(size());
     saveHelpWindowPos(pos());

Ok. Here comes the problem. The size I have got from the size() function inside the child window is different from the one which I have used in setGeometry() function, even I see it in some method other than resize event (i.e Close event).

So, Suppose first time the child window was created with the size (using setGeometry) 400, 200, when I go to close this window the size I will get from HelpWindow::size() might be say (385, 190) which I will save, and next time setGeometry() will get this size from settings, and at this time on close event I will get more smaller size, and eventually my child window will have size (0, 0).

1: Any thoughts on how to sync both these sizes (i.e., the size if the child widget being set by setGeometry() function and the size of the widget I am getting from size() function of the widget)

2: Another question, I am receiving resizeEvent / closeEvent events etc in the child widget (HelpWindow) class, but I am not getting moveEvent when I move the child window inside QMdiArea, the only time I receive this event is when I maximize the child window inside QMdiArea (which I won't be doing). So I need to save the position of my child window when its position is changed. How can I do it?

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Farrukh Arshad
  • 1,391
  • 2
  • 16
  • 26
  • Is this code compiling? First of all `QMdiArea mdiArea = new QMdiArea(this)` is wrong and even though you would say `mdiArea` is a pointer when it is getting out of scope you won't have access to the memory anymore(leak). These said, where is the declaration of `mdiArea` in `mdiArea.addSubWindow(&child1);`? I see a lot more issues with the code, please correct it, it is very confusing... – Jacob Krieg Oct 02 '14 at 19:44
  • is this solved? I have similar question. – bactone Nov 03 '21 at 03:02

0 Answers0