QDialog::setGeometry() on Windows 10 / Linux
-
I have a QDialog with a single QComboBox which is displayed after selection of a popup menu option. It should look like a simple combo box, but with a window title bar, which is why I chose to embed it in a dialog. The dialog's
accept()slot is connected to the combo boxcurrentIndexChanged()signal, and the dialog also has the window attributeWA_DeleteOnClose.On Linux and Windows, I create the dialog as follows:
void DlgGraphView::onProfessionSearch(bool) { const QPoint pt = QCursor::pos(); const QString title = tr("Find Profession:"); DlgFramedComboBox *pfcb = new DlgFramedComboBox(title, true, this); if (pfcb) { pfcb->setAttribute(Qt::WA_DeleteOnClose); // auto-delete after selection const QRect cr = QRect(pt, pfcb->minimumSize()); // get minimum rectangle initBerufeComboBox(pfcb); // fill in the items pfcb->setGeometry(cr); // should eliminate extra space around combobox pfcb->show(); pfcb->setFocus(); } }On Linux, this works as desired (see image below). However, on Windows, this opens much too large vertically. The dialog is made in Qt Designer and has the sizeHint property
[Horizontal:]Preferred - [Vertical:]Fixed. The combo is contained in a grid layout of the dialog. I can manually resize the dialog once it is shown, but it shouldn't be necessary.Screenshot of the way this opens on Windows:

Screenshot of the desired size:

How can I write the code so that the behavior is the same on Linux as on Windows?
Linux: Ubuntu 18.04 LTS running default window manager (Gtk) with Qt 5.15.5 built from source;
Windows: Windows 10, Qt 5.12.9 installed from binary offline installer.
-
I have a QDialog with a single QComboBox which is displayed after selection of a popup menu option. It should look like a simple combo box, but with a window title bar, which is why I chose to embed it in a dialog. The dialog's
accept()slot is connected to the combo boxcurrentIndexChanged()signal, and the dialog also has the window attributeWA_DeleteOnClose.On Linux and Windows, I create the dialog as follows:
void DlgGraphView::onProfessionSearch(bool) { const QPoint pt = QCursor::pos(); const QString title = tr("Find Profession:"); DlgFramedComboBox *pfcb = new DlgFramedComboBox(title, true, this); if (pfcb) { pfcb->setAttribute(Qt::WA_DeleteOnClose); // auto-delete after selection const QRect cr = QRect(pt, pfcb->minimumSize()); // get minimum rectangle initBerufeComboBox(pfcb); // fill in the items pfcb->setGeometry(cr); // should eliminate extra space around combobox pfcb->show(); pfcb->setFocus(); } }On Linux, this works as desired (see image below). However, on Windows, this opens much too large vertically. The dialog is made in Qt Designer and has the sizeHint property
[Horizontal:]Preferred - [Vertical:]Fixed. The combo is contained in a grid layout of the dialog. I can manually resize the dialog once it is shown, but it shouldn't be necessary.Screenshot of the way this opens on Windows:

Screenshot of the desired size:

How can I write the code so that the behavior is the same on Linux as on Windows?
Linux: Ubuntu 18.04 LTS running default window manager (Gtk) with Qt 5.15.5 built from source;
Windows: Windows 10, Qt 5.12.9 installed from binary offline installer.
@Robert-Hairgrove Here is the code for the class definition of
DlgFramedComboBoxas well as the implementation:DlgFramedComboBox.hpp:
#ifndef DLGFRAMEDCOMBOBOX_H #define DLGFRAMEDCOMBOBOX_H #include "SbblGlobal.hpp" #include "wrapQtWidgets.h" namespace Ui { class FramedComboBox; } namespace sbbl::gui::dialogs { class DlgGraphView; class DlgFramedComboBox : public QDialog { Q_OBJECT public: explicit DlgFramedComboBox(const QString &windowtitle, bool prof_search, QWidget *parent = nullptr); ~DlgFramedComboBox(); void addItem(const QString &itemtext, const QVariant &value); bool isProfSearch() const { return is_prof_search_; } bool isStudentSearch() const { return !is_prof_search_; } public slots: void on_comboBox_currentIndexChanged(int idx); protected: void changeEvent(QEvent *e); signals: void notifyProfSelected(int bnr); void notifySuSSelected(int bnr); private: Ui::FramedComboBox *ui; DlgGraphView *p_; const bool is_prof_search_; }; } // namespace sbbl::gui::dialogs #endif // DLGFRAMEDCOMBOBOX_HDlgFramedComboBox.cpp:
#include "gui/dialogs/DlgFramedComboBox.hpp" #include "gui/dialogs/DlgGraphView.hpp" #include "ui_FramedComboBox.h" namespace sbbl::gui::dialogs { static bool SLOTS_ACTIVE = false; DlgFramedComboBox::DlgFramedComboBox( const QString &windowtitle, bool prof_search, QWidget *parent) : QDialog(parent) , ui(new Ui::FramedComboBox) , p_ (qobject_cast<dialogs::DlgGraphView*>(parent)) , is_prof_search_(prof_search) { using namespace dialogs; SLOTS_ACTIVE = false; ui->setupUi(this); setWindowTitle(windowtitle); setFocusPolicy(Qt::StrongFocus); if (p_) { if (prof_search) { connect(this, &DlgFramedComboBox::notifyProfSelected, p_, &DlgGraphView::gotProfSearch); } else { connect(this, &DlgFramedComboBox::notifySuSSelected, p_, &DlgGraphView::gotSuSSearch); } } SLOTS_ACTIVE = true; } DlgFramedComboBox::~DlgFramedComboBox() { delete ui; } void DlgFramedComboBox::addItem(const QString &itemtext, const QVariant &value) { SLOTS_ACTIVE = false; ui->comboBox->addItem(itemtext, value); SLOTS_ACTIVE = true; } void DlgFramedComboBox::on_comboBox_currentIndexChanged(int idx) { if (!SLOTS_ACTIVE) return; int selected = -1; if (idx >= 0) { int the_data = ui->comboBox->currentData().toInt(); if (the_data) { selected = the_data; } } if (is_prof_search_) { emit notifyProfSelected(selected); } else { emit notifySuSSelected(selected); } //--------------------------------------------- // This will automatically delete the object // since the caller sets the 'WA_DeleteOnClose' // window attribute when it is created: //--------------------------------------------- accept(); } void DlgFramedComboBox::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } } // namespace sbbl::gui::dialogs -
I have a QDialog with a single QComboBox which is displayed after selection of a popup menu option. It should look like a simple combo box, but with a window title bar, which is why I chose to embed it in a dialog. The dialog's
accept()slot is connected to the combo boxcurrentIndexChanged()signal, and the dialog also has the window attributeWA_DeleteOnClose.On Linux and Windows, I create the dialog as follows:
void DlgGraphView::onProfessionSearch(bool) { const QPoint pt = QCursor::pos(); const QString title = tr("Find Profession:"); DlgFramedComboBox *pfcb = new DlgFramedComboBox(title, true, this); if (pfcb) { pfcb->setAttribute(Qt::WA_DeleteOnClose); // auto-delete after selection const QRect cr = QRect(pt, pfcb->minimumSize()); // get minimum rectangle initBerufeComboBox(pfcb); // fill in the items pfcb->setGeometry(cr); // should eliminate extra space around combobox pfcb->show(); pfcb->setFocus(); } }On Linux, this works as desired (see image below). However, on Windows, this opens much too large vertically. The dialog is made in Qt Designer and has the sizeHint property
[Horizontal:]Preferred - [Vertical:]Fixed. The combo is contained in a grid layout of the dialog. I can manually resize the dialog once it is shown, but it shouldn't be necessary.Screenshot of the way this opens on Windows:

Screenshot of the desired size:

How can I write the code so that the behavior is the same on Linux as on Windows?
Linux: Ubuntu 18.04 LTS running default window manager (Gtk) with Qt 5.15.5 built from source;
Windows: Windows 10, Qt 5.12.9 installed from binary offline installer.
@Robert-Hairgrove said in QDialog::setGeometry() on Windows 10 / Linux:
The combo is contained in a grid layout of the dialog
Why a grid if you dont plan to add more to that dialog?
const QRect cr = QRect(pt, pfcb->minimumSize()); // get minimum rectangle
Try something like (if you have the correct size set in QtDesigner)
pfcb->setFixedHeight(pfcb->sizeHint().height()) -
@Robert-Hairgrove Here is the code for the class definition of
DlgFramedComboBoxas well as the implementation:DlgFramedComboBox.hpp:
#ifndef DLGFRAMEDCOMBOBOX_H #define DLGFRAMEDCOMBOBOX_H #include "SbblGlobal.hpp" #include "wrapQtWidgets.h" namespace Ui { class FramedComboBox; } namespace sbbl::gui::dialogs { class DlgGraphView; class DlgFramedComboBox : public QDialog { Q_OBJECT public: explicit DlgFramedComboBox(const QString &windowtitle, bool prof_search, QWidget *parent = nullptr); ~DlgFramedComboBox(); void addItem(const QString &itemtext, const QVariant &value); bool isProfSearch() const { return is_prof_search_; } bool isStudentSearch() const { return !is_prof_search_; } public slots: void on_comboBox_currentIndexChanged(int idx); protected: void changeEvent(QEvent *e); signals: void notifyProfSelected(int bnr); void notifySuSSelected(int bnr); private: Ui::FramedComboBox *ui; DlgGraphView *p_; const bool is_prof_search_; }; } // namespace sbbl::gui::dialogs #endif // DLGFRAMEDCOMBOBOX_HDlgFramedComboBox.cpp:
#include "gui/dialogs/DlgFramedComboBox.hpp" #include "gui/dialogs/DlgGraphView.hpp" #include "ui_FramedComboBox.h" namespace sbbl::gui::dialogs { static bool SLOTS_ACTIVE = false; DlgFramedComboBox::DlgFramedComboBox( const QString &windowtitle, bool prof_search, QWidget *parent) : QDialog(parent) , ui(new Ui::FramedComboBox) , p_ (qobject_cast<dialogs::DlgGraphView*>(parent)) , is_prof_search_(prof_search) { using namespace dialogs; SLOTS_ACTIVE = false; ui->setupUi(this); setWindowTitle(windowtitle); setFocusPolicy(Qt::StrongFocus); if (p_) { if (prof_search) { connect(this, &DlgFramedComboBox::notifyProfSelected, p_, &DlgGraphView::gotProfSearch); } else { connect(this, &DlgFramedComboBox::notifySuSSelected, p_, &DlgGraphView::gotSuSSearch); } } SLOTS_ACTIVE = true; } DlgFramedComboBox::~DlgFramedComboBox() { delete ui; } void DlgFramedComboBox::addItem(const QString &itemtext, const QVariant &value) { SLOTS_ACTIVE = false; ui->comboBox->addItem(itemtext, value); SLOTS_ACTIVE = true; } void DlgFramedComboBox::on_comboBox_currentIndexChanged(int idx) { if (!SLOTS_ACTIVE) return; int selected = -1; if (idx >= 0) { int the_data = ui->comboBox->currentData().toInt(); if (the_data) { selected = the_data; } } if (is_prof_search_) { emit notifyProfSelected(selected); } else { emit notifySuSSelected(selected); } //--------------------------------------------- // This will automatically delete the object // since the caller sets the 'WA_DeleteOnClose' // window attribute when it is created: //--------------------------------------------- accept(); } void DlgFramedComboBox::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } } // namespace sbbl::gui::dialogs@Robert-Hairgrove
Set the size after calling show()pfcb->show(); pfcb->setFixedSize(pcfb->size()); -
@Robert-Hairgrove said in QDialog::setGeometry() on Windows 10 / Linux:
The combo is contained in a grid layout of the dialog
Why a grid if you dont plan to add more to that dialog?
const QRect cr = QRect(pt, pfcb->minimumSize()); // get minimum rectangle
Try something like (if you have the correct size set in QtDesigner)
pfcb->setFixedHeight(pfcb->sizeHint().height()) -
R Robert Hairgrove has marked this topic as solved on