Reputation: 315
I'm building a user interface with qt creator. I've created a StartWindow class extending a QMainWindow. After the creation, I've added new methods and attributes. Automatically QT Creator generates this Kind of code for the header file:
#include <QMainWindow>
namespace Ui {
class StartWindow;
}
class StartWindow : public QMainWindow
{
Q_OBJECT
public:
explicit StartWindow(QWidget *parent = 0);
~StartWindow();
public:
int i;
private:
Ui::StartWindow *ui;
};
Now, when i try to give i the value 2, from the cpp file:
ui->i = 2;
I get this error:
/home/carl/QT5Projects/DopProject/startwindow.cpp:13: error: 'class Ui::StartWindow' has no member named 'i'
but I can access to i
through the scope resolution operator
StartWindow::i = 0;
or
this->i = 0;
Neither can I access i
through
this->ui->i = 0;
Could someone explain that to me?
Upvotes: 1
Views: 376
Reputation: 8355
Both i
and ui
are class member variables, you should be accessing i
the same way you are accessing ui
.
ui is a StartWindow pointer type.
ui
is not of type StartWindow*
, it is of Ui::StartWidow*
type. This is a completely separate class, despite having the same name, it is declared in the Ui
namespace at the beginning of your startwindow.h
file:
#include <QMainWindow>
//class declaration
namespace Ui {
class StartWindow;
}
//...
This class is generated by the Qt User Interface Compiler from the startwindow.ui
file that have been created by the designer. By default, the class is used at the beginning of your StartWindow
class's constructor by calling its setupUi()
member function:
StartWindow::StartWindow(QWidget *parent) :
QMainWindow(parent),
//an instance of Ui::StartWindow is created
ui(new Ui::StartWindow)
//^^^^^^^^^^^^^^^^^^^^^
{
//the instance is used to create the widgets and set up layouts
//that you have defined in the designer
ui->setupUi(this);
//...
}
The Ui::StartWindow
class is defined in the file ui_startwindow.h
which should be included in the beginning of your startwindow.cpp
class. Have a look at this question for information about the ui_startwindow.h
file.
Upvotes: 1
Reputation: 23610
The StartWindow
class has i
and ui
as two separate data members. i
is not a member of ui
, but of the StartWindow
object itself. Hence the containment is like this:
StartWindow object
| |
i ui
and not like this:
StartWindow object
|
ui
|
i
That's why you cannot access i
through ui
, but you must access it directly.
Upvotes: 3