Is there any other better approach - c++

QT Paintevent is not painting the required image, instead paints black screen. this paintevent is called for each time interval of 2 seconds using SLOT(update).
I have tried with triggering PaintImage method that is Implemented to draw QImage supplied, but didnt work either.
void Client::paintEvent(QPaintEvent *event)
{
//qWarning() << image.isNull() << image.width()<< image.height();
QPainter painter(&image);
painter.end();
QPixmap pixMap;//(image);
//painter.drawImage(1280, 639, image);
bool b = pixMap.convertFromImage(image);
QLabel w;
w.setPixmap(pixMap);
w.show();
}
Expected actual Image to be displayed but ending up blank screen.

Add QLabel as a member of class (Client) ... lets give it name m_my_label.
Then in paintEvent say just m_my_label.setPixmap(pixMap) and m_my_label.show().
Problem is that QLabel is local variable and will cease to exist when you go out of paintEvent function.

Related

how to delay drawing

I want to show the proccess of filling an area line by line. The problem is: even if I create QPixmap from QImage every time I draw a new line (and also delay) and add it to QGraphicsScene in a loop, it doesn't update until the whole thread is finished.
QTest::qWait does the trick. And using QGraphicsPixmapItem for an actual display of the proccess instead of QPixmap itself. I use QPainter initialized from QPixmap and then just reset the pixmap for the pixmap item.
QImage img = QImage(WIDTH, HEIGHT, QImage::Format_RGB32);
img.fill(Qt::white);
QPixmap *pixmapFill = new QPixmap(QPixmap::fromImage(img));
QGraphicsPixmapItem pixmapItem(*pixmapFill);
fillScene->addItem(&pixmapItem);
pixmapItem.setPixmap(*pixmapFill);
// ...
for (auto point : *points) {
QTest::qWait(delay);
painter.drawLine(point.x(), point.y(), xBarrier, point.y());
pixmapItem.setPixmap(*pixmapFill);
}

QPixmap, how to make sure it is 'always on top'

For Widgets I can call 'raise' which keeps the widget on top of anything else, but this doesn't seem to work for any QPixmap's that are rendered.
How can I ensure that a QPixmap remains on top of anything else?
In my paintEvent function:
QPainter objPainter(this);
if ( strImage.isEmpty() != true ) {
qint16 int16NudgeImageX = mpobjNode->int16GetAttr(clsXML::mscszAttrNudgeImageX)
,int16NudgeImageY = mpobjNode->int16GetAttr(clsXML::mscszAttrNudeImageY);
QPixmap pmImage(":/" + strImage);
QSize szImage = pmImage.size();
QPoint ptImage(rctGeom.center().x() - (szImage.width() / 2) + int16NudgeImageX
,rctGeom.center().y() - (szImage.height() / 2) + int16NudgeImageY);
QRect rctImage(ptImage, szImage);
objPainter.drawPixmap(rctImage, pmImage);
}
A QPixmap is not a widget (no parent, nor layout).
The QPixmap class is an off-screen image representation that can be used as a paint device.
Use a QLabel for use with a parent (and layout)
QLabel* label = new QLabel(parent);
label->setPixmap(pixmap);
If you want to make sure that your QPixmap is painted on top of everything else inside your widget, simply paint it last.
Keep in mind that other widgets may be painted above your widget.

QT5: Drawing rect on top of another rect using drawPixMap()

am I using drawPixmap() correctly?
Essentially my goal is to take an tileset image, and replace an individual tile with a custom tile image.
I'm able to get both images to load on the screen, but when I call drawPixmap(), then original image doesn't change at all.
Thanks in advance.
void replaceCustomTile(QPixmap custom, QPixmap target, int whichTile) {
QRect rect(0, 0 + (squareTileSize * whichTile), squareTileSize, squareTileSize);
QRect customRect = custom.rect();
QPainter painter(this);
painter.drawPixmap(rect, target, customRect);
painter.end();
}
EDIT:
This is how replaceCustomTile is called:
QPixmap terrainTiles(":/static/Terrain.png");
QPixmap customTile(":/static/Smiles.png");
replaceCustomTile(customTile, terrainTiles, 0);
To intialize QPainter by this it must be called from the widget paintEvent(QPaintEvent *) if you want to draw it on some widget. So, replaceCustomTile() should be called from the event handler in that case.
To draw some pixmap on top of another pixmap QPainter should be initialized by the target pixmap using QPainter::begin():
QPainter painter;
painter.begin(&target);
painter.drawPixmap(rect, custom);
painter.end();
The above code draws QPixmap custom into given QRect rect over QPixmap target. The target is modified.

QLabel border won't display with pixmap

I can get a border to display on my QLabels just fine:
But when I try to display a pixmap in them, the border goes away:
I set the frame properties in the constructor of my QLabel subclass:
ObjectSlot::ObjectSlot(int index) {
setIndex(index);
setFrameShape(QFrame::StyledPanel);
setFrameShadow(QFrame::Raised);
setLineWidth(3);
setMidLineWidth(3);
setAlignment(Qt::AlignCenter);
return;
}
The pixmap is set in the paintEvent:
void ObjectSlot::paintEvent(QPaintEvent* event) {
QPixmap* image = new QPixmap("://images/Box.png");
setPixmap(image->scaled(width(),height(),Qt::KeepAspectRatio));
QLabel::paintEvent(event);
}
Why does the border go away? Why is life so cruel?
As doc said:
Setting the pixmap clears any previous content. The buddy shortcut, if
any, is disabled.
So it seems that it is impossible, but I found next solution, you shouldn't setPixmap(), you need just drawPixmap() when all correct label was painted:
void ObjectSlot::paintEvent(QPaintEvent *e)
{
QLabel::paintEvent(e);
//label painted
QPainter p(this);
QPixmap image("G:/2/qt.png");
p.drawPixmap(QPoint(1,1),image.scaled(100,100,Qt::KeepAspectRatio));
}
Result:
Not the best solution because you should adapt it to your purposes, but currently better than nothing.

QPixmap of a QGraphicsTextItem

How do you convert/paint a QGraphicsTextItem into a QPixmap?
You can add it to a QGraphicsScene (if it's not already inside one) and then render() the scene to a QPixmap using a QPainter
QPixmap pix(100, 100);
QPainter paint(&pix);
scene.render(&paint);
Or, you can save yourself the trouble and just use QPainter::drawText() after changing the current font of the painter. it should provide the same capabilities.
Maybe something like this-
QPixmap pix(100, 100);
QPainter paint(&pix);
paint.drawText(0, 0, "Hello World");
The QGraphicsTextItem::document() function is the back door you're looking for:
// pItem is a QGraphicsTextItem *
QPixmap srcPixmap(pItem->boundingRect().size().toSize());
QPainter tmpPainter(&srcPixmap);
pItem->document()->drawContents(&tmpPainter);
tmpPainter.end()