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. Access UI elements from another class with a UI
Forum Updated to NodeBB v4.3 + New Features

Access UI elements from another class with a UI

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 389 Views 1 Watching
  • 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by
    #1

    I have two classes with Ui's
    MainWindow and firstStackWidget

    In firstStackWidget I'd like to access the MainWindow's Ui.

    In my firstStackWidget a new menu is shown and I'd like to access that menu from there to connect it to a subclass of firstStackWidget.

    I've already made them friend classes, but that didn't allow me to access the ui elements yet.
    Is there anyone who can help?

    1 Reply Last reply
    0
    • PsnarfP Offline
      PsnarfP Offline
      Psnarf
      wrote on last edited by Psnarf
      #2

      The Ui's are declared private in the header files, don't change that.. You can delete the friend declarations. I ran into a similar problem calling a function in one QDialog from another. You have to add
      #include " MainWindow.h" at the top of firstStackWidget.cpp. To call functions or slots in MainWindow.h, declare a variable of type MainWindow, then use "dot" to access MainWindow functions. In MainWindow.ui, choose the menu item you wish to access, right-click, go to slot triggered, then in MainWindow.h, change private slots: to public.

      // MainWindow.h
      //private slots:
      public slots:
          void on_actionSomeMenuItem_triggered();
      
      //firstStackWidget.cpp
      #include "MainWindow.h"
      ...
      firstStackWidget::someFunction()
      {
      //...
          MainWindow main;
          main.on_actionSomeMenuItem_triggered();
      }
      

      Do not access the MainWindow UI directly, create a slot for the desired UI menu item-triggered, then call that public slot from the other UI.
      Please let me know if I left anything out.

      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