PDA

View Full Version : QGraphicsView



Shawn
31st May 2007, 15:17
I derive a subclass from QMainWindow, I want to set the QGraphicsView object as the mainwindow's centralWidget, like below:


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
test w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}

test::test(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

QPixmap pixCT;
pixCT.load("Resources/CT.bmp");
QGraphicsScene scene;
QGraphicsView view(&scene, this);
setCentralWidget(&view);
scene.addPixmap(pixCT);
//view.show();
}

But it doesn't work. What should I do to solve this problem ?

Thank you very much for helping!

marcel
31st May 2007, 16:43
You're creating the scene and the view locally in the constructor of test. Therefore they are destroyed after the constructor returns.

The solution is to either make them members of test or creating them dynamically in the constructor.

Regards

Shawn
1st June 2007, 01:50
hehe

Thanks ! a silly mistake...

I made them members and got passed.

BTW, after
scene.addPixmap(pixCT); is there any way I can control its position? is there some function like view.draw(&pixmap, x, y) or scene.addPixmap(&pixmap, x, y)?

marcel
1st June 2007, 05:08
is there any way I can control its position? is there some function like view.draw(&pixmap, x, y) or scene.addPixmap(&pixmap, x, y)?


No, not like this.
Add pixmap return a QGraphicsPixmapItem. You can play with this - translate it, rotate it, scale it, etc.

Regards

Shawn
1st June 2007, 05:51
I look through the QGraphicsPixmapItem reference but still have no idea...

Would you please give me a small example for showing pixmap at (x, y ) in QGraphicsView ? I 'd be very appreciate of that if you would.

I have read the code of example "exportedcanvas", it's a lillte too "large" for me...

wysota
1st June 2007, 05:55
QGraphicsItem::setPos

Shawn
1st June 2007, 09:39
QGraphicsItem::setPos

It acts very strange when I using this setPos()


class test : public QMainWindow
{
Q_OBJECT

public:
test(QWidget *parent = 0, Qt::WFlags flags = 0);
~test();
QGraphicsView view;
QGraphicsScene scene;

private:
Ui::testClass ui;

};

test::test(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

QPixmap pixTrans;
pixTrans.load("Resources/Trans.bmp");

view.setScene(&scene);
setCentralWidget(&view);

QGraphicsPixmapItem* i = scene.addPixmap(pixTrans);
i->setPos(-200,-200);
}

before I using this setPos, the central point of the image is at (300,200) (because the size of centralWidget is 600*400);
after I using this setPos(-200,-200), the central point of the image moves to (200,100). why?

wysota
1st June 2007, 09:41
Because you didn't set the scene size and in such situation the scene adapts to the amplitude (spread) of coordinates used.

Shawn
4th June 2007, 13:57
I had read the Graphics View Framework for days but I am now confused with "mapping this mapping that" and still have no clear idea about my work.

The expected diagram is like attachment "SLD", and from a xml file, I can get information like this, as showing in attachment "SLD2":
for item E1\Q2\U1, it has 2 connectivity nodes: E1\Q2\l0, E1\Q2\l1
for item E1\Q1\I1, it has 2 connectivity nodes: E1\Q2\l1, E1\Q2\l2
so that I can decide that U1 is connected with I1 by connecivity node E1\Q2\l1.

what I want to know is : using the Praphics View Framework , how can I decide each item's position ?

wysota
4th June 2007, 14:12
Position related to what? The scene? Or the parent item?

Shawn
4th June 2007, 14:22
I just don't have an idea that how to arrange these items, the only information I have is the connectivity node.

wysota
4th June 2007, 14:31
It's safest to go through the scene then. Use mapToItem() or mapToScene(), depending on which one you currently need.