Qt Cannot access *ui pointer from inheriting class

922 views Asked by At

I am trying to write an application where I would have a generic dialog window and specific dialog windows that would inherit some basic functionalities from the generic one. I am not sure this is the best approach for this, but this is how I did it (The CGenericProject class was created from Dialog template in Qt Creator):

CGenericProject.h:

#include <QDialog>

namespace Ui {
class CGenericProject;
}

class CGenericProject : public QDialog
{
    Q_OBJECT

public:
    explicit CGenericProject(QWidget *parent = 0);
    ~CGenericProject();

protected:
    Ui::CGenericProject *ui;
};

CGenericProject.cpp:

#include "cgenericproject.h"
#include "ui_cgenericproject.h"

CGenericProject::CGenericProject(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CGenericProject)
{
    ui->setupUi(this);
}

CGenericProject::~CGenericProject()
{
    delete ui;
}

CEisProject.h:

#include "cgenericproject.h"

class CEisProject : public CGenericProject
{
public:
    CEisProject();
    ~CEisProject();

};

CEisProject.cpp:

#include "ceisproject.h"

CEisProject::CEisProject()
{
    ui-> NO ACCESS
}

CEisProject::~CEisProject()
{

}

As you see in the CEisProject.cpp file, I have no access to the ui field inherited from CGenericProject, even though it is protected. I mean, I see ui itself, but I dont see its methods and members. Any other variable that I would define there, would be accessible. What's wrong? I would appreciate all help in this manner.

1

There are 1 answers

2
aslg On BEST ANSWER

You have to add the line

#include "ui_cgenericproject.h"

to the CEisProject.cpp file.

The CGenericProject.h file is included in CEisProject.h, but CEisProject.h does not have access to CGenericProject.cpp. In the header of your base class you have a only forward declaration of Ui::CGenericProject, and you include its file in the .cpp. So CGenericProject.cpp knows the implementation of this class.

But CEisProject.cpp doesn't have access to that, so you have to include the file again in here.

NOTE Your forward declaration is confusing, you should indent it properly. Also, add some comments to your code to add some clarity for who is reading it, you're using two different classes with the same name.