Link Menu Option to another window
-
Hello,
I am attempting to link a menu option to another window in QT. For example, in the menu bar, I have an option for "Rules Home". When the user clicks this, it should open the rules form that I have created. I was curious as to how I get the menu option to allow that to happen?
-
Hi and welcome to the forums.
Do you mean from code or using the Designer?
(both is possible)In any case, the menus use QAction so when you click on menu item
its triggered() signal is emitted.So for anything to happen, you connect a slot to this signal and
then create an instance of the other window and show it.https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html
-
Hello,
I do mean in the creator. I managed to figure it out by using
void Firewall::on_actionRules_Home_triggered()
{
Rules *w = new Rules;
w->setAttribute(Qt::WA_DeleteOnClose);
w->show();
} -
Hi
Super you found the "Goto slot" feature.
One note about that one.
Its based on automatic match up using the widget name so after you used "goto slot"
you should not rename the actionRules_Home as then it fails to
auto connect. Nor should you rename the on_actionRules_Home_triggered function.The better solution is to use connect to do it manually as then you can get compile errors
in case you renamed something.That said - for a small app with a few menu items, the auto connect is fine but in
a multiple person project with an app having tons of menus and buttons, the auto connect is
just too fragile in real life. At least thats my experience.