gpt4 book ai didi

c++ - 在 Qt 中显示线程图形的最有效方式是什么?

转载 作者:行者123 更新时间:2023-11-30 03:54:14 25 4
gpt4 key购买 nike

这不是我的项目,但为了简单起见,假设我只需要打印移动的方 block (这是我实际上作为中间步骤所做的)。我见过很多在 Qt 中制作图形的方法,但我真的不清楚什么是“最佳”方法。

当然,我希望我的图形在一个单独的线程中运行,我通过简单地绘制到一个小部件或通过将绘制功能移动到它自己的线程中以及通过在不同线程中绘制到图像然后显示图像。为此,我创建了一个 QObject,然后将其分配给 QThread,而不是通过子类化 QThread 并重新实现 run()。

这些方式在屏幕上移动了几千个方 block 后都会变慢。我认为这是显示图形的最简单方法,但我知道还有其他使用 openGL 或 QGraphicsView 的方法。我希望堆栈溢出可以节省我的研究时间并为我指明正确的方向。

此外,如果我以正确的方式执行但实现错误,这里是我的一些代码:

在渲染区域:

RenderArea::RenderArea(QWidget *parent) : QWidget(parent)
{
m_image = new QImage(size(), QImage::Format_ARGB32_Premultiplied);
m_imageThread = new QThread();
m_imageMaker = new ImageMaker(size());

m_imageMaker->moveToThread(m_imageThread);

setFocusPolicy(Qt::StrongFocus);

QTimer* timer = new QTimer(this);

//Frame rate approximations: 60FPS ~ 17 ms, 30FPS ~ 33 ms
timer->start(33);

connect(timer, SIGNAL(timeout()), this, SLOT(requestImageUpdate()));
connect(this, SIGNAL(newImageParams(const QSize &)),
m_imageMaker, SLOT(makeImage(const QSize &)));
connect(m_imageMaker, SIGNAL(theImage(const QImage &)), this, SLOT(updateImage(const QImage &)));
connect(m_imageThread, SIGNAL(finished()), m_imageMaker, SLOT(deleteLater()));
connect(this, SIGNAL(doubleEntities()), m_imageMaker, SLOT(doubleEntities()));
connect(this, SIGNAL(halveEntities()), m_imageMaker, SLOT(halveEntities()));

m_imageThread->start();
}

RenderArea::~RenderArea()
{
m_imageThread->quit();
m_imageThread->deleteLater();
}

void RenderArea::paintEvent(QPaintEvent * /* event */)
{
QPainter widgetPainter(this);
widgetPainter.drawImage(0,0,*m_image);
}

void RenderArea::updateImage(const QImage& image) {
*m_image = image;
m_displayUpdatePending = false;
update();
}

void RenderArea::requestImageUpdate() {
if(!m_displayUpdatePending) {
m_displayUpdatePending = true;
emit newImageParams(size());
}
}

void RenderArea::keyPressEvent(QKeyEvent *event) {
switch (event->key()) {
case Qt::Key_Plus:
emit doubleEntities();
break;
case Qt::Key_Minus:
emit halveEntities();
break;
default:
QWidget::keyPressEvent(event);
}
}

在图像制作器中:

ImageMaker::ImageMaker(QSize parentSize, QObject* parent) : QObject(parent)
{
m_numEntities = 128;
m_upperBound = 0;
m_rightBound = parentSize.width();
m_lowerBound = parentSize.height();
m_leftBound = 0;

//seed the random number generator
QTime time = QTime::currentTime();
qsrand((uint)time.msec());

m_mutex.lock();
randomizeEntities();
m_mutex.unlock();
}

ImageMaker::~ImageMaker()
{

}

void ImageMaker::makeImage(const QSize & parentSize) {
m_mutex.lock();

advanceEntities();

m_rightBound = parentSize.width();
m_lowerBound = parentSize.height();

QImage image(parentSize, QImage::Format_ARGB32_Premultiplied);


QPainter imagePainter(&image);
imagePainter.setRenderHint(QPainter::Antialiasing, true);

imagePainter.setPen(QColor(0,0,0));

for (int i = 0; i < m_numEntities; ++i) {
imagePainter.drawRect(m_entityList.at(i).at(0),m_entityList.at(i).at(1),10,10);
}

imagePainter.setPen(QColor(255,0,0));
imagePainter.drawText(10,10,QString::number(m_numEntities));

imagePainter.end();

m_mutex.unlock();

emit theImage(image);
}

void ImageMaker::randomizeEntities() {
m_entityList.clear();

for(int i = 0; i < m_numEntities; ++i) {
QList<int> thisRow;
thisRow.push_back(qrand() % (m_rightBound + 1)); //random starting X coordinate
thisRow.push_back(qrand() % (m_lowerBound + 1)); //random starting Y coordinate

int tempRandX = (qrand() % 4) + 1;
int tempRandY = (qrand() % 4) + 1;
tempRandX *= (qrand() % 2) ? -1 : 1;
tempRandY *= (qrand() % 2) ? -1 : 1;

thisRow.push_back(tempRandX); //random starting X velocity
thisRow.push_back(tempRandY); //random starting Y velocity

m_entityList.push_back(thisRow);
}
}

void ImageMaker::advanceEntities() {
for (int i = 0; i < m_numEntities; ++i) {
QList<int> thisRow = m_entityList.at(i);
int xPos = thisRow.at(0);
int yPos = thisRow.at(1);
int xVel = thisRow.at(2);
int yVel = thisRow.at(3);

xPos += xVel;
yPos += yVel;

if ((xPos < 0 && xVel < 0) || (xPos > m_rightBound && xVel > 0)) xVel *= -1;
if ((yPos < 0 && yVel < 0) || (yPos > m_lowerBound && yVel > 0)) yVel *= -1;

thisRow.clear();
thisRow << xPos << yPos << xVel << yVel;

m_entityList.replace(i, thisRow);
}
}

void ImageMaker::halveEntities() {
m_mutex.lock();
if (m_numEntities > 16) {
m_numEntities /= 2;
}
randomizeEntities();
m_mutex.unlock();
}

void ImageMaker::doubleEntities() {
m_mutex.lock();
m_numEntities *= 2;
randomizeEntities();
m_mutex.unlock();
}

最佳答案

如果您试图通过处理多线程优化来优化渲染速度,那您就错了。

您应该尝试做的是尽可能在一次绘制调用中对渲染图元进行批处理(因此,您应该尝试一次绘制 1000 个正方形,而不是绘制 1000 次 1 个正方形)。

我建议您将您的研究指向 OpenGl 渲染优化方向(并了解 QT 如何批处理对象)。

关于c++ - 在 Qt 中显示线程图形的最有效方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29674901/

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