Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Qt Creator: How to access QLabel inserted by Ui Designer from MainWindow.cpp
clhallinan last edited by
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:
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?
Gerhard last edited by
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
JonB last edited by JonB
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
staticin from of them. This means they have to be accessed via an instance of the class, by one of
If however you have declared the method/variable with
staticthen you do not access it via an instance. That is the time where you go
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
statickeyword in C++, for future reference.