QDialog with a menu?
-
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?
-
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?
@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. -
@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.@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.
-
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?
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()
{}
-
@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"
-
@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"
-
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.
-
@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.
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
'scloseEvent
. You dont need to subclass for it (well, just your basic main window subclass that you are using anyway). -
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
'scloseEvent
. You dont need to subclass for it (well, just your basic main window subclass that you are using anyway).@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
'scloseEvent
. 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