PDA

View Full Version : How to draw a line with tiled images with QPainter?



MadBear
22nd July 2011, 07:04
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

Santosh Reddy
22nd July 2011, 08:45
try using QPen width 0 and QBrush::TexturePattern


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

MadBear
23rd July 2011, 10:52
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

Santosh Reddy
24th July 2011, 03:46
Will this work




MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, Ui::MainWindow()
{
setupUi(this);

QGraphicsScene * scene = new QGraphicsScene(this);
graphicsView->setScene(scene);

QPoint point1(0, 0);
QPoint point2(1000, 0);

addBanner(scene, point1, point2, "POLICE - STOP");
}

void MainWindow::addBanner(QGraphicsScene * scene
, const QPoint & start
, const QPoint & end
, const QString & text) const
{
const int height = 25;
const int spaceing = 40;
QPen pen(Qt::black, 2, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
QBrush brush(Qt::yellow);
QFont font("Arial", 12);
QPoint pos(start);

QRect rect = QRect(start, QPoint(end.x(), end.y() + height));
QGraphicsRectItem * rect_item = scene->addRect(rect ,pen, brush);
rect_item->setFlag(QGraphicsItem::ItemClipsChildrenToShape);

while(pos.x() < end.x())
{
QGraphicsTextItem * item = new QGraphicsTextItem(text, rect_item, scene);
item->setFont(font);
item->setPos(pos);
item->clipPath();
pos.setX(pos.x() + spaceing + item->boundingRect().right());
}
}

MadBear
25th July 2011, 05:52
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

Santosh Reddy
25th July 2011, 06:36
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 :)