QGraphicsItem子类引发X错误并崩溃
QGraphicsItem subclass throwing X Error and crashing
我正在尝试创建一个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。(它们是隐含共享的,所以副本很便宜)。
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- cv::namedWindow() 崩溃,分段错误
- 由于指针算法错误,代码在 memcpy 中崩溃
- python-clang 崩溃并出现分段错误
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- C++如果调用 vector,矢量分割错误会导致崩溃
- libcurl curl_easy_perform崩溃(分段错误)c++
- C++ rezing 动态数组最多可以处理一定数量的元素,但在某些时候会崩溃并显示错误 (0XC0000005)
- 从转码示例中使用时,比例过滤器会带有错误崩溃
- 在函数内部错误定位后,从函数中释放返回的指针会导致芯片崩溃
- 函数和指针帮助:程序生成,但崩溃,没有任何特定错误
- React 本机 0.59 应用程序崩溃并出现 libc 错误
- 为什么输出错误崩溃
- 如何调试 gRPC 服务器崩溃而没有任何错误
- 如果ConstexPR编译了错误分支的时间崩溃
- 添加MFC到现有的Win32 c++导致错误/崩溃