PDA

View Full Version : Abstraction required in Class



salmanmanekia
3rd July 2008, 12:51
Hi..
I am facing a rather conceptual problem,it is related to the structure of classes in my program.
Initially i had a class derived from QGraphicsView in which i instantiate the object of another class derived from QGraphicsScene ,in the QGraphicsScene i instantiate the object of QGraphicsItem...this seems to be logical ..and it worked fine
the thing to note is that in my QGraphicsScene class i had some button and message text displayed and a QGraphicsItem object
but then i had to produce more abstarction in such a way that i had to develop another class in which i would put all the buttons and message text and would make an object of it in QGraphicsScene...so what i want is that that there should be two objects in my QGraphicsScene ,one that is of QGraphicsItem and the other object is my problem..because i cannot understand what my new class with button and message text should be derived from such that i can include it safely in my scene ...i dont want QGraphicsItem.for my second object...
I hope this is well understood. :)

salmanmanekia
3rd July 2008, 14:17
Actually i have made a new class for that purpose and from my part i did not have any base class for it...so that may be then i can include it in my QGraphicsScene class...but it compiles well ,but throws an error when i try to run it...i am not sure what to do ...

salmanmanekia
5th July 2008, 03:55
Hi ..it seemed to be a very different type of question,i was myself confused before posting to what(and how) to post exactly..but if some one understands the problem and has some advice ,please do tell me...any further queries would be also welcome

salmanmanekia
16th July 2008, 10:45
I have pasted my way of implementing the thing but it shows a runtime error ,what i have tried to do here is that i created a TrainingUi object which is a QGraphicsView,which instantiates QGraphicsScene object called mainScene,the mainscene is responsible for progressWidget which is QGraphicsItem and is successfully implemented and mainScene is also responsible for navigate which is a QWidget/QGraphicsWidget ,but when i create a object of navigate in my mainScene ,it throws a runtime error..



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TrainingUI w;
w.show();
return a.exec();
}

class TrainingUI : public QGraphicsView
{
public:
TrainingUI();
~TrainingUI();
private:
MainScene *mainScn;
};

class MainScene:public QGraphicsScene
{
public:
ProgressWidget *progress;
QGraphicsProxyWidget *proxy;
Navigation *navigate;
MainScene();
};

class Navigation:public QGraphicsWidget
{
Q_OBJECT
};
class ProgressWidget:public QObject,public QGraphicsItem
{
Q_OBJECT
};

jacek
26th July 2008, 00:49
I have pasted my way of implementing the thing but it shows a runtime error
Could you post the exact error message?

salmanmanekia
28th July 2008, 15:40
actually that problem has been solved but in the same regard i faced a new problem so instead of starting a new thread ,i will post the problem here
this following is my implementation but it only shows an empty window


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TrainingUI w;
w.show();
return a.exec();
}



#include "MainScene.h"
#include <QGraphicsView>

class TrainingUI : public QGraphicsView
{
Q_OBJECT
public:

TrainingUI();
~TrainingUI();

private:
..

MainScene *mainScn;

..
};

TrainingUI::TrainingUI()
{
mainScn= new MainScene;

..
}




#include "MainWidget.h"

#include <QGraphicsScene>


class MainScene:public QGraphicsScene
{
Q_OBJECT
public:

MainWidget *mainWidg;
MainScene();
virtual ~MainScene();

};

MainScene::MainScene()
{

mainWidg = new MainWidget;
addWidget(mainWidg);

}



#include "Navigation.h"

class MainWidget:public QWidget
{
Q_OBJECT

public:
Navigation *navigate;

MainWidget();
virtual ~MainWidget();

};

MainWidget::MainWidget()
{

navigate = new Navigation(this);
navigate->setGeometry(30,20,350,200);
// naviagte->show(); // if i put this line here then it shows my navigate widget which means navigate which is a widget is doing its works
}



i think there is some problem in between MainScene and MainWidget..
before introducing MainWidget all the class heirarchy was same and the code inside MainWidget was in MainScene and my program ran successfully...

jacek
28th July 2008, 17:18
Where do you add the scene to the view?

salmanmanekia
28th July 2008, 17:36
in the code above, i mean its in the code which i pasted in my last reply..:)

jacek
28th July 2008, 18:05
I don't see it.

salmanmanekia
28th July 2008, 18:16
i forgot...but its like

setScene(mainScn);
in TrainingUI constructor..:D

jacek
28th July 2008, 18:36
OK, what happens if you add mainScn->addLine( 0, 0, 100, 100 ) to the TrainingUI constructor?

salmanmanekia
29th July 2008, 08:41
It works,i mean the line is shown i also changed the background of the view so for now i can see the line ,i can also see the background,but there is a grey window(empty window) over the background...in my implementation instead of grey window there should be my navigate widget...

salmanmanekia
29th July 2008, 17:57
its solved ,i had to add setParent the MainWidget class in the Naigation class..:)