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 5.1k 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 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
                • L Laner107

                  @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 Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #21

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

                  are child objects of MainWindow when i create them? Does this not mean they are automatically in MainWindow class

                  No, this simply means that main window is the parent.
                  Look, if you add a QLabel to your MainWindow you will have a member variable of type QLabel, but QLabel class is not defined inside MainWindow. So, you can define your own widgets and add them as member variables to your MainWindow or any other widget:

                  // In mywidget.h
                  class MyWidget : public QWidget
                  {...};
                  
                  // In mainwindow.h
                  class MainWindow : public QMainWindow
                  {
                  private:
                      MyWidget *myWidget;
                  }
                  

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

                  L 1 Reply Last reply
                  1
                  • jsulmJ jsulm

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

                    are child objects of MainWindow when i create them? Does this not mean they are automatically in MainWindow class

                    No, this simply means that main window is the parent.
                    Look, if you add a QLabel to your MainWindow you will have a member variable of type QLabel, but QLabel class is not defined inside MainWindow. So, you can define your own widgets and add them as member variables to your MainWindow or any other widget:

                    // In mywidget.h
                    class MyWidget : public QWidget
                    {...};
                    
                    // In mainwindow.h
                    class MainWindow : public QMainWindow
                    {
                    private:
                        MyWidget *myWidget;
                    }
                    
                    L Offline
                    L Offline
                    Laner107
                    wrote on last edited by
                    #22

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

                    MainWindow you will have a member variable of type QLabel, but QLabel class is not defined inside Ma

                    If I add a QLabel it shows up in the file u_mainwindow.h instead of mainwindow.h, its like the u_mainwindow is meant to setup the ui i guess, and also the object that i want to use is using an API so is called QCustomPlot *stockGraph, so to actually edit this object that is presented in the MainWindow UI i have to actually add new code in the QCustomPlot API file?

                    jsulmJ 1 Reply Last reply
                    0
                    • L Laner107

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

                      MainWindow you will have a member variable of type QLabel, but QLabel class is not defined inside Ma

                      If I add a QLabel it shows up in the file u_mainwindow.h instead of mainwindow.h, its like the u_mainwindow is meant to setup the ui i guess, and also the object that i want to use is using an API so is called QCustomPlot *stockGraph, so to actually edit this object that is presented in the MainWindow UI i have to actually add new code in the QCustomPlot API file?

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

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

                      If I add a QLabel it shows up in the file u_mainwindow.h instead of mainwindow.h

                      It's because you're using Qt Designer. The code I provided is just an example (without using Qt Designer).

                      "so to actually edit this object that is presented in the MainWindow UI i have to actually add new code in the QCustomPlot API file?" - no, that would mean to rebuilt Qt modules from sources. What do you mean by "edit"? Change its state? For that you simply use its public API. Please explain better what you want to do.

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

                      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