PDA

View Full Version : Custom Widget constructor and registration problem.



stepchik
6th March 2012, 14:23
Hello! I would like to create and register custom widget.



class ResizeStackedWidget : public QStackedWidget
{
Q_CLASSINFO("ClassID", "ResizeStackedWidget") //Try to name class
Q_OBJECT
public:
ResizeStackedWidget(QWidget * = 0, const char *name = 0 );
ResizeStackedWidget(const ResizeStackedWidget& other ){} // Try to resolve no copy constructor
void setCurrentIndex(int d);
};

Q_DECLARE_METATYPE(ResizeStackedWidget* ); //I would like register it. Here error

ResizeStackedWidget::ResizeStackedWidget(QWidget * parent, const char *name )
: QStackedWidget(parent)
{
}

And get this error "Error C4430: missing type specifier - int assumed. Note: C++ does not support default-int". Where is my mistackes?

high_flyer
6th March 2012, 14:49
You don't need to call Q_DECLARE_METATYPE on your custom widget - since it is a QObject already (its a sublcass of QStackedWidget).
Q_DECLARE_METATYPE is for non Qt types that you would like to add to use with the signal slot system and QVariant usage.

stepchik
11th March 2012, 08:04
But somewhere in my program i have getType() function. Depends from widget type i do some actions. The problem is ResizeStackedWidget detect as base class QStackedWidget.


String cls = m_widget->metaObject()->className(); //cls = QStackedWidget, but i expected cls = ResizeStackedWidget



const QMetaObject *QStackedWidget::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}

If i step into metaObject() moc_qstackedwidget.cpp, but i suppose to call const QMetaObject *ResizeStackedWidget::metaObject() from moc_resizestackedwidget.cpp file ?

ChrisW67
11th March 2012, 08:40
A copy constructor on a QObject does not make much sense. Are you trying to achieve what the Q_DISABLE_COPY macro does?

Other than that, your code snippet generates no errors here.

stepchik
11th March 2012, 09:43
Are you trying to achieve what the Q_DISABLE_COPY macro does?

I do:) Is it possible to realise some way?

Added after 37 minutes:

Does this mean I have to create my own Custom Widget is not inherited from the some QWidget(QStackedWidget in my case)?
Why it works(constructor class ResizeStackedWidget : public QStackedWidget) as plugin in Qt Designer? I haven't errors.

wysota
11th March 2012, 13:24
I do:) Is it possible to realise some way?
Simply remove your copy constructor or put it in private scope.


Does this mean I have to create my own Custom Widget is not inherited from the some QWidget(QStackedWidget in my case)?
Why it works(constructor class ResizeStackedWidget : public QStackedWidget) as plugin in Qt Designer? I haven't errors.

We don't know what you are trying to do so it is hard to answer those questions. My guess is that you should use virtual methods instead of checking the class name at runtime. Especially that if someone inherits your class and uses the Q_OBJECT macro, the meta-object will point to the inherited class and not yours.