Results 1 to 3 of 3

Thread: QDialog not centering when showEvent() implemented

  1. #1
    Join Date
    Aug 2011
    Location
    Finland
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QDialog not centering when showEvent() implemented

    I have several dialogs subclassed from QDialog and all are created and shown the same way:
    SomeDialog dlg(this);
    dlg.exec();

    However, only one of the dialogs behaved in the way I expected based on the reference: "A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself)." The others appeared in more or less random locations. After some effort I found out the proximate cause: the randomly appearing ones overrode QDialog::showEvent(QShowEvent* event) while the properly appearing dialog did not.

    I worked around this by deleting the showEvents from the offending classes, but supposing that I do need to do something in that function, how do I replicate the desired default behaviour regarding positioning?

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QDialog not centering when showEvent() implemented

    I suppose there is some mistake in the overridden method.

    Do it this way:
    Qt Code:
    1. class MyDialog: public QDialog
    2. {
    3. protected:
    4. void showEvent(QShowEvent *e)
    5. {
    6. // some custom code
    7.  
    8. // call inherited method
    9. QDialog::showEvent(e);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    CoderMan (24th February 2012)

  4. #3
    Join Date
    Aug 2011
    Location
    Finland
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDialog not centering when showEvent() implemented

    Ah! Merely calling accept() for the event is not enough: you have to manually call QDialog's showEvent.

Similar Threads

  1. My showevent does not work properly.
    By tonnot in forum Newbie
    Replies: 7
    Last Post: 18th February 2011, 07:18
  2. SIP stack implemented with Qt
    By iaguirre in forum Qt-based Software
    Replies: 0
    Last Post: 26th January 2011, 16:19
  3. Replies: 6
    Last Post: 9th November 2010, 09:58
  4. How to override ShowEvent() etc
    By marcvanriet in forum Newbie
    Replies: 1
    Last Post: 22nd September 2010, 10:13
  5. Close Dialog in showEvent
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 11th April 2008, 15:32

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.