Results 1 to 5 of 5

Thread: How to draw polyline using vector computed from another class

  1. #1
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question How to draw polyline using vector computed from another class

    Hi,

    I am having trouble draw a polyline from a vector of QPointF which is calculated in another class "getAvgHighTempVec".

    Here is my code:

    Qt Code:
    1. // main.cpp
    2. size_t qpoint_size = c.getAvgHighTempVec().size();
    3. QVector<QPointF> points(qpoint_size);
    4.  
    5. for(size_t i=0; i < qpoint_size; i++){
    6. points[i].setX(i);
    7. points[i].setY(c.getAvgHighTempVec().at(i));
    8. }
    9.  
    10. weatherStationGUI.draw(points);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //weatherstationgui.h
    2. namespace Ui {
    3. class weatherstationgui;
    4. }
    5. class weatherstationgui : public QMainWindow
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. explicit weatherstationgui(QWidget *parent = 0);
    11. ~weatherstationgui();
    12.  
    13. void draw( QVector<QPointF> dataIn);
    14.  
    15. private:
    16. Ui::weatherstationgui *ui;
    17.  
    18. QVector<QPointF> wellData;
    19.  
    20. protected:
    21. // paint method
    22. void paintEvent(QPaintEvent *event);
    23. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //weatherstationgui.cpp
    2. void weatherstationgui::draw(QVector<QPointF> dataIn){
    3. wellData = dataIn;
    4. update();
    5. }
    6.  
    7. void weatherstationgui::paintEvent(QPaintEvent *event){
    8.  
    9. // create a painter
    10. QPainter painter(this);
    11.  
    12. painter.drawPolyline(wellData.data(), static_cast<int>(wellData.size()));
    13. }
    To copy to clipboard, switch view to plain text mode 

    In the debugger I can see 'wellData' indeed has the correct QPointF points values, but it just doesn't draw on the main window.

    Is there anything wrong with my code?

    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to draw polyline using vector computed from another class

    What are typical values in the points array? As you have presented it these values will be treated as equivalent to pixel coordinates.

  3. #3
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to draw polyline using vector computed from another class

    yes, you are absolutely right. The values stored in the vector points are the coordinates. It looks like { (1.6, 0) , (2.3, 1) ...} in debugger.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to draw polyline using vector computed from another class

    Your points are being drawn in a few pixels in the top left of the window. You need to scale the values yourself or use a transform on the painter to do it for you.
    See Coordinate transforms

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

    PXMYH (18th November 2013)

  6. #5
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to draw polyline using vector computed from another class

    Thank you Chris! its been there all the time but I chose to ignore it

Similar Threads

  1. Qwtplot3D : plot 3D polyline
    By franchouze in forum Qwt
    Replies: 0
    Last Post: 16th January 2013, 16:01
  2. Replies: 2
    Last Post: 2nd July 2011, 01:02
  3. vector with own class
    By T0bi4s in forum Qt Programming
    Replies: 13
    Last Post: 14th January 2010, 23:06
  4. draw a polyline
    By bonics in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2007, 10:51
  5. QRubberBand class to draw a line
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 3rd April 2007, 06:53

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
  •  
Qt is a trademark of The Qt Company.