PDA

View Full Version : On resize using Maximize Button, Dialog not appears in center of screen?



merry
22nd August 2011, 11:35
Hi all,

I am working on Qt4.6 on mac os, I am working on MultiDialog Application. All the windows are in the layout, When I click on the maximize button on the top of the mainwindow appaers to be maximized. When i move from MainWindow to the next Dialog I want if MainWiindow is maximised then nextDialog should also be maximized.For this I used


if(this->isMaximized())
{
nextDialog->showMaximized();
}

It works perfectly fine. But When I click on nextDialog maximize button to restore it to its orginalGeometry, then it resizes it to the original width and height but not the position, I appears on the bottom right, for this I am trying to get the center position in the resize event of the dialog but still no use.

For this I used:


void nextDialog::resizeEvent(QResizeEvent * event)
{
this->move(QApplication::desktop()->screen()->rect().center()- this->rect().center());
}

But It wont appear in the center

I also tried this but again fails:


void nextDialog::resizeEvent(QResizeEvent * event)
{
QDesktopWidget *d = QApplication::desktop();
int dW = d->width();
int dH = d->height();
int mw = this->width();
int mH = this->height();
int cw = dW/2-mw/2;
int ch = dH/2-mH/2;
move(cw,ch);
}

Pls Suggest what should i do.

high_flyer
22nd August 2011, 12:49
try using saveGeometry() and restoreGeometry().

merry
22nd August 2011, 13:22
No, its also not working.

high_flyer
22nd August 2011, 13:39
show your code with what you tried.

merry
23rd August 2011, 05:33
I saved the original geometry that appears when the MainWindow appears.


Void MainWindow::init()
{
QSettings settings("MyCompany", "MyApp");
settings.setValue("geometry", saveGeometry());
}


And on Next Dialog, On Resize Event, I restored that saved geometry.

void NextDialog::resizeEvent(QResizeEvent * event)
{
QSettings settings("MyCompany", "MyApp");
this->restoreGeometry(settings.value("geometry").toByteArray());
}


Actually My Dialog is in the layout So, when I restore its orginal geometry then it restores the widget and On Maximize Button it expands without expanding the widgets inside the dialog.

high_flyer
23rd August 2011, 10:23
Your code makes no sense.
You are storing the geometry of your main window, and give that geometry to the dialog??
You should restored the geometry for your main window!



Actually My Dialog is in the layout
How can that be? Dialog is a top level widget.

merry
23rd August 2011, 12:10
Attached is the sample code for reference

Unzip the code and run , There is one case in which i am getting problem:
The case is:


On Application Run, First click on the maximize Button on the top, after that On the "OK Button" -> On Click on OkButton -> it will show the next dialog, then click on the maximize Button on the top of this next dialog, It will not come in the centre of the screen.

I had also, try to use saveGeometry() and restoreGeometry(). It is commented in the code, Uncomment it and try to run again, and see the result,

Pls help me, what i am doing wrong?

high_flyer
23rd August 2011, 12:51
I can't run your code where I am at.
Maybe some one else can.
I'll try looking at it in the evening if I have time.

merry
25th August 2011, 11:11
Nobody is there to solve my problem.

Its very urgent, Pls help.

high_flyer
25th August 2011, 11:46
QDlg::QDlg(QWidget *parent):QDialog(NULL)
{
setupUi(this);
OnInit();
}

you are saving the geometry in OnInit() but you call OnInit() during construction time, during that time the geometry is not defined.
You have to save the geometry after the dialog is visible, best as a reaction to resizing or moving.

merry
26th August 2011, 05:53
Thanks a ton high_flyer, its working.