Results 1 to 7 of 7

Thread: Place QMessageBox on middle of screen

  1. #1
    Join Date
    Mar 2011
    Posts
    20
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Place QMessageBox on middle of screen

    hello every one,
    i'm meeting problem with QMessageBox ,
    i can't locate the box window to the middle of the screen,
    it's change position depend on the message box size,

    below is my code , but it not work after i need to change language
    till now, i'm doing the stupid thing is:
    1.Run the program first time, get the real messagebox:
    Qt Code:
    1. QMessageBox message(QMessageBox::Information, "",
    2. QObject::tr("Test QMessageBox Size"),
    3. QMessageBox::NoButton ,
    4. this, Qt::FramelessWindowHint);
    5.  
    6. message.exec();
    7.  
    8. QString sss ;
    9. sss.sprintf(" Test QMessageBox Size w=%d h=%d",message.width(),message.height());
    10. qDebug() << sss ;
    To copy to clipboard, switch view to plain text mode 

    2.After i get the message output "sss"
    "Test QMessageBox Size w=242 h=103"
    , setFixedSize(242, 103) of the message box
    and move the message on the middle of the screen before exec()
    Qt Code:
    1. QMessageBox message(QMessageBox::Information, "",
    2. QObject::tr("Test QMessageBox Size"),
    3. QMessageBox::NoButton ,
    4. this, Qt::FramelessWindowHint);
    5.  
    6. message.setFixedSize(242, 103);
    7. message.move((window_w-message.width())/2,(window_h-message.height())/2);
    8.  
    9. message.exec();
    To copy to clipboard, switch view to plain text mode 
    which window_w and window_h are the window width, height.


    it's worked well , but now i need to involve the multi-language using .qm file ,
    after translate to other language, the box size changed by the text ,
    (the length of text changed , and the box size changed)
    therefore the messagebox not on the middle of screen again,
    is there any way to let the messagebox always on the middle of screen no matter the MessageBox size?

    thanks for your patience ,
    any response is appreciate!

  2. #2
    Join Date
    Nov 2011
    Posts
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Place QMessageBox on middle of screen

    Try setting the parent to 0 and not moving the window at all.

    Qt Code:
    1. QMessageBox message(QMessageBox::Information, "",
    2. QObject::tr("Test QMessageBox Size"),
    3. QMessageBox::NoButton ,
    4. 0, Qt::FramelessWindowHint);
    5.  
    6. message.exec();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Posts
    20
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Place QMessageBox on middle of screen

    hi gouyoku,

    thanks for your reply ,
    but if the parent is 0, message box may show on random place? (can't control)
    i found the article here

    and after looking for hours , there's an idea through my head ,

    i'm corrected the code by adding the .show() function
    Qt Code:
    1. QMessageBox message(QMessageBox::Information, "",
    2. QObject::tr("Test QMessageBox Size"),
    3. QMessageBox::NoButton ,
    4. this, Qt::FramelessWindowHint);
    5.  
    6. // message.setFixedSize(242, 103);
    7. message.show();
    8. message.move((window_w-message.width())/2,(window_h-message.height())/2);
    9.  
    10. message.exec();
    To copy to clipboard, switch view to plain text mode 

    In this way , i can get the correct message box size
    and dynamically move to the center of screen, no matter what language .
    (there will be wrong size if your content(setText()) include both "\n" and another very long string(will automatically change line)

    but i'm not sure is it a good way to use .show() and .exec() at the same time ????
    can anyone answer me ?

  4. #4
    Join Date
    Nov 2011
    Posts
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Place QMessageBox on middle of screen

    It is not a random place. It is decided by the window manager and dialogs without parent windows tend to show up in the middle of the screen. If you have no faith in window managers you can do this:
    Qt Code:
    1. QMessageBox message(QMessageBox::Information, "",
    2. QObject::tr("Test QMessageBox Size"),
    3. QMessageBox::NoButton ,
    4. QDesktopWidget().screen(), Qt::FramelessWindowHint); // will show QMessageBox in the center of the default screen
    5. message.exec();
    To copy to clipboard, switch view to plain text mode 
    Can't give you a definite answer about show() and exec() one after another but I wouldn't do that. It makes the program go through show() twice.

  5. #5
    Join Date
    Mar 2011
    Posts
    20
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Place QMessageBox on middle of screen

    Thanks , I've try to set the parent (0 or QDesktopWidget().screen() )
    but it's not in the middle of screen , i don't know why ,
    it's change position depend on the content , anyway thank you~

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Place QMessageBox on middle of screen

    Remember that if you won't parent the dialog it will have an extra icon on the task bar - sometimes that's desired, but most of the times - not.

    as to the solution - the simplest is always the best:
    Qt Code:
    1. QMessageBox m( QMessageBox::Information, "test", "Put here whatever you want.", QMessageBox::NoButton, this );
    2.  
    3. QSize mSize = m.sizeHint(); // here's what you want, not m.width()/height()
    4. QRect screenRect = QDesktopWidget().screen()->rect();
    5. m.move( QPoint( screenRect.width()/2 - mSize.width()/2,
    6. screenRect.height()/2 - mSize.height()/2 ) );
    7.  
    8. m.exec();
    To copy to clipboard, switch view to plain text mode 
    You have to use sizeHint() to know the widgtet size if it hasn't been shown yet.

  7. The following user says thank you to Spitfire for this useful post:

    lllturtle (25th November 2011)

  8. #7
    Join Date
    Mar 2011
    Posts
    20
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Place QMessageBox on middle of screen

    Thank you Spitfire ,
    that's exactly what i want ,
    i've try that and it works perfectly~

Similar Threads

  1. QGridLayout: insert row in middle
    By ctgrund in forum Qt Programming
    Replies: 5
    Last Post: 31st May 2012, 16:29
  2. Replies: 1
    Last Post: 29th September 2011, 07:51
  3. QStyle : how to make the QMenu display in middle?
    By nish in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 27th November 2008, 10:39
  4. Replies: 2
    Last Post: 4th August 2008, 08:14
  5. Middle Button Mouse
    By jaime in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2006, 03:01

Tags for this Thread

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.