[Qt-interest] inheritance problem

Malyushytsky, Alex alex at wai.com
Fri Feb 26 21:42:14 CET 2010


>>But when I'm trying to create class that subclass from custPanel_TabWidget I get this error:
>>invalid use of incomplete type 'struct custPanel_TabWidget'
>>
>>and the header is like this:

>>#include "custpanel.h"
>>class custPanel_TabWidget;

>>class classForTab1: public custPanel_TabWidget {
>>public:


First. Let's forget for a second for your goals.
You can't derive from incomplete type and that is what compiler tells you.
You can use forward declaration only until compiler does not need to know anything about object.
It means "class custPanel_TabWidget;" has to be replaced with proper #include "custPanel_TabWidget.h"

Second.
You can promote each tab widget to whatever class you need.
That what you normally do, cause each page in the QTabWidget is a widget you can subclass QWidget and place specific for the page code in separated class.

If you already use designer I would highly recommend to consider promotion feature.
Otherwise you will have to deal with a fact that instance of the widget of the base class you want to derive from is already created and this widget is already placed in the layout.


Alex



-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of franki
Sent: Friday, February 26, 2010 3:49 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] inheritance problem

Thursday 25 of February 2010 23:57:00 Malyushytsky, Alex napisał(a):
> You derive from the class you need to derive from.
>
> If I understood your problem (which I am not sure) you want  to use your
> derived class instead of QTabWidget. In this case you might use "Promoting
> Widgets" technique.
> http://doc.trolltech.com/4.6/designer-using-custom-widgets.html

Hi,

I've read about promoting widgets and I think it's not what I need.
Maybe I explain it in different words:
The way I'm doing my application now is called: "The Single Inheritance
Approach", so in my case QTabWidget is placeholder for all widgets that
constitute the user interface.
Next, I sublassed this QTabWidget with my class custPanel_TabWidget
and the header file is like this:

#include "ui_custpanel.h"
class custPanel_TabWidget: public QTabWidget
{
        Q_OBJECT
public:
        custPanel_TabWidget ();
        ~custPanel_TabWidget();
        Ui::custPanel_TabWidget cp;
}

so now all widgets created inside QTabWidget (by Qt Designer) are accessible
by this variable "cp", and when I need to connect some signals, genrated for
example by pushButtons, with functions inside custPanel_TabWidget then it is
simple as that:
connect(cp.saveButton,SIGNAL(clicked()),this,SLOT(saveValues()))

But the problem is, that I have lets say 10 tabs in QTabWidget (each tab
present some different data for user, and each use different functions to
manage this data) and all these functions are inside class
custPanel_TabWidget - so this file has grown big, and does not look clean.

So I thought about creating class for each tab, and that class should maintain
functions needed to interact with user.
Now is what I'm not sure:
As I understand this now, if I subclass from class custPanel_TabWidget I will
be able to access "cp" variable (which means easy access to all widgets in
QTabWidget just like this->cp), and I will be able to access all public
functions created inside custPanel_TabWidget, so the inheritance model would
look like this
QTabWidget -> custPanel_TabWidget ->classForTab1

But when I'm trying to create class that subclass from custPanel_TabWidget I
get this error:
invalid use of incomplete type 'struct custPanel_TabWidget'

and the header is like this:

#include "custpanel.h"
class custPanel_TabWidget;

class classForTab1: public custPanel_TabWidget
{
public:
        classForTab1 ();
        ~classForTab1();
};

So are there some mistakes in what I think I know about inheriting from other
classes, and how to make this class "classForTab1"

The second apporoach that I mentioned in previous post, was to create new
class and pass it variables like "cp" (to access widgets inside QTabWidget)
and others - to access objects holding connection to sql, and finally pointer
to the base class (custPanel_TabWidget)
So the constructor for classForTab1 looks like this:
classForTab1::classForTab1(Ui::custPanel_TabWidget cp,ItSql *itsql,QVariant
cust_id,custPanel_TabWidget *parent) : QWidget ()

It works fine, but it does not look clean.
So please help.

Greetings
Marek

>
> In your case:
>
> - You subclass from the QTabWidget
> - in designer place QTabWidget and then promote it.
>
> Hope this helps,
>    Alex
>
>
>
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com
> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of franki Sent:
> Thursday, February 25, 2010 8:11 AM
> To: qt-interest at trolltech.com
> Subject: [Qt-interest] inheritance problem
>
> Hi,
>
> I've application with QMainWindow and tabWidget as a centralWidget,
> class that maintains this tabWidget is called custPanel_TabWidget
> some piece of custpanel.h:
>
> #ifndef CUSTPANEL_H
> #define CUSTPANEL_H
> #include "ui_custpanel.h"
>
> class custPanel_TabWidget: public QTabWidget
> {
>         Q_OBJECT
> public:
>         custPanel_TabWidget (QWidget *parent,QVariant cust_id,ItSql
> *itsql); ~custPanel_TabWidget();
>         Ui::custPanel_TabWidget cp;
> }
>
> I wanted each tab to be maintained by its own class, because there is so
> many functions that custPanel_TabWidget started to be huge mess.
> so I did class e.g. Notify
> header notify.h:
>
> #ifndef NOTIFY_H
> #define NOTIFY_H
> #include <QObject>
> #include "custpanel.h"
>
> class custPanel_TabWidget;
>
> class Notify: public custPanel_TabWidget
> {
> public:
>         Notify ();
>         ~Notify();
> };
>
> but during compilation, I get error:
> invalid use of incomplete type 'struct custPanel_TabWidget'
>
> I've googled somewhere that I should derive from Ui::custPanel_TabWidget
> instead of custPanel_TabWidget, because this class is called that way in
> ui_custpanel.h - which is true.
> But, when I've tried to derive from Ui::custPanel_TabWidget, program
> compile fine, but I can't access any widget on tabWidget, and in fact which
> class do I derive from?
>
> Second sollution for me, that I've proven to work earlier, was to pass some
> variables to new class instead of deriving from custPanel_TabWindow, and
> maintain all function related to this tab in that new class.
> So in that case, the constructor is like this:
>
> Abonament::Abonament(Ui::custPanel_TabWidget cp,ItSql *itsql,QVariant
> cust_id,custPanel_TabWidget *parent) : QWidget ()
>
> And this second approach works fine for me, but which one is better, or
> which one is recommended?
>
> Thanks in advance
> Marek
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
> ---------------------------------------------------------------------------
>------------------------ Weidlinger Associates, Inc. made the following
> annotations.
>
> "This message and any attachments are solely for the intended recipient and
> may contain confidential or privileged information. If you are not the
> intended recipient, any disclosure, copying, use, or distribution of the
> information included in this message and any attachments is prohibited. If
> you have received this communication in error, please notify us by reply
> e-mail and immediately and permanently delete this message and any
> attachments. Thank you."
>
> "Please consider our environment before printing this email."
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."




More information about the Qt-interest-old mailing list