Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDialog::setGeometry() on Windows 10 / Linux
Qt 6.11 is out! See what's new in the release blog

QDialog::setGeometry() on Windows 10 / Linux

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 653 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    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 box currentIndexChanged() signal, and the dialog also has the window attribute WA_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:
    combo_too_large.png

    Screenshot of the desired size:
    combo_desired_size.png

    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.

    R Pl45m4P 2 Replies Last reply
    0
    • R Robert Hairgrove

      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 box currentIndexChanged() signal, and the dialog also has the window attribute WA_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:
      combo_too_large.png

      Screenshot of the desired size:
      combo_desired_size.png

      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.

      R Offline
      R Offline
      Robert Hairgrove
      wrote on last edited by
      #2

      @Robert-Hairgrove Here is the code for the class definition of DlgFramedComboBox as 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_H
      

      DlgFramedComboBox.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
      
      M 1 Reply Last reply
      0
      • R Robert Hairgrove

        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 box currentIndexChanged() signal, and the dialog also has the window attribute WA_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:
        combo_too_large.png

        Screenshot of the desired size:
        combo_desired_size.png

        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.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @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())
        

        https://doc.qt.io/qt-6/qwidget.html#setFixedHeight


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        R 1 Reply Last reply
        1
        • R Robert Hairgrove

          @Robert-Hairgrove Here is the code for the class definition of DlgFramedComboBox as 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_H
          

          DlgFramedComboBox.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
          
          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @Robert-Hairgrove
          Set the size after calling show()

          pfcb->show();
          pfcb->setFixedSize(pcfb->size());
          
          1 Reply Last reply
          1
          • Pl45m4P Pl45m4

            @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())
            

            https://doc.qt.io/qt-6/qwidget.html#setFixedHeight

            R Offline
            R Offline
            Robert Hairgrove
            wrote on last edited by
            #5

            @Pl45m4 and @mpergand : Thanks!

            This was the solution -- a combination of both suggestions:

            // as above, then:
            pfcb->show();
            pfcb->setFixedHeight(pfcb->minimumHeight());
            // etc.
            
            1 Reply Last reply
            1
            • R Robert Hairgrove has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved