PDA

View Full Version : Set initial size of widget (not min or max)



Galen
15th April 2010, 05:59
I have a graphical application whose main window extends qwidget. Here's how it works:


borderPanel is a 2x1 GridPanel. Top row contains buttons / sliders / labels and only expands horizontally. Bottom row contains everything else and expands in both directions.
gridPanel is a 2x3 GridPanel. These expand in either direction. This panel goes in the bottom row of borderlPanel.
border is a 2x1 GridPanel. The bottom row only expands horizontally and contains 3 QLabels. The top row expands in both directions and contains everything else. One borderPanel goes in each grid of gridPanel.
window is an ArrayWindow extends QWidget and draws an array. One of each goes in the top row of each border.

All the panels are created and placed in the constructor except the ArrayWindows. This is handled by this method:


void SortWindow::populate(int** d, int s, QString* n[]) {
size = s;
for (int i=0; i<6; i++) {
name[i]->setText(" "+QString::number(i+1)+": "+*n[i]);
window[i] = new ArrayWindow(this, d[i], s, s+2, s+2);
// window[i]->updateGeometry();
// window[i]->resize(s+2,s+2);
// window[i]->setGeometry(0,0,s+2,s+2);
border[i]->addWidget(window[i],0,0);
}
// borderPanel->invalidate();
}
d is an array of int arrays, s is the length of each array, n is an array of names for name QLabels in the borderPanels. Commented out are some attempts I made that didn't work. In all cases, the size was based on the width the QLabels in the bottom row of the borderPanels and is way too small with s=300. In ArrayWindow I also reimplemented sizehint as so:


public:
virtual QSize sizeHint() const;

QSize ArrayWindow::sizeHint() const {
return QSize(width, height);
}
What did work was setMinimumSize, but the problem is this application supports resizing the screen. The user resizes the window, which recenters the content and enables a button, you click the button and new arrays are generated that best fit the new window size. This works for both increasing and decreasing the window size, so locking it to some minimum is undesirable.

thanks

Galen
16th April 2010, 22:51
Huh, got somewhere.


void SortWindow::populate(int** d, int s, QString* n[]) {
size = s;

for (int i=0; i<6; i++) {
name[i]->setText(" "+QString::number(i+1)+": "+*n[i]);
window[i] = new ArrayWindow(this, d[i], s, s+2, s+2);
border[i]->addWidget(window[i],0,0); }

adjustSize();

}

Looks like adjustSize is the magic command and it checks the sizehints. Only problem is that it only works up to a certain size. When arrayWindows are 202x202, no problem. But with larger sizes, they always max out at 285x315 no matter what I set it to. My screen is 1280x1024 and these sizes shoiuld not be a problem (3x arrayWindows comprise the entire width, and they are touching).


QDesktopWidget* desktop = QApplication::desktop();
int screen = desktop->screenNumber();
QRect srect = desktop->availableGeometry(screen);
// setMaximumSize(srect.width(), srect.height());
I did some console debug statements and verified it is detecting the screen size correctly. The commented out line was an attempt to fix the issue but it didn't work. Any ideas?

thanks

Galen
16th April 2010, 23:55
Oh, apparently adjustSize() maxes out at 2/3 the screen size. You have to figure out what the width and height should be and then call resize(w,h) instead (on the main widget).