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 from another class
Qt 6.11 is out! See what's new in the release blog

Access UI from another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.2k 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.
  • jipe3001J Offline
    jipe3001J Offline
    jipe3001
    wrote on last edited by
    #1

    I am trying to create a class which should access a ui object via a connect function as
    Ui is private but can't get it work:

    I wrote the connect function in the secondary class as follows:
    QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );

    but can't figure out why it is not working.

    AddPlot program is just adding a Widget to a HorizontalLayout

    jsulmJ 1 Reply Last reply
    0
    • jipe3001J jipe3001

      I am trying to create a class which should access a ui object via a connect function as
      Ui is private but can't get it work:

      I wrote the connect function in the secondary class as follows:
      QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );

      but can't figure out why it is not working.

      AddPlot program is just adding a Widget to a HorizontalLayout

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @jipe3001 First of all do this:

      qDebug() << QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );
      

      Does it print true? And can you see any warnings when you start your application?
      Is the SendPlot() signal emitted somewhere?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        is MainWindow the name of the class or of an object ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • jipe3001J Offline
          jipe3001J Offline
          jipe3001
          wrote on last edited by
          #4

          MainWindow is the class of the QMainwindow managed by Ui

          I just need to add A Qwidget to the Horizontal Layout included in MainWindow
          from another class and get out how to access Ui (private) from external class.

          I guess connect is the best way, any help appreciated

          jerome_isAviableJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You can't connect an object to a class. Both end must be objects.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4
            • jipe3001J jipe3001

              MainWindow is the class of the QMainwindow managed by Ui

              I just need to add A Qwidget to the Horizontal Layout included in MainWindow
              from another class and get out how to access Ui (private) from external class.

              I guess connect is the best way, any help appreciated

              jerome_isAviableJ Offline
              jerome_isAviableJ Offline
              jerome_isAviable
              wrote on last edited by
              #6

              @jipe3001
              because you can not link a class name, you have to link an object or a pointer to the object.
              So, a solution is to pass a pointer (or a reference) to the object who has the type name of your class (MainWindow) to the object who has the type name of the class from where you want to connect SIGNAL/SLOT (if you call the second UI object from inside the MainWindow object, it should be: "this"). Then, just use the pointer or the reference instead of the class name (i think you will probably connect them in the constructor of your class).

              1 Reply Last reply
              0

              • Login

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