Results 1 to 10 of 10

Thread: QGraphicsView in a QDialog?

  1. #1
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView in a QDialog?

    I am trying to create and display a QPixmap using a QGraphicsView in a QDialog as follows:
    Qt Code:
    1. void ViewerDialog::paintEvent(QPaintEvent *e)
    2. {
    3. QPixmap pixmap(ui->view->size());
    4. QPainter painter(&pixmap);
    5. QGraphicsScene scene(ui->view);
    6. scene.setSceneRect(ui->view->rect());
    7. vector<SyDataT>::iterator it;
    8. for (it = sy->syVector.begin(); it < sy->syVector.end(); it++)
    9. {
    10. if ((pl == it->pl) && (fr == it->fr))
    11. {
    12. int rsi = it->rsi;
    13. if ((rsi > -10) || (rsi < -130))
    14. {
    15. rsi = -130;
    16. }
    17. int colorMax = 255;
    18. int red = 0;
    19. if (rsiMax > rsiMin)
    20. {
    21. red = colorMax*(rsi-rsiMin)/(rsiMax-rsiMin);
    22. }
    23. int green = colorMax - red;
    24. int blue = 0;
    25. int alpha = colorMax;
    26. QColor color(red, green, blue, alpha);
    27. painter.setPen(color);
    28. painter.drawPoint(it->az, it->el);
    29. }
    30. scene.addPixmap(pixmap);
    31. }
    32. if (rsiMax == rsiMin)
    33. {
    34. scene.addText("No rsi");
    35. }
    36. ui->view->setScene(&scene);
    37. ui->view->show();
    38. }
    To copy to clipboard, switch view to plain text mode 
    I can see an expected image on the QDialog with "QPainter painter(this);" but I'd like to get the image into the QGraphicsView. What am I missing?

    Also: I know there are a number of things that can trigger a paintEvent, but it is being called continuously when nothing is happening. Any idea why?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsView in a QDialog?

    This is so very very wrong :-)

    Qt Code:
    1. ui->view->setScene(&scene);
    2. ui->view->show();
    To copy to clipboard, switch view to plain text mode 

    Among other lines, this will at least put your paint event in an infinite loop.

    And from what it looks like, you don't even need to use the paint event of your dialog.
    Why use a graphics view and start painting yourself?

    The only thing and nothing else you should be doing in the paint event is painting.

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

    jnadelman (3rd June 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView in a QDialog?

    Thanks! That explains the loop from hell: setScene() and show() were causing paintEvents in the paintEvent.
    But I still do not understand how to get the QPixmap into the QGraphicsView.
    I moved QGraphicsView and QGraphicsScene out of the paintEvent so that they only get called once.
    I moved QPixmap so it is available to the class.
    I got rid of the view->show() since I do not think it is needed.
    But I still do not see the image in the QGraphicsView.
    Am I getting closer at least?

    Qt Code:
    1. ViewerDialog::ViewerDialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::ViewerDialog)
    4. {
    5. ui->setupUi(this);
    6. ...
    7. pixmap = new QPixmap(ui->view->size());
    8. scene = new QGraphicsScene(ui->view);
    9. }
    10. ...
    11. void ViewerDialog::updateUi()
    12. {
    13. ...
    14. scene->setSceneRect(ui->view->rect());
    15. ui->view->setScene(scene);
    16. scene->addPixmap(*pixmap);
    17. }
    18. ...
    19. void ViewerDialog::paintEvent(QPaintEvent *e)
    20. {
    21. QPainter painter(pixmap);
    22. vector<SyDataT>::iterator it;
    23. for (it = sy->syVector.begin(); it < sy->syVector.end(); it++)
    24. {
    25. if ((pl == it->pl) && (fr == it->fr))
    26. {
    27. ...
    28. QColor color(red, green, blue, alpha);
    29. painter.setPen(color);
    30. painter.drawPoint(it->az, it->el);
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsView in a QDialog?

    Yep, you're getting closer, but you still don't understand the paint event.

    Qt Code:
    1. void ViewerDialog::paintEvent(QPaintEvent *e)
    To copy to clipboard, switch view to plain text mode 
    This is the paint event of your dialog.

    In this paint event, all you do now is paint on a pixmap, nothing else.
    Hence, when you display the dialog, nothing will be shown.

    Do not use the paint event.
    Create another function that creates the pixmap. Don't do this in the paint event.

  6. The following user says thank you to tbscope for this useful post:

    jnadelman (3rd June 2010)

  7. #5
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView in a QDialog?

    Thanks--that was it! What was confusing me was the Qt help text:
    Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent()...
    Thanks again.

  8. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView in a QDialog?

    You can go for QGraphicsPixnapItem... you need to add the item in a scene.
    Set the scene to a view, and show the view . Thats it.

    But what special case u have that you need to show pixmap in a graphics view only ?

  9. The following user says thank you to aamer4yu for this useful post:

    jnadelman (3rd June 2010)

  10. #7
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView in a QDialog?

    Don't think it is a special case. I was working with Qt Designer and needed a QDialog for mapping some of the data from a csv file to an image based on user selections. It was easy enough to add the QGraphicsView to the QDialog in Qt Designer. It just wasn't clear how to do it properly. Now I need to look at ways to modify the QPixmap in the QGraphicsView such as zoom, interpolate, etc.

    Quote Originally Posted by aamer4yu View Post
    You can go for QGraphicsPixnapItem... you need to add the item in a scene.
    Set the scene to a view, and show the view . Thats it.

    But what special case u have that you need to show pixmap in a graphics view only ?

  11. #8
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView in a QDialog?

    What would prevent
    Qt Code:
    1. ...
    2. view = new QGraphicsView(layoutWidget);
    3. ...
    4. pixmap = new QPixmap(ui->view->size());
    5. scene = new QGraphicsScene(ui->view);
    6. ...
    7. pixmap->scaled(ui->view->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    8. ui->view->setScene(scene);
    9. scene->addPixmap(*pixmap);
    To copy to clipboard, switch view to plain text mode 
    from scaling pixmap to ui->view->size()?
    I don't see anything in QPixmap like setScaledEnabled.

  12. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsView in a QDialog?

    scaled returns the scaled pixmap, it doesn't perform the scaling on the pixmap ;-)

  13. The following user says thank you to tbscope for this useful post:

    jnadelman (3rd June 2010)

  14. #10
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView in a QDialog?

    Sorry. I've been looking at this too long. Got a nice image now... Thanks a million.
    Quote Originally Posted by tbscope View Post
    scaled returns the scaled pixmap, it doesn't perform the scaling on the pixmap ;-)

Similar Threads

  1. QDialog
    By jayreddy in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2009, 09:51
  2. closing a Qdialog called from a Qdialog
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 08:02
  3. QDialog
    By Carlsberg in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2009, 09:08
  4. How to "lock" a new qdialog in another qdialog
    By donglebob in forum Qt Programming
    Replies: 7
    Last Post: 4th February 2009, 08:37
  5. QDialog
    By sonuani in forum Newbie
    Replies: 1
    Last Post: 5th March 2008, 06:40

Tags for this Thread

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.