gpt4 book ai didi

c++ - QGraphicsItem 子类抛出 X 错误并崩溃

转载 作者:行者123 更新时间:2023-11-28 03:48:50 29 4
gpt4 key购买 nike

我正在尝试创建一个 QGraphicsItem,它将以线程方式从文件中提取数据,并在读入数据时显示它。看代码:

#ifndef TILE_H
#define TILE_H

#include <QGraphicsItem>
#include <QThread>
#include <QFutureWatcher>
#include <QtConcurrentRun>
#include "gdal_priv.h"
#include <QPainter>

class Tile : public QObject, public QGraphicsItem
{
Q_OBJECT
Q_INTERFACES(QGraphicsItem)

public:
Tile(QGraphicsItem *parent = 0);

Tile( int nBlocksX, int nBlocksY, int xoffset, int yoffset, int nXBlockSize, int nYBlockSize, int A_BPP,QGraphicsItem *parent=0);
~Tile();
void LoadTilePixmap();

protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
QRectF boundingRect() const;

private:
QFutureWatcher<void> *watcher;
QFuture<void> *future;
QImage *image;
const QStyleOptionGraphicsItem *TileOption;
QPainter *TilePainter;
QWidget *TileWidget;
int nBlocksX, nBlocksY, nBlockXSize, nBlockYSize, tilePosX, tilePosY, A_BPP;

signals:

public slots:
void updateSceneSlot();

};

#endif // TILE_H

还有.cpp

#include "tile.h"

Tile::Tile(QGraphicsItem *parent) : QGraphicsItem(parent)
{
}

Tile::~Tile()
{
}
Tile::Tile(int nBlocksX, int nBlocksY, int xoffset, int yoffset, int nXBlockSize, int nYBlockSize, int A_BPP,QGraphicsItem *parent)
: QGraphicsItem(parent)
{
//set some flags and size parameters related to reading from the file on disk. All instances of this QGraphicsItem read from the same file
this->nBlocksX = nBlocksX;
this->nBlocksY = nBlocksY;
this->nBlockXSize = nXBlockSize;
this->nBlockYSize = nYBlockSize;
this->tilePosX =xoffset;
this->tilePosY = yoffset;
this->A_BPP = A_BPP;
setCacheMode(NoCache);
setFlag(QGraphicsItem::ItemIsMovable,false);
setActive(true);
this->setAcceptHoverEvents(true);
this->setFlag(QGraphicsItem::ItemIsFocusable);
this->image = NULL;

this->future = new QFuture<void>;
this->watcher = new QFutureWatcher<void>;
connect(watcher,SIGNAL(finished()),this,SLOT(updateSceneSlot()));
}
QRectF Tile::boundingRect() const
{
if(image == NULL)
return(QRectF(0,0,0,0));

return(QRectF(0,0,image->width(),image->height()));
}

void Tile::updateSceneSlot()
{
qDebug("updateSceneSlot Thread id %i", QThread::currentThread());
this->paint(TilePainter, TileOption, TileWidget);
}

void Tile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
if(image==NULL)
{
TilePainter=painter;
TileOption=option;
TileWidget=widget;
qDebug()<<"Paint Thread id "<< QThread::currentThread();
*future=QtConcurrent::run(this, &Tile::LoadTilePixmap);
watcher->setFuture(*future);

}

QPointF *p = new QPointF(0.0,0.0);
painter->drawImage(*p, *image);
}

void Tile::LoadTilePixmap()
{
/*...populate floatData with data from file...*/

image = new QImage(nXSize, nYSize, QImage::Format_RGB32);
for (int i = 0 ; i < nYSize ; i++)
{
for (int j = 0 ; j < nXSize ; j++)
{
image->setPixel(j,i,qRgb((unsigned char)floatData[i*nXSize+j],(unsigned char)floatData[i*nXSize+j],(unsigned char)floatData[i*nXSize+j]));
}
}


}

这可以正常编译,但是当我尝试加载图像时,出现一大堆 X 错误,然后程序退出:

.
.
.
X Error: BadGC (invalid GC parameter) 13
Major opcode: 60 (X_FreeGC)
Resource id: 0x1c002bb
X Error: RenderBadPicture (invalid Picture parameter) 181
Extension: 156 (RENDER)
Minor opcode: 7 (RenderFreePicture)
Resource id: 0x1c002ba
X Error: BadPixmap (invalid Pixmap parameter) 4
Major opcode: 54 (X_FreePixmap)
Resource id: 0x1c002b9
X Error: BadAlloc (insufficient resources for operation) 11
Major opcode: 53 (X_CreatePixmap)
Resource id: 0x1c002bc
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Extension: 156 (RENDER)
Minor opcode: 4 (RenderCreatePicture)
Resource id: 0x1c002bc
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 55 (X_CreateGC)
Resource id: 0x1c002bc
X Error: BadGC (invalid GC parameter) 13
Major opcode: 56 (X_ChangeGC)
Resource id: 0x1c002be
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 70 (X_PolyFillRectangle)
Resource id: 0x1c002bc
X Error: BadGC (invalid GC parameter) 13
Major opcode: 60 (X_FreeGC)
Resource id: 0x1c002be
X Error: RenderBadPicture (invalid Picture parameter) 181
Extension: 156 (RENDER)
Minor opcode: 7 (RenderFreePicture)
Resource id: 0x1c002bd
X Error: BadPixmap (invalid Pixmap parameter) 4
Major opcode: 54 (X_FreePixmap)
Resource id: 0x1c002bc
Paint Thread id QThread(0x105feac0)
The program has unexpectedly finished.

我做错了什么?当我将此设置为单线程时,我能够毫无问题地执行此操作。

最佳答案

您的图像加载代码不是线程安全的,因为 QImage 不是线程安全的。这应该是您崩溃的原因。

您也有泄漏,例如在 paint() 中在堆上创建且从未删除的 QPoint。只需在堆栈而不是堆上创建它。图像也一样:你泄露了它,也不需要在堆上创建 QImage。 (它们是隐式共享的,因此拷贝很便宜)。

关于c++ - QGraphicsItem 子类抛出 X 错误并崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6334075/

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