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. Change GUI objects from a scound class
Forum Updated to NodeBB v4.3 + New Features

Change GUI objects from a scound class

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.3k Views 3 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.
  • Q Offline
    Q Offline
    quter
    wrote on last edited by
    #1

    Hi,
    I have a standard Qt Widgets Application.

    I want to access methods in my MainWindow class from a other class.
    (I wrote methode to change the button names, for example)
    The problem is I have to use this instance of the GUI which I instantiate in the main because if I instance a new objekt of my MainWindow class I can not change the button attributes.

    Is that right that I have to use this instance?
    I tried to use singleton for this but im not able to solve it.
    Has anybody a working example how to change GUI attributes from a other class as the mainwindow.cpp?? Maybe you can upload it?

    Thanks guys

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

      Hi and welcome to devnet,

      You should take a look at the Signals and Slots chapter of Qt's documentation as well as the examples.

      You should also describe a bit more your application workflow so we may help you more easily

      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
      • Q Offline
        Q Offline
        quter
        wrote on last edited by
        #3

        Hi and thank You form your answer,

        Not sure if I can solve this with signal and slots.

        My program has two classes one for the GUI witch inherits from QObject and Ui and my second class TcpCon which is used to open a socket to a Java socket server.

        I start the GUI in main with MainWindow w; w.show();

        And finally I want to access the gui from my second class TcpCon.

        I'm tried to bring the variable w to my TcpCon class implement a singleton from MainWindow but my c++ skills are not very good to do that.
        Now I hope that anybody have an example for me. Or give me please a hint how to do it easily:)

        Cheers

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          There's no need for a singleton here. Just pass an instance of one class to the other (via constructor or a proper setter method), e.g.

          class TcpCon : public /* whatever */ {
              ... //the usual stuff
          public:
             inline void setUI(MainWindow* window) { ui = window; }
          private:
             MainWindow* ui = nullptr
          }
          
          //and then:
          void main() {
             ... //other stuff
             MainWindow w;
             w.show();
             TcpCon tcp;
             tcp.setUi(&w); //now you can use ui inside tcp via ui->
          }
          
          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            quter
            wrote on last edited by
            #5

            Hi Chris,

            thank you very much I was able to solve it :). I tried something similar before but I think I have done sth. wrong with all these pointer stuff..

            Cheers

            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