Qt Creator: How to access QLabel inserted by Ui Designer from MainWindow.cpp
-
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!!
-
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
-
@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 ofinstancePtr->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 goClassName::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.