Results 1 to 17 of 17

Thread: Examples of QGraphicsView

  1. #1
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Examples of QGraphicsView

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsEllipseItem>
    3. #include <QGraphicsScene>
    4. #include <QGraphicsView>
    5.  
    6. int main( int argc, char **argv )
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    11.  
    12. QGraphicsEllipseItem *item = new QGraphicsEllipseItem( 0, &scene );
    13. item->setRect( -50.0, -50.0, 100.0, 100.0 );
    14.  
    15. QGraphicsView view( &scene );
    16. view.setRenderHints( QPainter::Antialiasing );
    17. view.show();
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 7 users say thank you to jacek for this useful post:

    Ade Malsasa Akbar (31st May 2013), grosem (26th July 2006), hasenj (6th June 2010), Iderval (7th June 2011), sector (22nd August 2006), Thanatos (15th October 2010), vinayaka (16th June 2011)

  4. #3
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    Thanks
    I changed some details for me and it works :-)

  5. #4
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Examples of QGraphicsView

    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.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    Qt Code:
    1. item->setFlag( QGraphicsItem::ItemIsMovable );
    To copy to clipboard, switch view to plain text mode 

  7. The following 3 users say thank you to jacek for this useful post:

    grosem (20th August 2006), hasenj (6th June 2010), Thanatos (15th October 2010)

  8. #6
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    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?

  9. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Examples of QGraphicsView

    Quote Originally Posted by grosem
    Is there a way to avoid this scroll bar?
    Set both scrollbar policies to Qt::ScrollBarAlwaysOff.
    void setHorizontalScrollBarPolicy ( Qt::ScrollBarPolicy )
    void setVerticalScrollBarPolicy ( Qt::ScrollBarPolicy )
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    grosem (20th August 2006)

  11. #8
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Examples of QGraphicsView

    Well,
    I described my problem in a wrong way, but solved it now myself.
    Nevertheless: Thank you

  12. #9
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    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
    Qt Code:
    1. #ifndef SCENE_H
    2. #define SCENE_H
    3.  
    4. #include <QGraphicsScene>
    5.  
    6. class Scene: public QGraphicsScene
    7. {
    8. Q_OBJECT
    9. public:
    10.  
    11. void dragLeaveEvent ( QGraphicsSceneDragDropEvent * event );
    12.  
    13.  
    14. };
    15.  
    16. #endif
    To copy to clipboard, switch view to plain text mode 

    scene.cpp
    Qt Code:
    1. #include "scene.h"
    2.  
    3. void Scene::dragLeaveEvent ( QGraphicsSceneDragDropEvent * event )
    4. {
    5. qDebug("dragLeaveEvent");
    6. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by jacek; 22nd August 2006 at 12:50.

  13. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    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).

  14. The following user says thank you to jacek for this useful post:

    grosem (22nd August 2006)

  15. #11
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    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...uote=1&p=18444

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

  16. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    Quote Originally Posted by grosem
    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.

  17. #13
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    I have another question (thanks for this great forum. I hope to give something back when I'm able to):

    I have subclassed QGraphicsPixmapItem:

    Qt Code:
    1. class Pieceitem: public QGraphicsPixmapItem
    2. {
    3. Q_OBJECT
    4. public:
    5.  
    6. void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
    7.  
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. Pieceitem *a= new Pieceitem;
    2. b=a; //I can't add a Pieceitem, that's why I thought I let the QGraphicsPixmapItem
    3. //point on the Pieceitem. But this is actually a weird plan and cannot be right...
    4. scene->addItem(b);
    To copy to clipboard, switch view to plain text mode 

    This is the error I get the following error:

    Qt Code:
    1. mainwindow.cpp: In constructor »MainWindow::MainWindow()«:
    2. mainwindow.cpp:62: Fehler: »Pieceitem*« kann nicht nach »QGraphicsPixmapItem*« in assignment umgewandelt werden
    To copy to clipboard, switch view to plain text mode 

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

    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.

  18. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    Since Pieceitem inherits QGraphicsPixmapItem, this should work:
    Qt Code:
    1. Pieceitem *item = new Pieceitem(...);
    2. _scene->addItem( item );
    To copy to clipboard, switch view to plain text mode 

  19. #15
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Examples of QGraphicsView

    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:

    Qt Code:
    1. #ifndef PIECEITEM_H
    2. #define PIECEITEM_H
    3.  
    4. #include <QGraphicsPixmapItem>
    5.  
    6. class Pieceitem: public QGraphicsPixmapItem
    7. {
    8. Q_OBJECT
    9. public:
    10.  
    11. // void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
    12.  
    13. };
    14. #endif
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. moc_pieceitem.cpp:37: Fehler: »staticMetaObject« ist kein Element von »QGraphicsPixmapItem«
    2. moc_pieceitem.cpp: In member function »virtual void* Pieceitem::qt_metacast(const char*)«:
    3. moc_pieceitem.cpp:51: Fehler: »qt_metacast« ist kein Element von »QGraphicsPixmapItem«
    4. moc_pieceitem.cpp: In member function »virtual int Pieceitem::qt_metacall(QMetaObject::Call, int, void**)«:
    5. moc_pieceitem.cpp:56: Fehler: »qt_metacall« ist kein Element von »QGraphicsPixmapItem«
    To copy to clipboard, switch view to plain text mode 

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

  20. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    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.

  21. The following 2 users say thank you to jacek for this useful post:

    Bryku (9th December 2009), grosem (22nd August 2006)

  22. #17
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Examples of QGraphicsView

    That worked, thank you very much.

Similar Threads

  1. QCanvas Examples
    By Kapil in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 14:12
  2. Where are tutorial 2 code examples?
    By Mariane in forum Newbie
    Replies: 6
    Last Post: 25th January 2006, 16:02
  3. Qt 4.1.0 - static examples run with errors!
    By Elder Orb in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2006, 10:40
  4. Qt 3.x Examples
    By ball in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 14:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.