Results 1 to 3 of 3

Thread: Set initial size of widget (not min or max)

  1. #1
    Join Date
    Apr 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Set initial size of widget (not min or max)

    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:

    Qt Code:
    1. void SortWindow::populate(int** d, int s, QString* n[]) {
    2. size = s;
    3. for (int i=0; i<6; i++) {
    4. name[i]->setText(" "+QString::number(i+1)+": "+*n[i]);
    5. window[i] = new ArrayWindow(this, d[i], s, s+2, s+2);
    6. // window[i]->updateGeometry();
    7. // window[i]->resize(s+2,s+2);
    8. // window[i]->setGeometry(0,0,s+2,s+2);
    9. border[i]->addWidget(window[i],0,0);
    10. }
    11. // borderPanel->invalidate();
    12. }
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. public:
    2. virtual QSize sizeHint() const;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QSize ArrayWindow::sizeHint() const {
    2. return QSize(width, height);
    3. }
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by Galen; 15th April 2010 at 06:22.

  2. #2
    Join Date
    Apr 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set initial size of widget (not min or max)

    Huh, got somewhere.

    Qt Code:
    1. void SortWindow::populate(int** d, int s, QString* n[]) {
    2. size = s;
    3.  
    4. for (int i=0; i<6; i++) {
    5. name[i]->setText(" "+QString::number(i+1)+": "+*n[i]);
    6. window[i] = new ArrayWindow(this, d[i], s, s+2, s+2);
    7. border[i]->addWidget(window[i],0,0); }
    8.  
    9. adjustSize();
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    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).

    Qt Code:
    1. QDesktopWidget* desktop = QApplication::desktop();
    2. int screen = desktop->screenNumber();
    3. QRect srect = desktop->availableGeometry(screen);
    4. // setMaximumSize(srect.width(), srect.height());
    To copy to clipboard, switch view to plain text mode 
    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

  3. #3
    Join Date
    Apr 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set initial size of widget (not min or max)

    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).

Similar Threads

  1. initial size of widgets in a QSplitter
    By momesana in forum Newbie
    Replies: 3
    Last Post: 12th July 2012, 15:41
  2. Replies: 4
    Last Post: 11th July 2010, 09:47
  3. Adjust the initial size of a QTextEdit in a layout
    By hecinho in forum Qt Programming
    Replies: 0
    Last Post: 26th November 2009, 15:58
  4. QGraphicsView wrong initial size of objects
    By syrtyr in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2009, 07:13
  5. How to set initial QDockWidget's size?
    By rajesh in forum Qt Programming
    Replies: 4
    Last Post: 16th November 2006, 15:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.