Results 1 to 6 of 6

Thread: Draw Line: QGraphicsView

  1. #1
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Draw Line: QGraphicsView

    I'm trying to draw a line based on the points selected using the mouse double click event.
    Mainwindow is as below:
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8.  
    9. private:
    10. Ui::MainWindow *ui;
    11.  
    12. public slots:
    13. void DrawLine(int x1, int y1, int x2, int y2);
    14.  
    15. private slots:
    16. //This method to be used to draw the line after selecting the points.
    17. void on_pushButton_clicked();
    18.  
    19. private:
    20. GraphicsView *view;
    21. QGraphicsPixmapItem *pixmapItem;
    22. QPixmap *pixmap;
    23. };
    24.  
    25. Image is displayed in the mainwindow:
    26.  
    27. view = new GraphicsView();
    28. scene = new QGraphicsScene(0, 0, 800, 600);
    29. pixmapItem = new QGraphicsPixmapItem();
    30. pixmap = new QPixmap(800,600);
    31. QImage img;
    32. img.load("D:/NPOL_Test_Modules/Qt/4.7.1/QGraphicsViewMouseEvent/Winter.jpg"))
    33. QPainter painter;
    34. painter.begin(pixmap);
    35. painter.drawImage(0,0,img);
    36. painter.end();
    37.  
    38. pixmapItem->setPixmap(*pixmap);
    39. scene->addItem(pixmapItem);
    40. view->setScene(scene);
    41.  
    42. connect(view, SIGNAL(PointSelected(int,int,int,int)), this, SLOT(DrawLine(int,int,int,int)));
    To copy to clipboard, switch view to plain text mode 

    GraphicsView is implemented as below:

    Qt Code:
    1. class GraphicsView: public QGraphicsView
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit GraphicsView();
    7.  
    8. protected:
    9. void mouseDoubleClickEvent(QMouseEvent *event);
    10.  
    11. signals:
    12. void PointSelected(int a, int b, int c, int d);
    13.  
    14. private:
    15. int x1, y1, x2, y2;
    16. };
    17.  
    18. mouseDoubleClickEvent is as below
    19.  
    20. static int pCount = 0;
    21. if(event->type() == QMouseEvent::MouseButtonDblClick)
    22. {
    23. event->accept();
    24. pCount++;
    25. if(pCount == 1)
    26. {
    27. x1 = QCursor::pos().x();
    28. y1 = QCursor::pos().y();
    29. }
    30. else if(pCount == 2)
    31. {
    32. pCount = 0;
    33. x2 = QCursor::pos().x();
    34. y2 = QCursor::pos().y();
    35. emit PointSelected(x1, y1, x2, y2);
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    I'm not able to drawline... I have tried using QPainter as below in the DrawLine function of Mainwindow.

    Qt Code:
    1. QPainter painter;
    2. painter.begin(pixmap);
    3. painter.drawLine(x1, y1, x2, y2);
    4. painter.end();
    To copy to clipboard, switch view to plain text mode 

    Please let me know if my approach is right.
    My intension is to draw the line on the image once points are selected using the mouse event.
    I'll be thankfull if any one can please guide on how I can achieve this.

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

    Default Re: Draw Line: QGraphicsView

    Your approach is wrong. If you are using Graphics View then use QGraphicsScene::addLine() to add a line to the scene. And don't use QCursor::pos(), instead use QGraphicsSceneMouseEvent::scenePos() you get in the event handler.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    vinaykusta (3rd January 2012)

  4. #3
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Draw Line: QGraphicsView

    Thank you for the guidance.

    I'm now able to draw the line.

    I have a scenario where my image being displayed keep changing. A sequence of image.
    Qt Code:
    1. // Set Up is as below
    2. pixmap = new QPixmap(800, 600);
    3. pixmap->fill(Qt::black);
    4. pixmapItem = new QGraphicsPixmapItem();
    5.  
    6. scene = new QGraphicsScene(0,0,798,598);
    7. scene->addItem(pixmapItem);
    8.  
    9. ui->liveFeedView = new QGraphicsView();
    10. ui->liveFeedView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    Furtherm, Image update is done periodically as below :

    Qt Code:
    1. // Load image data in the buffer to a QImage object and udpate the pixmap area
    2. // Set the QGraphicsView to display QGraphicsScene updated with an updated graphics item
    3. if(rawImg.loadFromData(buf->imgData[i]))
    4. {
    5. QPainter painter;
    6. painter.begin(pixmap);
    7. painter.drawImage(225,85,rawImg);
    8. painter.end();
    9. pixmapItem->setPixmap(*pixmap);
    10.  
    11. ui->liveFeedView->setScene(scene);
    12. }
    To copy to clipboard, switch view to plain text mode 

    As suggested, In case I use QGraphicsScene addline function... I think I might loose the line on every update.
    In such case, what can be another alternative.
    How can I achieve this line drawing and retain the line, even though the image keeps changing or refreshing.

    Even in the above case of frequent update to image display, I would like to know if its the correct approach.
    Thank you very much.

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

    Default Re: Draw Line: QGraphicsView

    If you update just the existing pixmap item, you will not loose anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Draw Line: QGraphicsView

    Thank you again.
    Please guide on How to clear/erase the line ?

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

    Default Re: Draw Line: QGraphicsView

    Quote Originally Posted by vinaykusta View Post
    Please guide on How to clear/erase the line ?
    The line is represented by the item you have added to the scene by calling QGraphicsScene::addLine(). If you delete the object, the line will go away. I suggest you read a bit about Graphics View before you start using it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. To draw a line
    By vinayaka in forum Qt Quick
    Replies: 1
    Last Post: 6th June 2011, 10:53
  2. Draw Line
    By sagirahmed in forum Newbie
    Replies: 5
    Last Post: 18th October 2010, 07:49
  3. Draw a line
    By Daan in forum KDE Forum
    Replies: 1
    Last Post: 27th August 2009, 17:29
  4. Draw Line
    By aloysiusjegan in forum Qwt
    Replies: 4
    Last Post: 12th February 2009, 11:02
  5. Best way to draw a Line
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2008, 09: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
  •  
Qt is a trademark of The Qt Company.