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. How to access actions from QMainWindow in a Widget?

How to access actions from QMainWindow in a Widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 437 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by
    #1

    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.

    Pl45m4P 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      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.

      1 Reply Last reply
      2
      • I Infinity

        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.

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

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

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

        ~E. W. Dijkstra

        1 Reply Last reply
        1

        • Login

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