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. Qt Creator: How to access QLabel inserted by Ui Designer from MainWindow.cpp
Forum Updated to NodeBB v4.3 + New Features

Qt Creator: How to access QLabel inserted by Ui Designer from MainWindow.cpp

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 2.5k Views
  • 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
    clhallinan
    wrote on 9 Nov 2019, 03:34 last edited by
    #1

    I'm gaining competency w/ C++, but a beginner w/ Qt. I started a new project using Qt Creator. In Ui Designer, I added a QLabel object. I want to change the text of the label under program control from my mainwindow.cpp. I have not figured out how to access it.

    The build system creates Ui_MainWindow.h where my label is defined as a QLabel * inside a class called Ui_MainWindow. Accessing it like this:

    Ui_MainWindow::myQLabel->setText("NewLabel");
    

    from within my main MainWindow.cpp created by the framework yields a compiler errror:

    ../mainwindow.cpp:63:20: error: invalid use of non-static data member 'myQLabel'
        Ui_MainWindow::myQLabel->setText("NewLabel");
    

    What is the proper way to gain access to this QLabel object that is inserted by the Qt Creater Design module from elsewhere in my application such as from class MainWindow?

    Thanks!!

    J 1 Reply Last reply 9 Nov 2019, 09:54
    0
    • G Offline
      G Offline
      Gerhard
      wrote on 9 Nov 2019, 07:14 last edited by
      #2

      Hello,

      for your UI_MainWindow is created a member named ui in your mainwndow.h. This Widget is allocated in the constructor of MainWindow and also created in the constructor (ui->setupUi()).
      To Access the members of you can use ui->myQLabel->setText("bla bla");

      sorry for my english

      Gerhard

      1 Reply Last reply
      6
      • C clhallinan
        9 Nov 2019, 03:34

        I'm gaining competency w/ C++, but a beginner w/ Qt. I started a new project using Qt Creator. In Ui Designer, I added a QLabel object. I want to change the text of the label under program control from my mainwindow.cpp. I have not figured out how to access it.

        The build system creates Ui_MainWindow.h where my label is defined as a QLabel * inside a class called Ui_MainWindow. Accessing it like this:

        Ui_MainWindow::myQLabel->setText("NewLabel");
        

        from within my main MainWindow.cpp created by the framework yields a compiler errror:

        ../mainwindow.cpp:63:20: error: invalid use of non-static data member 'myQLabel'
            Ui_MainWindow::myQLabel->setText("NewLabel");
        

        What is the proper way to gain access to this QLabel object that is inserted by the Qt Creater Design module from elsewhere in my application such as from class MainWindow?

        Thanks!!

        J Offline
        J Offline
        JonB
        wrote on 9 Nov 2019, 09:54 last edited by JonB 11 Sept 2019, 09:55
        #3

        @clhallinan said in Qt Creator: How to access QLabel inserted by Ui Designer from MainWindow.cpp:

        I'm gaining competency w/ C++, but a beginner w/ Qt.

        Your question relates to general C++ programming rather than anything Qt. @Gerhard's reply above is correct.

        When you declare class methods or variables, you normally do not put static in from of them. This means they have to be accessed via an instance of the class, by one of

        instancePtr->method();
        instance.method();
        

        If however you have declared the method/variable with static then you do not access it via an instance. That is the time where you go

        ClassName::method();
        

        so you are using the ::.

        That is what the error message was telling you.

        You might want to read up on the use of the static keyword in C++, for future reference.

        1 Reply Last reply
        4

        1/3

        9 Nov 2019, 03:34

        • Login

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