Results 1 to 6 of 6

Thread: How to draw a line with tiled images with QPainter?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to draw a line with tiled images with QPainter?

    Will this work



    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. , Ui::MainWindow()
    4. {
    5. setupUi(this);
    6.  
    7. QGraphicsScene * scene = new QGraphicsScene(this);
    8. graphicsView->setScene(scene);
    9.  
    10. QPoint point1(0, 0);
    11. QPoint point2(1000, 0);
    12.  
    13. addBanner(scene, point1, point2, "POLICE - STOP");
    14. }
    15.  
    16. void MainWindow::addBanner(QGraphicsScene * scene
    17. , const QPoint & start
    18. , const QPoint & end
    19. , const QString & text) const
    20. {
    21. const int height = 25;
    22. const int spaceing = 40;
    23. QPen pen(Qt::black, 2, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
    24. QBrush brush(Qt::yellow);
    25. QFont font("Arial", 12);
    26. QPoint pos(start);
    27.  
    28. QRect rect = QRect(start, QPoint(end.x(), end.y() + height));
    29. QGraphicsRectItem * rect_item = scene->addRect(rect ,pen, brush);
    30. rect_item->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
    31.  
    32. while(pos.x() < end.x())
    33. {
    34. QGraphicsTextItem * item = new QGraphicsTextItem(text, rect_item, scene);
    35. item->setFont(font);
    36. item->setPos(pos);
    37. item->clipPath();
    38. pos.setX(pos.x() + spaceing + item->boundingRect().right());
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Aug 2010
    Posts
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to draw a line with tiled images with QPainter?

    Greetings,
    Thank you for your reply. But it does not work quite the way I wanted it. I did not notice, that this forum erased all my spaces in my example. So that was supposed to be a diagonal line (not vertical). And when I changed point2 to (1000, 30) the program didn't work anymore.
    Thank you again.

    Regards,
    MadBear

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to draw a line with tiled images with QPainter?

    The example I post will work only for horizontal banners, you need to modify it for drawing to diagonal, using QPainterPath (you cannot use QGraphicsRectItem and QGraphicsTextItem)

    I was only trying to give an example

Similar Threads

  1. Draw line on image using QPainter
    By Qt Coder in forum Qt Programming
    Replies: 29
    Last Post: 11th August 2015, 12:09
  2. Draw Line
    By sagirahmed in forum Newbie
    Replies: 5
    Last Post: 18th October 2010, 07:49
  3. To draw images in QLabel
    By augusbas in forum Qt Programming
    Replies: 5
    Last Post: 11th October 2010, 06:27
  4. Qt Draw fast png images
    By anafor2004 in forum Newbie
    Replies: 4
    Last Post: 4th November 2008, 09:28
  5. GDAL+QT, tiled geotiff
    By Nithya in forum Qt Programming
    Replies: 0
    Last Post: 23rd April 2008, 12:47

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.