Results 1 to 4 of 4

Thread: strange problem in QGraphicsView

  1. #1
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default strange problem in QGraphicsView

    Qt Code:
    1. test::test(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5.  
    6. QString str = "test";
    7. pixTrans.load("Resources/Trans.bmp");
    8.  
    9. view.setScene(&scene);
    10. setCentralWidget(&view);
    11.  
    12. showText(&str,0,0);
    13. showItem(&pixTrans,0,0);
    14. showText(&str,0,100);
    15. showItem(&pixTrans,0,100);
    16. }
    17.  
    18. void test::showItem(QPixmap *pixmap, int x, int y)
    19. {
    20. QGraphicsPixmapItem* i = scene.addPixmap(*pixmap);
    21. i->setPos(x,y);
    22. }
    23. void test::showText(QString *str, int x, int y)
    24. {
    25. QGraphicsTextItem* i = scene.addText(*str);
    26. i->setPos(x, y);
    27. }
    To copy to clipboard, switch view to plain text mode 
    the result is so strange:
    the 1st text "test" at (0,0) is covered by the pixmap "pixTrans"
    while the 2nd text "test" at (0,0) is on top of the pixmap "pixTrans"
    I want to show text on top of pixmap at the same coordinate (x,y), is there any way to do this ?
    thanks very much for suggestion!
    Attached Images Attached Images
    Last edited by Shawn; 29th June 2007 at 08:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: strange problem in QGraphicsView

    I'm not sure what you mean by "at the same coordinate" but you're probably looking for QGraphicsItem::setZValue().

  3. The following user says thank you to wysota for this useful post:

    Shawn (29th June 2007)

  4. #3
    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: strange problem in QGraphicsView

    By the way, QMainWindow::setCentralWidget() takes ownership of the widget and deletes it at the appropriate time so passing a pointer to widget allocated on stack leads to crash by the time the main window is destructed.
    J-P Nurmi

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

    Shawn (29th June 2007)

  6. #4
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: strange problem in QGraphicsView

    Thank you very much wysota !
    I check the reference for QGraphicsItem::setZValue(), and find out where my problem exist:
    "Sibling items that share the same Z-value will be drawn in an undefined order, although the order will stay the same for as long as the items live."
    I have tried to find some member function like setTop() or somthing in QGraphicsView with no luck, now I see that the "order" is controled by ZValue, in QGraphicsItem. Thank you very much again!

    I set ZValue in showItem() and showText(), and it works well !
    Qt Code:
    1. void test::showItem(QPixmap *pixmap, int x, int y)
    2. {
    3. QGraphicsPixmapItem* i = scene.addPixmap(*pixmap);
    4. i->setPos(x,y);
    5. i->setZValue(0);
    6. }
    7. void test::showText(QString *str, int x, int y)
    8. {
    9. QGraphicsTextItem* i = scene.addText(*str);
    10. i->setPos(x, y);
    11. i->setZValue(1);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 29th June 2007 at 21:46. Reason: changed [html] to [quote]

Similar Threads

  1. QGraphicsView scrolling problem with 4.3.0
    By hb in forum Qt Programming
    Replies: 8
    Last Post: 30th August 2007, 22:18
  2. Strange shortcut problem
    By blukske in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2007, 10:26
  3. Strange problem with events on Unix
    By sukanyarn in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2006, 02:45
  4. Strange Problem with JPEG Support on win XP
    By caligula in forum Installation and Deployment
    Replies: 3
    Last Post: 18th September 2006, 10:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.