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 Have Access To A QWidget in MainWindow *ui Through Another Class
Forum Updated to NodeBB v4.3 + New Features

How To Have Access To A QWidget in MainWindow *ui Through Another Class

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 4 Posters 4.7k 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.
  • L Offline
    L Offline
    Laner107
    wrote on last edited by
    #1

    I am confused on how I can have access and change my QWidget(Which is actually a QCustomPlot) in another class. I cant figure out how to pass through the *QWidget into that class function. When i tried it before it didnt seem to change the actual ui like it should so whats the best way to go about accessing part of the ui, I would need to have access to ui->object inside my other class.

    Example:

    Class A(MainWindow) {
    MainWindow *ui;
    ui->stockGraph->color(blue) <---- can change color of graph in the MainWindow Class
    }

    Class B(StockClass) {
    void changeColor(ui.stockGraph)
    ui.stockGraph->color(blue> <---- I know this is wrong but kind of work like this where i can access the widget in my other class.
    }

    CP71C jsulmJ 2 Replies Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Can you access an instance of class A in class B, or access an instance of class B in class A?
      And when is changeColor called?

      L 1 Reply Last reply
      0
      • L Laner107

        I am confused on how I can have access and change my QWidget(Which is actually a QCustomPlot) in another class. I cant figure out how to pass through the *QWidget into that class function. When i tried it before it didnt seem to change the actual ui like it should so whats the best way to go about accessing part of the ui, I would need to have access to ui->object inside my other class.

        Example:

        Class A(MainWindow) {
        MainWindow *ui;
        ui->stockGraph->color(blue) <---- can change color of graph in the MainWindow Class
        }

        Class B(StockClass) {
        void changeColor(ui.stockGraph)
        ui.stockGraph->color(blue> <---- I know this is wrong but kind of work like this where i can access the widget in my other class.
        }

        CP71C Offline
        CP71C Offline
        CP71
        wrote on last edited by CP71
        #3

        @Laner107
        Hi,
        I hope to well understand your question.
        Ui is declared private so you can’t normally access to him directly.

        I see two ways:

        • Declaring functions to change your instance by external class.
        • Passing in your external class (class B) a pointer to your ui (class A) and declaring B as friend by "friend" statement in class A.

        Class A(MainWindow) {
        friend class B;
        MainWindow *ui;
        ui->stockGraph->color(blue) <---- can change color of graph in the MainWindow Class
        }
        class B
        {
        ...
        classA *myPointer;
        myPointer->ui->stockGraph->color(blue);
        ...
        }
        regards

        L 1 Reply Last reply
        0
        • B Bonnie

          Can you access an instance of class A in class B, or access an instance of class B in class A?
          And when is changeColor called?

          L Offline
          L Offline
          Laner107
          wrote on last edited by
          #4

          @Bonnie So changeColor is just a random example i gave, basically what I need to do is use a secondary class to do a lot of setup work for the ui->stockGraph widget which is part of class A. I need to be able to change the object ui->stockGraph properties in class B.

          1 Reply Last reply
          0
          • CP71C CP71

            @Laner107
            Hi,
            I hope to well understand your question.
            Ui is declared private so you can’t normally access to him directly.

            I see two ways:

            • Declaring functions to change your instance by external class.
            • Passing in your external class (class B) a pointer to your ui (class A) and declaring B as friend by "friend" statement in class A.

            Class A(MainWindow) {
            friend class B;
            MainWindow *ui;
            ui->stockGraph->color(blue) <---- can change color of graph in the MainWindow Class
            }
            class B
            {
            ...
            classA *myPointer;
            myPointer->ui->stockGraph->color(blue);
            ...
            }
            regards

            L Offline
            L Offline
            Laner107
            wrote on last edited by
            #5

            @CP71 Okay option two makes more sense to me so ill give it a shot, thank you

            1 Reply Last reply
            1
            • L Offline
              L Offline
              Laner107
              wrote on last edited by
              #6

              @CP71 So i tried the pointer and friend method, i made sure Class B was a friend to Class A, but when I go to grab a private variable from class A and output it using mainWindow->symbolSearched, it doesnt output anything and my program closes, here is pictures of my code.

              code

              B CP71C 2 Replies Last reply
              0
              • L Laner107

                @CP71 So i tried the pointer and friend method, i made sure Class B was a friend to Class A, but when I go to grab a private variable from class A and output it using mainWindow->symbolSearched, it doesnt output anything and my program closes, here is pictures of my code.

                code

                B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #7

                @Laner107 Seems mainwindow is not assigned anywhere, isn't it a wild pointer?

                1 Reply Last reply
                1
                • L Laner107

                  @CP71 So i tried the pointer and friend method, i made sure Class B was a friend to Class A, but when I go to grab a private variable from class A and output it using mainWindow->symbolSearched, it doesnt output anything and my program closes, here is pictures of my code.

                  code

                  CP71C Offline
                  CP71C Offline
                  CP71
                  wrote on last edited by CP71
                  #8

                  @Laner107
                  Yep, @Bonnie is right!
                  Normally I set a pointer at null value in constructor and I check it before to use (must be not equal to NULL).
                  Try check code with debug.

                  1 Reply Last reply
                  0
                  • L Laner107

                    I am confused on how I can have access and change my QWidget(Which is actually a QCustomPlot) in another class. I cant figure out how to pass through the *QWidget into that class function. When i tried it before it didnt seem to change the actual ui like it should so whats the best way to go about accessing part of the ui, I would need to have access to ui->object inside my other class.

                    Example:

                    Class A(MainWindow) {
                    MainWindow *ui;
                    ui->stockGraph->color(blue) <---- can change color of graph in the MainWindow Class
                    }

                    Class B(StockClass) {
                    void changeColor(ui.stockGraph)
                    ui.stockGraph->color(blue> <---- I know this is wrong but kind of work like this where i can access the widget in my other class.
                    }

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

                    @Laner107 You should not expose internals of a class to other classes! OOP basics.
                    Instead implement setter methods or (when using Qt) slots in the class where you want to change something from outside. Then other classes can call these setters with needed parameters or emit signals connected to the slots in that class. This way other classes do not have to know anything about internal details of your class.

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

                    L 2 Replies Last reply
                    3
                    • jsulmJ jsulm

                      @Laner107 You should not expose internals of a class to other classes! OOP basics.
                      Instead implement setter methods or (when using Qt) slots in the class where you want to change something from outside. Then other classes can call these setters with needed parameters or emit signals connected to the slots in that class. This way other classes do not have to know anything about internal details of your class.

                      L Offline
                      L Offline
                      Laner107
                      wrote on last edited by
                      #10

                      @jsulm I remember doing this in my comp sci class, basically create a void set function and pass in the QWidget, and also a QWidget Get function to call it back for use in other classes?

                      1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Laner107 You should not expose internals of a class to other classes! OOP basics.
                        Instead implement setter methods or (when using Qt) slots in the class where you want to change something from outside. Then other classes can call these setters with needed parameters or emit signals connected to the slots in that class. This way other classes do not have to know anything about internal details of your class.

                        L Offline
                        L Offline
                        Laner107
                        wrote on last edited by
                        #11

                        @jsulm You mentioned slots and signals but im still confused how these work, do you mind doing a quick example of how this would work with Class A and CLass B, this type of concept did confuse me.

                        jsulmJ 1 Reply Last reply
                        0
                        • L Laner107

                          @jsulm You mentioned slots and signals but im still confused how these work, do you mind doing a quick example of how this would work with Class A and CLass B, this type of concept did confuse me.

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

                          @Laner107 It is actually easy: one class declares a signal another one slot, then you connect both. If the first class needs to send information it emits the signal. Please read https://doc.qt.io/qt-5/signalsandslots.html

                          class A
                          {
                          signals:
                              void someSignal(const QString &someInformation);
                          
                          private:
                              void doSomething() { emit someSignal("Some Information"); }
                          }
                          
                          class B
                          {
                          public slots:
                              void reactOnSignal(const QString &someInformation) { // Do something with information }
                          }
                          
                          // Somewhere in your app connect the slots
                          A a;
                          B b;
                          connect(&a, &A::someSignal, &b, &B::reactOnSignal);
                          

                          Of course you can have a setter to set a widget from another class, but this is still bad design, as you are still exposing private details of a class. Another classes should not know anything about internal details. They should use an API (getter/setter) to communicate in more abstract way with other classes, instead of knowing what widgets are used in which way there.

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

                          1 Reply Last reply
                          2
                          • L Offline
                            L Offline
                            Laner107
                            wrote on last edited by
                            #13

                            So what would be the good design way of doing this, all i need is to be able to use a second class so my code is more organized, I just need to be able to change an object(QWidget) thats in MainWindow class in the second class, I know there are different ways to go about it but if I was working for a FANG company or a professional developer what would they do in this scenario? Thank you so much for all the help!

                            jsulmJ 1 Reply Last reply
                            0
                            • L Laner107

                              So what would be the good design way of doing this, all i need is to be able to use a second class so my code is more organized, I just need to be able to change an object(QWidget) thats in MainWindow class in the second class, I know there are different ways to go about it but if I was working for a FANG company or a professional developer what would they do in this scenario? Thank you so much for all the help!

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

                              @Laner107 said in How To Have Access To A QWidget in MainWindow *ui Through Another Class:

                              I just need to be able to change an object(QWidget) thats in MainWindow class in the second class

                              In what way "change"? Use a setter as already suggested, but don't access the widgets directly. For example, if you want to change the text in a QLabel add a setter like this:

                              class B
                              {
                              public:
                                  void setSomeText(const QString &text) {  ui->label->setText(text); }
                              }
                              

                              As you can see the caller (class A) does not have to know where and how exactly this text is set in class B. If you later change the QLabel to something else (QTextEdit for example) you do not have to change class A to make it work.

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

                              1 Reply Last reply
                              2
                              • L Offline
                                L Offline
                                Laner107
                                wrote on last edited by
                                #15

                                I am using a QWidget which is promtoed to a QCustomPolt which is essentially a chart widget that shows values of stock market data, i am basically forging and creating this whole chart in the other class, maybe I should just make it all in the Main Classi just figured it was getting to large so I was going to separate it up.

                                1 Reply Last reply
                                0
                                • L Offline
                                  L Offline
                                  Laner107
                                  wrote on last edited by
                                  #16

                                  @jsulm So basically my header file will lok like this but eventually there will actually be more graphs, essentially you have many options with the graph, right now this is what it is in MainWindow class header file, I was hoping I could put most of these in a serperate class to keep it less crowded, do you think just keeping them all in the header file is my best bet since some do need possible values from MainWindow private members. pciture of functions in header

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • L Laner107

                                    @jsulm So basically my header file will lok like this but eventually there will actually be more graphs, essentially you have many options with the graph, right now this is what it is in MainWindow class header file, I was hoping I could put most of these in a serperate class to keep it less crowded, do you think just keeping them all in the header file is my best bet since some do need possible values from MainWindow private members. pciture of functions in header

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

                                    @Laner107 said in How To Have Access To A QWidget in MainWindow *ui Through Another Class:

                                    I was hoping I could put most of these in a serperate class to keep it less crowded

                                    You can add a new class derived from QWidget for example where you put the graph. Then use this class in your main window. And you can create many instances of that class if you need more than one graph.

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

                                    1 Reply Last reply
                                    0
                                    • L Offline
                                      L Offline
                                      Laner107
                                      wrote on last edited by
                                      #18

                                      @jsulm What do you mean derived from that widget? I thought that if we create an object on the Forms in QT that it is defaulted in MainWindow class and can only be accessed there? Is there a work around or am I not getting something, sorry im very new to qt and just a novice programmer.

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • L Laner107

                                        @jsulm What do you mean derived from that widget? I thought that if we create an object on the Forms in QT that it is defaulted in MainWindow class and can only be accessed there? Is there a work around or am I not getting something, sorry im very new to qt and just a novice programmer.

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

                                        @Laner107 said in How To Have Access To A QWidget in MainWindow *ui Through Another Class:

                                        I thought that if we create an object on the Forms in QT that it is defaulted in MainWindow class and can only be accessed there?

                                        No. You can not only create main window in designer but any number of other widgets.
                                        See "File/New File or Project.../Qt/Qt Designer Form Class.

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

                                        1 Reply Last reply
                                        1
                                        • L Offline
                                          L Offline
                                          Laner107
                                          wrote on last edited by
                                          #20

                                          @jsulm Okay im messing around with it now, I do notice that all of the files in my mainwindow.ui are child objects of MainWindow when i create them? Does this not mean they are automatically in MainWindow class, or can i just promote the widget to my QWidget class, but then how can I actually access and change that QWidget from that class?

                                          jsulmJ 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