Results 1 to 12 of 12

Thread: Centre the form when maximised

  1. #1
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Centre the form when maximised

    Hi,
    I have a form that is roughly half the width of the screen. By default, When the exe screen is maximised the form gets aligned to the left side of the screen. What i want to do is to make it appear in the centre Untitled.jpg How can i do that? Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Centre the form when maximised

    Use a layout, because at the moment it doesn't look like your main window has a layout applied.

  3. The following user says thank you to ChrisW67 for this useful post:

    premroxx (13th September 2011)

  4. #3
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Centre the form when maximised

    What you see is just a begin screen. I have lots of labels, texboxes & comboxes etc in the form. To add a layout at this point might a bit tedious. Is there anyway i can change it through coding.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Centre the form when maximised

    Sure, you can capture the QWidget::resizeEvent() of the main window, manually recalculate the size and location of each widget, and manually set their geometry. This is generally considered tedious, which is why layout engines exist.

    How have you built the form? Designer or in code?

  6. #5
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Centre the form when maximised

    I have built the form using designer. I have a series of labels & textboxes & images on top of each other. I display them based on conditions. Because the images and the widgets are on top of each other its kinda difficult to use layout as it might mess up the whole thing or is there an better/correct way of using layout?

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Centre the form when maximised

    You should look at QStackedWidget as a way to handle several layers of widgets that you can switch between.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Centre the form when maximised

    Have a look at this article: [wiki]The Panel Stack Pattern[/wiki]
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    May 2011
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Centre the form when maximised

    This may look crazy, but it works. Consider this code in the constructor of the form:

    Qt Code:
    1. int newX = QApplication::desktop ()->width () / 2;
    2.  
    3. newX -= this->width () / 2;
    4.  
    5. this->setGeometry (newX, 0, this->width (), this->height ());
    To copy to clipboard, switch view to plain text mode 


    In Line 1: QApplication::desktop ()->width () gets the maximum width of the current monitor where the app is

    Do not forget to #include <QDesktopWidget> and #include <QApplication> .

  10. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Centre the form when maximised

    Sure, this will place "this" in the middle of the desktop.

    That, however, is not what I understand premroxx was asking for. Premroxx has maximised "this" and the numerous, absolutely placed contents of "this" are not being placed as expected. The problem would also manifest if premroxx resized the window without maximising it. Premroxx has also tried to mimic a variable interface by overlaying widgets and manually controlling their individual visibility; a very VB6 way of going about the whole exercise.

  11. #10
    Join Date
    May 2011
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Centre the form when maximised

    Silly me . In this case, I think he wants what a QGridLayout does.

  12. #11
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Centre the form when maximised

    I tried the form layout. In the 'Object inspector' i right click the 'MainWindow' then layout->layout in a form layout. I dragged the red margin, so that it covers the form. Then in the layoutformalignment i changed to alignHcentre,AlignVcentre. Still it seems to aligned towards left side. What am i doing wrong here?

  13. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Centre the form when maximised

    QFormLayout is designed to arrange labels and associated data entry widgets in two columns with convenience features related to setting widget buddies. You probably have you content widget in the left column. I would suggest that you look at a QHBoxLayout with the various alignment values.

Similar Threads

  1. How to hide from taskbar a form showed form mainwindow
    By tonnot in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2011, 14:36
  2. Pass variable from a form to another form
    By dark1988 in forum Qt Programming
    Replies: 5
    Last Post: 8th February 2011, 18:19
  3. Replies: 5
    Last Post: 12th March 2010, 21:43
  4. Help me to load one form over another form PushButton
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 26th November 2008, 16:11
  5. Calling a new form from current form
    By webgenius in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2007, 19:50

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.