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. A beginner's question
Forum Updated to NodeBB v4.3 + New Features

A beginner's question

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.2k 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
    QTLeearn
    wrote on last edited by QTLeearn
    #1

    Hi, I am new to Qt and I have the following question: I declared an pointer to MainWindow in the main window class:

    Ui::MainWindow *ui;
    

    In another class, I first declare an object of main window class, then when I tried to use the ui pointer, the compiler said member access into incomplete type, forwarded declaration.

    MainWindow mWindow;
    mWindow.ui->labelText->setText("Msg"); // member access into incomplete type
    

    This maybe seems like a C++ question, but I am very new to Qt, please help me out, thank you

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

      Hi
      The UI of a Widget is private so you cannot access it from other class.
      While you can make it public, it's not a good way as it fast leads to intermixed and tightly coupled code.

      What works much better is that you define a public method to call from the outside

      like ( in .cpp, remember to add to .h also)
      MainWindow::SetMessageText(QString msg) {
      ui->labelText->setText(msg);
      }

      and from outside
      MainWindow mWindow;
      mWindow.SetMessageText("Msg");

      Just as a note.
      (since its often a beginners mistake)

      In this other class you create a new instance of MainWindow.
      So its not the one you might also have created in main.cpp !
      and if its inside say a buttons click it will very fast be deleted as its a local variable and will be deleted as
      soon as the function ends. So most likely will see nothing on screen.

      Q 2 Replies Last reply
      4
      • mrjjM mrjj

        Hi
        The UI of a Widget is private so you cannot access it from other class.
        While you can make it public, it's not a good way as it fast leads to intermixed and tightly coupled code.

        What works much better is that you define a public method to call from the outside

        like ( in .cpp, remember to add to .h also)
        MainWindow::SetMessageText(QString msg) {
        ui->labelText->setText(msg);
        }

        and from outside
        MainWindow mWindow;
        mWindow.SetMessageText("Msg");

        Just as a note.
        (since its often a beginners mistake)

        In this other class you create a new instance of MainWindow.
        So its not the one you might also have created in main.cpp !
        and if its inside say a buttons click it will very fast be deleted as its a local variable and will be deleted as
        soon as the function ends. So most likely will see nothing on screen.

        Q Offline
        Q Offline
        QTLeearn
        wrote on last edited by
        #3

        @mrjj Thank you for your help and yes, I found the problem you are saying: the new MainWindow object is not the one I created in main.cpp. So is there any way I can continue to use the object I created in main.cpp? Thank you very much

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          Well the normal way is to use signal and slot
          https://doc.qt.io/qt-5/signalsandslots.html

          Where do you create the other window ?
          You click a button to open or how ?

          Q 1 Reply Last reply
          2
          • mrjjM mrjj

            Hi
            Well the normal way is to use signal and slot
            https://doc.qt.io/qt-5/signalsandslots.html

            Where do you create the other window ?
            You click a button to open or how ?

            Q Offline
            Q Offline
            QTLeearn
            wrote on last edited by
            #5

            @mrjj I create another window object but not for showing it, I create it because there are some methods(functions) inside the MainWindow class like send receive sth. I create another window object inside a thread

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

              Hi,

              Since you said you are learning: stop immediately anything you want to do with threads. This is an advanced topic that you should turn to only when knowing correctly the basics. All the more since you can't manipulate GUI elements outside the GUI thread which is usually the main thread.
              Also the asynchronous nature of Qt allows for a large part to avoid using threads.

              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
              5
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                Can you show what you want to reuse from MainWindow in other class?
                One option is to connect the two class in main.cpp or in MainWindow
                depending on how your other class comes into play.

                1 Reply Last reply
                0
                • Q QTLeearn

                  @mrjj I create another window object but not for showing it, I create it because there are some methods(functions) inside the MainWindow class like send receive sth. I create another window object inside a thread

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

                  @QTLeearn said in A beginner's question:

                  I create it because there are some methods(functions) inside the MainWindow class

                  Sounds like you put those methods in wrong place. Either you make these methods static (if this is possible), or (better) put these methods in another class which will cover this functionality.

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

                  1 Reply Last reply
                  4
                  • mrjjM mrjj

                    Hi
                    The UI of a Widget is private so you cannot access it from other class.
                    While you can make it public, it's not a good way as it fast leads to intermixed and tightly coupled code.

                    What works much better is that you define a public method to call from the outside

                    like ( in .cpp, remember to add to .h also)
                    MainWindow::SetMessageText(QString msg) {
                    ui->labelText->setText(msg);
                    }

                    and from outside
                    MainWindow mWindow;
                    mWindow.SetMessageText("Msg");

                    Just as a note.
                    (since its often a beginners mistake)

                    In this other class you create a new instance of MainWindow.
                    So its not the one you might also have created in main.cpp !
                    and if its inside say a buttons click it will very fast be deleted as its a local variable and will be deleted as
                    soon as the function ends. So most likely will see nothing on screen.

                    Q Offline
                    Q Offline
                    QTLeearn
                    wrote on last edited by
                    #9

                    @mrjj thank you, this solved my problem

                    Pablo J. RoginaP 1 Reply Last reply
                    1
                    • Q QTLeearn

                      @mrjj thank you, this solved my problem

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @QTLeearn please don't forget to mark your post as solved! Thanks

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      1

                      • Login

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