PDA

View Full Version : set a QDialog's initial location



tfort
12th February 2018, 18:38
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:

class HelpDialog :public QDialog {
(removed the rest)
}

in helpDialog.cpp

HelpDialog::HelpDialog(QWidget *const theParent)
: QDialog(theParent),
ui(new Ui::HelpDialog)
{
ui->setupUi(this);

// display the dialog on the right of the current screen.
if (nullptr != MyParent)
{
move( MyParent->parentWidget()->window()->rect().topRight() ); //NOTE: one of various values we've tried here
setWindowFlags(Qt::Window); //ensure the minimize button on the QDialog
}
}

in code for main application window to bring this up:
in class init() function

myHelp = new HelpDialog(this);
//lamba to handle pressing of help button
const auto ToggleHelp = [this]()
{
const QString HelpText = tr("Some help text goes here");

if (isVisible())
{
myHelpDialog->setMessage(HelpText);
myHelpDialog->show(); //use show for nonmodal dialogs
}
};
( void )connect(footerWidget, &FooterWidget::helpButtonClicked, ToggleHelp);
myHelpDialog->hide();

high_flyer
12th February 2018, 21:51
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.

tfort
15th February 2018, 21:24
Thank you - this is the final solution that worked for me

helpDialog.h - same as above

in helpDialog.cpp:

HelpDialog::HelpDialog(QWidget *const theParent)
: QDialog(theParent)
, ui(new Ui::HelpDialog)
{
ui->setupUi(this);

// display the dialog on the right of the current screen.
if (nullptr != theParent)
{
setWindowFlags(Qt::Window); //ensure the minimize button on the QDialog
}

}

in code for main application window to bring this up:
in class init() function:

myHelpDialog = new HelpDialog(this);
//lamba to handle pressing of help button
const auto ToggleHelp = [this]()
{
const QString HelpText = tr("Some help text goes here");

if (isVisible())
{
myHelpDialog ->setMessage(HelpText);

//place help dialog "top right" corner just inside the top right corner of parent window
QRect rect = myHelpDialog->geometry(); //get current geometry of help window
QRect parentRect = this->geometry(); //get current geometry of this window
rect.moveTo(mapToGlobal(QPoint(parentRect.x() + parentRect.width() - rect.width(), parentRect.y())));

myHelpDialog ->setGeometry(rect);

myHelpDialog ->show(); //use show for nonmodal dialogs
}
};
( void )connect(m_footerWidget, &FooterWidget::helpButtonClicked, ToggleHelp);
myHelpDialog ->hide();