我正在创建从共享内存读取的QImage并将其添加到QList中,并且在paintevent中,我想绘制我在QList中具有的所有图像,但最终会崩溃,如下面的代码片段所示。
我创建了用户定义的paintImage()以从QList和QPainter逐一读取,并尝试绘制,尽管没有崩溃,但最终在屏幕上没有图像。
int fd = open("/run/user/1000/xpra.NanPLD.mmap", O_RDONLY);
int filesize = 256*1024*1024;
void* addr = mmap(NULL, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
createImage(addr,width, height, offset, colorFormat);
void Client::createImage(void* chunk,int width, int height, int offset,
QString colorFormat)
{
qDebug() << "offset Value " << offset;
image = QImage(((const unsigned char*)chunk ) + offset, width, height, QImage::Format_RGB888);
xpraImages.append(image);
}
void Client::paintEvent(QPaintEvent *event)
{
qDebug() <<"Inside paint event" ;
QPainter painter(this);
static int counter = 0;
qDebug() << "counter: " << counter;
qDebug() << "Image list length : " << xpraImages.count();
if(counter < xpraImages.count()){
qDebug() << "counter " << counter;
image = xpraImages.at(counter); // image is class member
qWarning() << image.isNull() << image.width()<< image.height();
if(!image.isNull()){
qDebug() << "Inside paintvent after nullcheck";
painter.drawImage(0,0,image); // here crashes
}
counter++;
QTimer::singleShot(50, this, SLOT(update()));
}
}