gpt4 book ai didi

qt - 如何在QPixmap上使用QPainter

转载 作者:行者123 更新时间:2023-12-03 23:17:31 25 4
gpt4 key购买 nike

我是Qt / Embedded的新手。我想使用QPainterQPixmap上绘制内容,该内容将添加到QGraphicsScene中。这是我的代码。但是它没有在像素图上显示图形。它仅显示黑色像素图。

int main(int argc, char **argv) {

QApplication a(argc, argv);

QMainWindow *win1 = new QMainWindow();
win1->resize(500,500);
win1->show();


QGraphicsScene *scene = new QGraphicsScene(win1);
QGraphicsView view(scene, win1);
view.show();
view.resize(500,500);

QPixmap *pix = new QPixmap(500,500);
scene->addPixmap(*pix);

QPainter *paint = new QPainter(pix);
paint->setPen(*(new QColor(255,34,255,255)));
paint->drawRect(15,15,100,100);

return a.exec();
}

最佳答案

将位图添加到场景之前,需要在位图上进行绘制。当您将其添加到场景中时,场景将使用它来构造一个QGraphicsPixmapItem对象,该对象也由addPixmap()函数返回。如果要在添加像素图后对其进行更新,则需要调用返回的setPixmap()对象的QGraphicsPixmapItem函数。

所以:

...
QPixmap *pix = new QPixmap(500,500);
QPainter *paint = new QPainter(pix);
paint->setPen(*(new QColor(255,34,255,255)));
paint->drawRect(15,15,100,100);
scene->addPixmap(*pix); // Moved this line
...


要么:

...
QPixmap *pix = new QPixmap(500,500);
QGraphicsPixmapItem* item(scene->addPixmap(*pix)); // Save the returned item
QPainter *paint = new QPainter(pix);
paint->setPen(*(new QColor(255,34,255,255)));
paint->drawRect(15,15,100,100);
item->setPixmap(*pix); // Added this line
...

关于qt - 如何在QPixmap上使用QPainter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17888795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com