PDA

View Full Version : Examples of QGraphicsView



grosem
26th July 2006, 12:24
Hey,
I'm just trying Qt 4.2 and especially the QGraphicsView thing is interesting for me. Unfortunatly my Qt knowledge is a little bit too small to understand the Qt examples of the documentation.

Could you help me with a really small example?

Thanks

jacek
26th July 2006, 13:38
#include <QApplication>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>

int main( int argc, char **argv )
{
QApplication app(argc, argv);

QGraphicsScene scene;
scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );

QGraphicsEllipseItem *item = new QGraphicsEllipseItem( 0, &scene );
item->setRect( -50.0, -50.0, 100.0, 100.0 );

QGraphicsView view( &scene );
view.setRenderHints( QPainter::Antialiasing );
view.show();

return app.exec();
}

grosem
26th July 2006, 15:17
Thanks :)
I changed some details for me and it works :-)

grosem
20th August 2006, 11:27
Hey,
I want to drag and drop the items at my GraphicsView/Scene. I've looked at the examples again and it seems that there is no very easy way like

item->enableDragging;

and done? Even if the item stays at it's scene/view?
Maybe it's the better way for me if I do it manually and capture the mouseevent by hand and do the repositioning myself?

Thanks for your help :)

PS: As you've probably recognized I use the QGraphicsScene of Qt 4.2.

jacek
20th August 2006, 13:17
item->setFlag( QGraphicsItem::ItemIsMovable );

grosem
20th August 2006, 22:03
Thanks for your help.
I have QGraphicsView as one part of a splitter. Moving the item to the left or to the right of the splitter part a scroll bar gets created. Is there a way to avoid this scroll bar?

jpn
20th August 2006, 22:15
Is there a way to avoid this scroll bar?
Set both scrollbar policies to Qt::ScrollBarAlwaysOff.
void setHorizontalScrollBarPolicy ( Qt::ScrollBarPolicy ) (http://doc.trolltech.com/4.2/qabstractscrollarea.html#horizontalScrollBarPolicy-prop)
void setVerticalScrollBarPolicy ( Qt::ScrollBarPolicy ) (http://doc.trolltech.com/4.2/qabstractscrollarea.html#verticalScrollBarPolicy-prop)

grosem
20th August 2006, 22:41
Well,
I described my problem in a wrong way, but solved it now myself.
Nevertheless: Thank you :)

grosem
22nd August 2006, 11:31
Hey, I've got now my next problem. I want to reimplement a event handler at my QGraphicsScene. That's why I derived a class from QGraphicsscene, but I'm doing something wrong:

scene.h


#ifndef SCENE_H
#define SCENE_H

#include <QGraphicsScene>

class Scene: public QGraphicsScene
{
Q_OBJECT
public:

void dragLeaveEvent ( QGraphicsSceneDragDropEvent * event );


};

#endif


scene.cpp

#include "scene.h"

void Scene::dragLeaveEvent ( QGraphicsSceneDragDropEvent * event )
{
qDebug("dragLeaveEvent");
}


OK, then I create an object of Scene and use it in the same way I used the QGraphicsScene Object before. When I try to compile I get the following error:


g++ -Wl,-rpath,/usr/local/Trolltech/Qt-4.2.0-snapshot-20060807/lib -o mainwindow.o main.o game.o piece.o scene.o moc_mainwindow.o -L/usr/local/Trolltech/Qt-4.2.0-snapshot-20060807/lib -lQtGui -L/home/adrian/Desktop/qt-x11-opensource-src-4.2.0-snapshot-20060807/qt-x11-opensource-src-4.2.0-snapshot-20060807/lib -L/usr/X11R6/lib -lpng -lSM -lICE -lXi -lXrender -lXrandr -lXfixes -lXcurs or -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -ldl -lpthread mainwindow.o: In function `MainWindow::MainWindow()':mainwindow.cpp:(.text+0 x198): undefined reference to `vtable for Scene'
mainwindow.o: In function `MainWindow::MainWindow()':mainwindow.cpp:(.text+0 x7cc ): undefined reference to `vtable for Scene'
collect2: ld gab 1 als Ende-Status zurück

jacek
22nd August 2006, 12:10
It looks like you didn't run moc on scene.h. If you use qmake, re-run it to generate a new makefile (you have to do this every time you add Q_OBJECT somewhere).

grosem
22nd August 2006, 12:18
You're right. I was using QIde and was not thinking about this. Thank you :)

By the way, I'm not allowed to post in the Feedback forum so maybe I can write it here (sorry): When I want to reply to a topic but I'm not logged in I am asked to log in and after this the forum wants me to open this site:

http://www.qtcentre.org/forum//forum//forum/newreply.php?do=newreply&noquote=1&p=18444

Of course this page can't be loaded. Just wanted to let you know.

jacek
22nd August 2006, 12:26
When I want to reply to a topic but I'm not logged in I am asked to log in and after this the forum wants me to open this site: [...]
We are aware of this. It should go away after we upgrade vRewrite.

grosem
22nd August 2006, 18:01
I have another question (thanks for this great forum. I hope to give something back when I'm able to):

I have subclassed QGraphicsPixmapItem:



class Pieceitem: public QGraphicsPixmapItem
{
Q_OBJECT
public:

void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )


};


You see I've only reimplemented a virtual function.
Then I try to add some objects of the new class to add to the QGraphicsScene:



Pieceitem *a= new Pieceitem;
QGraphicsPixmapItem *b = new QGraphicsPixmapItem;
b=a; //I can't add a Pieceitem, that's why I thought I let the QGraphicsPixmapItem
//point on the Pieceitem. But this is actually a weird plan and cannot be right...
scene->addItem(b);



This is the error I get the following error:


mainwindow.cpp: In constructor »MainWindow::MainWindow()«:
mainwindow.cpp:62: Fehler: »Pieceitem*« kann nicht nach »QGraphicsPixmapItem*« in assignment umgewandelt werden


That's actually not a Qt problem, but a big gap at my C++ knowledge :crying:

So I want to reimplement a virtual function of QGraphicsPixmapItem and use the objects of the new class just as I used a normal QGraphicsPixmapItem.

jacek
22nd August 2006, 19:53
Since Pieceitem inherits QGraphicsPixmapItem, this should work:

Pieceitem *item = new Pieceitem(...);
_scene->addItem( item );

grosem
22nd August 2006, 20:52
OK, actually I trid this first. It could not work because of some wrong headers I included. Next time I'll look more carefully before posting here. But the next problem has already arrived:



#ifndef PIECEITEM_H
#define PIECEITEM_H

#include <QGraphicsPixmapItem>

class Pieceitem: public QGraphicsPixmapItem
{
Q_OBJECT
public:

// void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );

};
#endif

with the error (the cpp file for this header is empty):


moc_pieceitem.cpp:37: Fehler: »staticMetaObject« ist kein Element von »QGraphicsPixmapItem«
moc_pieceitem.cpp: In member function »virtual void* Pieceitem::qt_metacast(const char*)«:
moc_pieceitem.cpp:51: Fehler: »qt_metacast« ist kein Element von »QGraphicsPixmapItem«
moc_pieceitem.cpp: In member function »virtual int Pieceitem::qt_metacall(QMetaObject::Call, int, void**)«:
moc_pieceitem.cpp:56: Fehler: »qt_metacall« ist kein Element von »QGraphicsPixmapItem«


I'm quite confused as there is not much to do wrong, but apparently I did :crying:

jacek
22nd August 2006, 21:00
The problem is that QGraphicsPixmapItem is not derived from QObject, so you can't use Q_OBJECT macro. If you want Pieceitem to be also a QObject (to be able to define new slots or signals inside it), you must use multiple inheritance.

grosem
22nd August 2006, 21:48
That worked, thank you very much. :)