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 with a menu?
QtWS25 Last Chance

QDialog with a menu?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.1k Views
  • 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.
  • D Offline
    D Offline
    Dan203
    wrote on last edited by
    #1

    I need a dialog with a menu. The only way to add a menu to a dialog in the designer is to make it a QMainWindow. This looks fine except I can't call the dialog from my main app using exec() like I'd do with a QDialog. I can use show() instead but then it's not modal (even if I set the windowModality property in the designer) and doesn't return accepted/rejected.

    Is there some way to either have a QDialog with a menubar or launch a QMainWindow as modal?

    Pl45m4P CP71C 2 Replies Last reply
    0
    • D Dan203

      I need a dialog with a menu. The only way to add a menu to a dialog in the designer is to make it a QMainWindow. This looks fine except I can't call the dialog from my main app using exec() like I'd do with a QDialog. I can use show() instead but then it's not modal (even if I set the windowModality property in the designer) and doesn't return accepted/rejected.

      Is there some way to either have a QDialog with a menubar or launch a QMainWindow as modal?

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

      @Dan203 said in QDialog with a menu?:

      I can use show() instead but then it's not modal (even if I set the windowModality property in the designer) and doesn't return accepted/rejected

      It should work using show() and the modal property.
      Make sure that you open your window after the property is set (and not before)
      You can add the return values and the handling yourself.


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

      ~E. W. Dijkstra

      D 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @Dan203 said in QDialog with a menu?:

        I can use show() instead but then it's not modal (even if I set the windowModality property in the designer) and doesn't return accepted/rejected

        It should work using show() and the modal property.
        Make sure that you open your window after the property is set (and not before)
        You can add the return values and the handling yourself.

        D Offline
        D Offline
        Dan203
        wrote on last edited by
        #3

        @Pl45m4 said in QDialog with a menu?:

        @Dan203 said in QDialog with a menu?:

        I can use show() instead but then it's not modal (even if I set the windowModality property in the designer) and doesn't return accepted/rejected

        It should work using show() and the modal property.
        Make sure that you open your window after the property is set (and not before)
        You can add the return values and the handling yourself.

        While I can technically get this to work using show() and some slots/signals it's a bit messy. I'd prefer to have it just block inline like a dialog so I can check the return value as soon as it closes.

        I found a stackoverflow post which showed how to use an eventloop to block after calling show, but it requires a custom class because QMainWindow doesn’t emit any signals when it closes.

        I also found one that suggested embedding the QMainWindow into a QDialog as a widget. It got a lot of negative responses though, so haven’t tried that one yet.

        Pl45m4P 1 Reply Last reply
        0
        • D Dan203

          I need a dialog with a menu. The only way to add a menu to a dialog in the designer is to make it a QMainWindow. This looks fine except I can't call the dialog from my main app using exec() like I'd do with a QDialog. I can use show() instead but then it's not modal (even if I set the windowModality property in the designer) and doesn't return accepted/rejected.

          Is there some way to either have a QDialog with a menubar or launch a QMainWindow as modal?

          CP71C Offline
          CP71C Offline
          CP71
          wrote on last edited by CP71
          #4

          @Dan203

          Never done!
          But your question has intrigued me and I have tried to insert a QToolbar in the Dialog.
          As you says it seems impossible to do via UI designer, but there is QToolButton widget.
          But via code seems yes.

          Dialog::Dialog(QWidget *parent)
          : QDialog(parent)
          , ui(new Ui::Dialog)
          {
          ui->setupUi(this);

          myToolbar = new QToolBar("Test", this);
          
          QAction *Test = new QAction();
          Test->setText("try");
          Test->setIcon(QIcon(":/icon/pin.png"));
          myToolbar->addAction( Test );
          connect( Test, &QAction::triggered, this, &Dialog::test );
          
          ui->verticalLayout->insertWidget( 0, myToolbar );
          

          }

          Dialog::~Dialog()
          {
          delete ui;
          }

          void Dialog::test()
          {

          }

          cd25614b-baaf-4718-a490-20e048d21339-image.png

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @Dan203 said in QDialog with a menu?:

            Is there some way to either have a QDialog with a menubar

            Yes, build it with one. A QMenuBar is just another widget.

            #include <QApplication>
            #include <QDialog>
            #include <QDialogButtonBox>
            #include <QGroupBox>
            #include <QVBoxLayout>
            #include <QMenuBar>
            #include <QAction>
            #include <QDebug>
            
            class MyDialog: public QDialog
            {
                Q_OBJECT
            public:
                explicit MyDialog(QWidget *p = nullptr)
                    : QDialog(p)
                {
                    QVBoxLayout *layout = new QVBoxLayout(this);
                    QMenuBar *menubar = new QMenuBar(this);
                    layout->addWidget(menubar);
                    QMenu *menu1 = menubar->addMenu("Menu&1");
                    QAction *action1 = menu1->addAction("Action 1");
                    QAction *action2 = menu1->addAction("Action 2");
                    QMenu *menu2 = menubar->addMenu("Menu&2");
                    QAction *action3 = menu2->addAction("Action 3");
                    QAction *action4 = menu2->addAction("Action 4");
                    // connect the actions however you want
            
                    // Some dummy content
                    QGroupBox *content = new QGroupBox(this);
                    content->setMinimumHeight(100);
                    layout->addWidget(content);
            
                    QDialogButtonBox *buttons = new QDialogButtonBox(
                                QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
                                this
                    );
                    layout->addWidget(buttons);
                    connect(buttons, &QDialogButtonBox::accepted, this, &MyDialog::accept);
                    connect(buttons, &QDialogButtonBox::rejected, this, &MyDialog::reject);
            
                }
            
            };
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                MyDialog d;
                int ret = d.exec();
                qDebug() << "Dialog returned" << ret;
            }
            #include "main.moc"
            

            Screenshot_20210804_182502.png

            CP71C 1 Reply Last reply
            4
            • C ChrisW67

              @Dan203 said in QDialog with a menu?:

              Is there some way to either have a QDialog with a menubar

              Yes, build it with one. A QMenuBar is just another widget.

              #include <QApplication>
              #include <QDialog>
              #include <QDialogButtonBox>
              #include <QGroupBox>
              #include <QVBoxLayout>
              #include <QMenuBar>
              #include <QAction>
              #include <QDebug>
              
              class MyDialog: public QDialog
              {
                  Q_OBJECT
              public:
                  explicit MyDialog(QWidget *p = nullptr)
                      : QDialog(p)
                  {
                      QVBoxLayout *layout = new QVBoxLayout(this);
                      QMenuBar *menubar = new QMenuBar(this);
                      layout->addWidget(menubar);
                      QMenu *menu1 = menubar->addMenu("Menu&1");
                      QAction *action1 = menu1->addAction("Action 1");
                      QAction *action2 = menu1->addAction("Action 2");
                      QMenu *menu2 = menubar->addMenu("Menu&2");
                      QAction *action3 = menu2->addAction("Action 3");
                      QAction *action4 = menu2->addAction("Action 4");
                      // connect the actions however you want
              
                      // Some dummy content
                      QGroupBox *content = new QGroupBox(this);
                      content->setMinimumHeight(100);
                      layout->addWidget(content);
              
                      QDialogButtonBox *buttons = new QDialogButtonBox(
                                  QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
                                  this
                      );
                      layout->addWidget(buttons);
                      connect(buttons, &QDialogButtonBox::accepted, this, &MyDialog::accept);
                      connect(buttons, &QDialogButtonBox::rejected, this, &MyDialog::reject);
              
                  }
              
              };
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  MyDialog d;
                  int ret = d.exec();
                  qDebug() << "Dialog returned" << ret;
              }
              #include "main.moc"
              

              Screenshot_20210804_182502.png

              CP71C Offline
              CP71C Offline
              CP71
              wrote on last edited by
              #6

              @Dan203
              After see the @ChrisW67 's answer I didn't understand your question, sorry! :(

              I really need a holiday : D

              I leave it the same even if my previous post is not what you asked for.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dan203
                wrote on last edited by
                #7

                I ended up just creating my own class QtMainWindowDialog which subclasses QMainWindow but adds exec() and the accept(), reject() and done(int) slots. I used code similar to what's in the source for QDialog, just with some minor modifications. It seems to work well.

                1 Reply Last reply
                0
                • D Dan203

                  @Pl45m4 said in QDialog with a menu?:

                  @Dan203 said in QDialog with a menu?:

                  I can use show() instead but then it's not modal (even if I set the windowModality property in the designer) and doesn't return accepted/rejected

                  It should work using show() and the modal property.
                  Make sure that you open your window after the property is set (and not before)
                  You can add the return values and the handling yourself.

                  While I can technically get this to work using show() and some slots/signals it's a bit messy. I'd prefer to have it just block inline like a dialog so I can check the return value as soon as it closes.

                  I found a stackoverflow post which showed how to use an eventloop to block after calling show, but it requires a custom class because QMainWindow doesn’t emit any signals when it closes.

                  I also found one that suggested embedding the QMainWindow into a QDialog as a widget. It got a lot of negative responses though, so haven’t tried that one yet.

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

                  Good to hear that you've found a solution.

                  @Dan203 said in QDialog with a menu?:

                  QMainWindow doesn’t emit any signals when it closes.

                  You can emit your own signal in the QMainWindow's closeEvent. You dont need to subclass for it (well, just your basic main window subclass that you are using anyway).


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

                  ~E. W. Dijkstra

                  D 1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

                    Good to hear that you've found a solution.

                    @Dan203 said in QDialog with a menu?:

                    QMainWindow doesn’t emit any signals when it closes.

                    You can emit your own signal in the QMainWindow's closeEvent. You dont need to subclass for it (well, just your basic main window subclass that you are using anyway).

                    D Offline
                    D Offline
                    Dan203
                    wrote on last edited by
                    #9

                    @Pl45m4 said in QDialog with a menu?:

                    Good to hear that you've found a solution.

                    @Dan203 said in QDialog with a menu?:

                    QMainWindow doesn’t emit any signals when it closes.

                    You can emit your own signal in the QMainWindow's closeEvent. You dont need to subclass for it (well, just your basic main window subclass that you are using anyway).

                    I am doing that too. I'm emitting accepted, rejected and done just like a QDialog. The subclass isn’t really that complicated. If I can get permission from my boss I'll throw it on GitHub

                    1 Reply Last reply
                    0

                    • Login

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