Results 1 to 20 of 35

Thread: Painting lines

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Painting lines

    hey there i am trying to create links between nodes that i have made but theres 1 problem. everytime i paint a link the last link that i painted onto the qpainter goes away. how can i keep the links that have been painted before on the link? i store the node Id's in an array and their position with a struct. so i just get a user input as to where they want the link to be created. my code for the painting is shown below:

    Qt Code:
    1. if( drawLine == 1)
    2. {
    3. painter.setPen(myTextColor());
    4. painter.drawLine( list.position[n1]+offset, list.position[n2]+offset );
    5. painter.drawText(QRect(midpoint,textbox),0,tr("%1").arg(topology[n1][n2]));
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    i also have a capacity number that i wish to appear between the links when they get painted.

  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: Painting lines

    Where do you draw those lines? In paintEvent()?

  3. #3
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting lines

    yeah in Paintevent. actaully it doesnt really matter where they get painted as long as they do!

  4. #4
    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: Painting lines

    How many lines do you paint in single call to paintEvent()?

  5. #5
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting lines

    just the lines that have been created so far. so for example the nodes are created first and then the user presses a button to create the link. the link then gets created between the nodes that the user specifies and then the user can then go on to create the next link. i just want the links created so far to be printed...

  6. #6
    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: Painting lines

    Could you show more code from your paintEvent()?

  7. #7
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting lines

    ok sure here is the code from the QPaintEvent and below it is the function that gets the user input:

    Qt Code:
    1. void SortingBox::paintEvent(QPaintEvent * /* event */)
    2. {
    3. QPainter painter(this);
    4. QPoint offset(7,13);
    5. int looplength=0;
    6. foreach (ShapeItem shapeItem, shapeItems)
    7. {
    8.  
    9. QPoint mid(-shapeItem.position());
    10. //QPoint offset(7,13);
    11. QPoint text(100,100);
    12.  
    13. painter.translate(shapeItem.position());
    14. painter.setBrush(shapeItem.color());
    15. painter.drawPath(shapeItem.path());
    16. painter.translate(-shapeItem.position());
    17. painter.setPen(initialItemColor());
    18. painter.drawText((shapeItem.position() + offset),tr("%1").arg(shapeItem.instanceNumber()));
    19.  
    20.  
    21. list.position[shapeItem.instanceNumber()] = shapeItem.position();
    22. list.nodeID[shapeItem.instanceNumber()] = shapeItem.instanceNumber();
    23. looplength = shapeItem.instanceNumber();
    24. }
    25.  
    26. QPoint midpoint;
    27. midpoint = ((list.position[n1] + list.position[n2])/2);
    28. QPoint test(100,100);
    29. QSize textbox(100,100);
    30.  
    31.  
    32.  
    33. if( drawLine == 1)
    34. {
    35. painter.setPen(myTextColor());
    36. for(int j=0;j<looplength;j++)
    37. /*{
    38. if(list.nodeID[j]>0 && list.nodeID[j]<100)
    39. {
    40. painter.drawLine( list.position[list.nodeID[j]]+offset, list.position[list.nodeID[j+1]]+offset );
    41. }
    42.  
    43.  
    44. }*/
    45.  
    46. painter.drawLine( list.position[n1]+offset, list.position[n2]+offset );
    47. painter.drawText(QRect(midpoint,textbox),0,tr("%1").arg(topology[n1][n2]));
    48.  
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 

    function user input( )

    Qt Code:
    1. void SortingBox::user_input()
    2. {
    3. drawLine = 1;
    4.  
    5.  
    6. QLabel *integerLabel = new QLabel(this);
    7. bool ok;
    8. n1 = QInputDialog::getInteger(this, tr("QInputDialog::getInteger()"),
    9. tr("enter first node to link:"), 25, 0, 100, 1, &ok);
    10.  
    11. if(ok)
    12. integerLabel->setText(tr("%1").arg(n1));
    13.  
    14. n2 = QInputDialog::getInteger(this, tr("QInputDialog::getInteger()"),
    15. tr("enter second node to link:"), 25, 0, 100, 1, &ok);
    16.  
    17. if(ok)
    18. integerLabel->setText(tr("%1").arg(n2));
    19.  
    20. topology[n1][n2] = QInputDialog::getInteger(this, tr("QInputDialog::getInteger()"),
    21. tr("required capacity:"), 25, 0, 100, 1, &ok);
    22.  
    23. if(ok)
    24. integerLabel->setText(tr("%1").arg(topology[n1][n2]));
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: Painting lines

    Quote Originally Posted by therealjag
    Qt Code:
    1. foreach (ShapeItem shapeItem, shapeItems)
    2. {
    3. //...
    4. }
    To copy to clipboard, switch view to plain text mode 
    What are these ShapeItems?

    Quote Originally Posted by therealjag
    Qt Code:
    1. for(int j=0;j<looplength;j++)
    2. /*{
    3. if(list.nodeID[j]>0 && list.nodeID[j]<100)
    4. {
    5. painter.drawLine( list.position[list.nodeID[j]]+offset, list.position[list.nodeID[j+1]]+offset );
    6. }
    7.  
    8.  
    9. }*/
    10.  
    11. painter.drawLine( list.position[n1]+offset, list.position[n2]+offset );
    12. painter.drawText(QRect(midpoint,textbox),0,tr("%1").arg(topology[n1][n2]));
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    Where do you change n1 and n2?

    Your paintEvent() should be something like this:
    Qt Code:
    1. for each edge in Edges:
    2. draw line from edge.startNode.point to edge.endNode.point
    3. draw text edge.label at midPoint( edge.startNode.point, edge.endNode.point )
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting lines

    Quote:
    Qt Code:
    foreach (ShapeItem shapeItem, shapeItems) { //...}

    What are these ShapeItems?
    the shapeItems are just my nodes that i have created. they arent linked in any programmable way like in a linked list or anything they have just been created individually.
    n1 and n2 get changed when the user enters their value so the first user input gets set to n1 and the second user input gets set to n2. the capacity for the link then gets asked for after that.

  10. #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: Painting lines

    Quote Originally Posted by therealjag
    n1 and n2 get changed when the user enters their value so the first user input gets set to n1 and the second user input gets set to n2. the capacity for the link then gets asked for after that.
    So in fact your paintEvent() draws only one edge?

  11. #11
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting lines

    yeah it only draws 1 edge at a time but is there a way to keep the older ones drawn on the screen?

Similar Threads

  1. how do i know if two lines intersect?
    By Vincenzo in forum Newbie
    Replies: 4
    Last Post: 7th November 2008, 14:23
  2. I want to get lines of text
    By newplayer in forum Qt Programming
    Replies: 11
    Last Post: 29th July 2008, 09:03
  3. Display QLabel in two lines
    By arunvv in forum Newbie
    Replies: 1
    Last Post: 8th February 2008, 05:25
  4. Slow painting in QGraphicsView
    By JonathanForQT4 in forum Qt Programming
    Replies: 12
    Last Post: 16th July 2007, 09:54
  5. How to draw lines with the right width?
    By albanelporto in forum Qt Programming
    Replies: 3
    Last Post: 22nd November 2006, 11:51

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.