PDA

View Full Version : Subclass from Qt Ui Form Class, invalid use of incomplete type



HappyCoder
22nd May 2017, 17:12
Hi,

i have a base class like this.
ui_calib_base.h


namespace Ui {
class ui_calib_base;
}

class ui_calib_base : public QWidget
{
Q_OBJECT

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

protected:
Ui::ui_calib_base *ui;
}

ui_calib_base.cpp


ui_calib_base::ui_calib_base(QWidget *parent) : QWidget(parent), ui(new Ui::ui_calib_base)
{
ui->setupUi(this);
ui->tabWidget->setCurrentIndex(0);
}


I have moved the ui pointer to protected.

My derived class looks like this:

calib_analog.h


#include <QObject>
#include <QWidget>

#include <ui/ui_calib_base.h>

class calib_analog : public ui_calib_base
{
Q_OBJECT
public:
explicit calib_analog(QWidget *parent = 0);

signals:

public slots:
};


calib_analog.cpp


#include "calib_analog.h"

calib_analog::calib_analog(QWidget *parent) : ui_calib_base(parent)
{
ui->tabWidget->setCurrentIndex(0); // this gives the error
}


I'm not able to get access to the ui pointer in my derived class. I got the error
"invalid use of incomplete type 'class Ui::ui_calib_base'
forward declaration of 'class Ui::ui_calib_base'

I have move the ui pointer to protected, but have no access to it in my derived class? What i'm doing wrong?
Thx

d_stranz
22nd May 2017, 19:22
Seems like you are deliberately setting out to confuse the heck out of yourself by using the name "ui_calib_base" for both the class declared in the "Ui" namespace as well as the class in the global namespace that derives from QWidget. Why not rename the QWidget class to something more sensible, like simply "calib_base" or "calib_base_widget". Geez.

If the code you posted is your actual code, then I think your mistake is that you are including the wrong header file: <ui/ui_calib_base.h> in calib_analog.h, where you should be including "ui_calib_base.h" (the header for the QWidget class) instead. See how confusing this is with this poor choice of names?

In ui_calib_base.h (the QWidget class header), you should be #including <ui/ui_calib_base.h> instead of using a forward declaration.

HappyCoder
23rd May 2017, 08:34
The includes are all Ok, to make it clear:

The base class is a Qt Form Class located in subdir ui/
ui/ui_calib_base.h
ui/ui_calib_base.cpp
ui/ui_calib_base.ui

This is the derived class from the base class
calib_analog.h
calib_analog.cpp

In my calib_analog i have no access to the ui pointer of the base class although i moved it into the protected section in "ui/ui_calib_base.h".

d_stranz
23rd May 2017, 17:04
In my calib_analog i have no access to the ui pointer of the base class although i moved it into the protected section in "ui/ui_calib_base.h".

You -do- have access to it. As I said, this code:


namespace Ui {
class ui_calib_base;
}


only declares a forward reference to the Ui:: ui_calib_base class; it does not define the class. You need to include that actual header file that defines the Ui:: class. This file is created by MOC when it processes your .ui file.

The compiler is telling you that you are trying to use methods and variables of the Ui:: class in a place where all it knows is the name of the class (via the forward declaration) because it hasn't seen the actual definition. That's an "invalid use of an incomplete type".