Results 1 to 3 of 3

Thread: set a QDialog's initial location

  1. #1
    Join Date
    Feb 2018
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default set a QDialog's initial location

    Hi,
    I created a help dialog as a non-modal QDialog that comes up when a button is pressed from our main application. We want it to display just to the right of the application's dialog. However whenever I run it it comes up at the top of the monitor just to the right of center (the application is in the center of the monitor both horizontally and vertically). I have been trying several ways of using "move" but I haven't got it to work. I would appreciate help in the proper way to place it to the right of it's parent. Thanks

    in the helpDialog.h:
    Qt Code:
    1. class HelpDialog :public QDialog {
    2. (removed the rest)
    3. }
    To copy to clipboard, switch view to plain text mode 

    in helpDialog.cpp
    Qt Code:
    1. HelpDialog::HelpDialog(QWidget *const theParent)
    2. : QDialog(theParent),
    3. ui(new Ui::HelpDialog)
    4. {
    5. ui->setupUi(this);
    6.  
    7. // display the dialog on the right of the current screen.
    8. if (nullptr != MyParent)
    9. {
    10. move( MyParent->parentWidget()->window()->rect().topRight() ); //NOTE: one of various values we've tried here
    11. setWindowFlags(Qt::Window); //ensure the minimize button on the QDialog
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    in code for main application window to bring this up:
    in class init() function
    Qt Code:
    1. myHelp = new HelpDialog(this);
    2. //lamba to handle pressing of help button
    3. const auto ToggleHelp = [this]()
    4. {
    5. const QString HelpText = tr("Some help text goes here");
    6.  
    7. if (isVisible())
    8. {
    9. myHelpDialog->setMessage(HelpText);
    10. myHelpDialog->show(); //use show for nonmodal dialogs
    11. }
    12. };
    13. ( void )connect(footerWidget, &FooterWidget::helpButtonClicked, ToggleHelp);
    14. myHelpDialog->hide();
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 12th February 2018 at 22:48.
    newbie

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: set a QDialog's initial location

    I'd try the following:
    0. Remove the moving code from the constructor.
    1. Create the dialog hidden.
    2. Call setGeometry() on it from the parent.
    3. show it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2018
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: set a QDialog's initial location

    Thank you - this is the final solution that worked for me

    helpDialog.h - same as above

    in helpDialog.cpp:
    Qt Code:
    1. HelpDialog::HelpDialog(QWidget *const theParent)
    2. : QDialog(theParent)
    3. , ui(new Ui::HelpDialog)
    4. {
    5. ui->setupUi(this);
    6.  
    7. // display the dialog on the right of the current screen.
    8. if (nullptr != theParent)
    9. {
    10. setWindowFlags(Qt::Window); //ensure the minimize button on the QDialog
    11. }
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    in code for main application window to bring this up:
    in class init() function:
    Qt Code:
    1. myHelpDialog = new HelpDialog(this);
    2. //lamba to handle pressing of help button
    3. const auto ToggleHelp = [this]()
    4. {
    5. const QString HelpText = tr("Some help text goes here");
    6.  
    7. if (isVisible())
    8. {
    9. myHelpDialog ->setMessage(HelpText);
    10.  
    11. //place help dialog "top right" corner just inside the top right corner of parent window
    12. QRect rect = myHelpDialog->geometry(); //get current geometry of help window
    13. QRect parentRect = this->geometry(); //get current geometry of this window
    14. rect.moveTo(mapToGlobal(QPoint(parentRect.x() + parentRect.width() - rect.width(), parentRect.y())));
    15.  
    16. myHelpDialog ->setGeometry(rect);
    17.  
    18. myHelpDialog ->show(); //use show for nonmodal dialogs
    19. }
    20. };
    21. ( void )connect(m_footerWidget, &FooterWidget::helpButtonClicked, ToggleHelp);
    22. myHelpDialog ->hide();
    To copy to clipboard, switch view to plain text mode 
    newbie

Similar Threads

  1. initial comboboxes at Qt using Ui
    By citix in forum Newbie
    Replies: 1
    Last Post: 25th November 2013, 19:33
  2. Replies: 9
    Last Post: 25th March 2011, 22:22
  3. QDialog.exec() exiting without calling QDialog::accept()
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2011, 12:35
  4. Replies: 2
    Last Post: 17th April 2010, 00:55
  5. How to popup QDialog at specific location?
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 23rd January 2009, 12:46

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.