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. Inheritance with multiple windows.
Forum Updated to NodeBB v4.3 + New Features

Inheritance with multiple windows.

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 3.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.
  • C Offline
    C Offline
    CDuck
    wrote on last edited by
    #1

    I have a Qt Widgets program that has three windows. One main window (mainwindow.h, mainwindow.cpp), an editor (editor.h, editor.cpp), and an about window (about.h, about.cpp), with respective forms attached to them.

    The MainWindow class has a two QList variables, "defs" and "words".

    The Editor class needs to edit both of those variables. When a button is pressed, this code is used:

    @MainWindow::words.append(st);@

    "st" being a string.

    In the "editor.cpp" file, I included the "mainwindow.h" file, the header file with the variables "defs" and "words" declared.

    I am getting the errors:
    "invalid use of non-static data member 'MainWindow::words'"
    "from this location
    @MainWindow::wordsappend(st);@"

    How can I access the MainWindow::words and MainWindow::defs variables without generating an error?

    1 Reply Last reply
    0
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by
      #2

      Hi, you need a copy of MainWindow's this pointer, i.e. you cannot access the words QList using "MainWindow::" prefix, instead you have to use a copy of the this pointer. for example:
      @
      pMainWindow->words.append(st):
      @

      To get the this pointer into your Editor class, you can do like the code ui->setupUI(), it copies the this pointer into the Ui_MainWindow class.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CDuck
        wrote on last edited by
        #3

        I used this code:
        @MainWindow *pMainWindow;
        pMainWindow->words.append(st);@

        and the program unexpectedly crashed. (I am using Windows 7.)

        I know that pointers need to be initialized, but I do not know what to do.

        Thank you for the response.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thiberi0
          wrote on last edited by
          #4

          Well, I think that's because you didn't allocated the space for you pointer. So you try this:
          @
          MainWindow *pMainWindow = new MainWindow;
          pMainWindow->words.append(st);
          @
          You should give I look at this:
          http://www.cplusplus.com/doc/tutorial/pointers/
          http://www.cplusplus.com/doc/tutorial/dynamic/

          thiberi0

          1 Reply Last reply
          0
          • T Offline
            T Offline
            t3685
            wrote on last edited by
            #5

            It seems you're still struggling with the basics of c++, it might help to brush up on that first.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              CDuck
              wrote on last edited by
              #6

              I understand that mistake, using 'new'.

              Can somebody explainhow to get the 'this' pointer from the MainWindow class in my Editor class?

              Inheritance will not be solution, I think, because the Editor class already inherits the QDialog class.

              1 Reply Last reply
              0
              • hskoglundH Online
                hskoglundH Online
                hskoglund
                wrote on last edited by
                #7

                Hi, you could add a function to your Editor class, that accepts a MainWindow flavored this pointer, like this:
                @
                void setMainWindowPtr(MainWindow *p);
                @

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  CDuck
                  wrote on last edited by
                  #8

                  What should be in the body of the function?

                  Because the parameter for the function is a MainWindow, I would have to create a MainWindow variable, right?

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kenchan
                    wrote on last edited by
                    #9

                    A variable that is a pointer to a MainWindow.

                    i.e.
                    @
                    MainWindow *pMw;
                    @
                    and such...

                    May I ask what your programming experience is?
                    I would respectfully suggest that if you are just learning C++ you will may find it difficult to learn both at the same time.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      CDuck
                      wrote on last edited by
                      #10

                      I learned C++ about one year ago, then learned Java, and now working on learning Android Programming.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kenchan
                        wrote on last edited by
                        #11

                        I see. Well is is good to know several useful languages. Good luck with your Qt work.

                        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