Qt6 topics merged with General

How to access actions from QMainWindow in a Widget?

  • I have a QTabWidget as the central Widget of QMainWindow. This tab widget has several tabs with widgets. How can I access the actions from QMainWindow inside these widgets?

    Do I have to do it via a signal and a slot or is there a built in feature for this?

    I have something like this:

    connect(m_ui->actionNew, &QAction::triggered,
                this, &MainWindow::execActionNew);

    Now I would like to access the actionNew inside each of the widgets.

  • Lifetime Qt Champion

    Why do you need access from inside the Widgets?
    Could you not just hook up any signals the Widget has
    to the right slot in MainWindow, same spot where you create the Widgets?
    I assume you want the widget to trigger some actions in MainWindow so
    to keep things not intermixed, the best is was just to declare those signals as public and
    then hook up directly in MainWindows so the Widget does not have to know anything about MainWindow at all.
    It will just emit your signals and be happy.

  • @Infinity

    If you have access to your MainWindow UI elements, you could do something like this:

         // QAction in MainWindows MenuBar will quit the App
        connect(ui->actionTestAction, &QAction::triggered, this, &QApplication::quit);
        // PushButton is the widget on one page of a QTabWidget
        // A click will trigger the QAction and will cause the App to quit, for example
        connect(ui->pushButton, &QPushButton::clicked, ui->actionTestAction, &QAction::trigger);

Log in to reply