Results 1 to 6 of 6

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

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

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

    Greetings,

    I would like to know if it is possible to draw a line with tiled images as a background on it. I have tried to use QPen (width=20) with QBrush and Qt:TexturePattern, but this did not work correctly. Is there another way?

    Thank you,
    MadBear

  2. #2
    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?

    try using QPen width 0 and QBrush::TexturePattern

    Quote Originally Posted by MadBear
    ...but this did not work correctly. Is there another way?
    What you mean by this, show the sample output, so that someone can point out the problem.

  3. #3
    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. Ok I will try to explain what I am trying to do. I must create QGraphicsItem that is like police tape (you see it in crime scenes) that prevents civilians to cross (it is a tape that says "POLICE - STOP" in constant intervals). My idea is that user clicks control points and then I create QPainterPath with lines through those control points. Control points can also be moved around QGraphicsScene. The problem is that line betwen control points. How can I create line that looks like that police tape. I have tried with texture for QBrush, but that created something like:

    PO
    LI
    CE
    -
    ST
    OP

    Is there perhaps a different way how I can create this?

    Thank you,
    MadBear

  4. #4
    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

  5. #5
    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

  6. #6
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.