[SOLVED] [Functions returning pointers] Accessing class methods from base class pointers
-
Greeting gurus!
Firstly, here's some "background info":http://developer.qt.nokia.com/forums/viewthread/7221/ on what's I'm doing.
As is customary with young C++ pupils, I'm having a bit of trouble with pointers. I am using multiple pointer-returning functions to instantiate objects in different classes. Let me show a branch of the tree of functions I'm using.
@TestPrototype* CQpkVI::instantiateTests(int test, bool mode)
{
switch (test)
{
case 20:
returnedWidget = new CQpkVI2(0, mode);
break;
case 21:
returnedWidget = new CQpkVI2(0, mode);
break;
case 22:
returnedWidget = new CQpkVI2(0, mode);
break;default: returnedWidget = new TestPrototype; qDebug() << "Value is a not a valid test PK"; break; } return returnedWidget;
}@
@ TestPrototype* CQp::instantiateCategory(int category, int test, bool mode)
{
if (_check == 0)
{
_check = 1;cqpcbct = new CQpCBCT(this); cqpdos = new CQpDos(this); cqpkvi = new CQpkVI(this); cqpmec = new CQpMec(this); cqpmlc = new CQpMLC(this); cqpmvi = new CQpMVI(this); cqprap = new CQpRap(this); } switch (category) { case 1: returnedWidget = cqprap->instantiateTests(test, mode); break; case 2: returnedWidget = cqpmec->instantiateTests(test, mode); break; case 3: returnedWidget = cqpdos->instantiateTests(test, mode); break; case 4: returnedWidget = cqpmlc->instantiateTests(test, mode); break; case 5: returnedWidget = cqpmvi->instantiateTests(test, mode); break; case 6: returnedWidget = cqpkvi->instantiateTests(test, mode); break; case 26: returnedWidget = cqpcbct->instantiateTests(test, mode); break; default: returnedWidget = 0; qDebug() << "Value not a valid Category PK"; break; } return returnedWidget;
}@
@QWidget* TestCreator::instantiateGroups(int group, int cat, int test, bool mode)
{
switch (group)
{
case 1:
returnedWidget = cqp->instantiateCategory(cat, test, mode);
break;
case 2:
returnedWidget = ent->instantiateCategory(cat, test, mode);
break;
case 3:
returnedWidget = rad->instantiateCategory(cat, test, mode);
break;
default:
qDebug() << "Not a valid group PK";
break;
}return returnedWidget;
}
@where the branch is presented from the bottom up. The above function is used in my MainWindow to populate a QStackedWidget with, well, widgets. That is why this function returns pointers to a QWidget.
The QWidget* is then added to a vector. From that vector, shouldn't I be able to call the derived class methods?
By the way, here's a summary of class hierarchy:
- QWidget
** TestPrototype
*** CQpkVI2
*** CQpkVI1
*** ...
Please tell me if you need any other information concerning anything. Thank you.
- QWidget
-
You can't call those derived class functions unless they are defined and are virtual in QWidget. If they are not defined in QWidget, for you to call them, you have to down cast them. I think in your use case it would be better to have a list of TestProptotypes points so you can use polymorphism.
-
Ok. Thanks. Now I turn to a more Qt-oriented question. Since TestPrototype inherits QWidget, it should be a QWidget. Now, is there a way I can subclass QStackedWidget for it to accept TestPrototype pointers in the function QStackedWidget::addWidget(QWidget*)?
I tried to do QStackedWidget::addWidget(TestProtoype*) thinking it would work because a TestPrototype is-a QWidget, but I got a compiler error.
Thanks!
-
Ok, never mind. It must have been another error somewhere in the code, because now the code compiles.
Initially, it said that there was no addWidget(TestPrototype*) method and that the candidate was addWidget(QWidget*). Now, it doesn't complain and compiles successfully.
Thank you, steno! You've been a great help, Mister Guru! ;)