C++ 在Qt中显示线程化图形最有效的方法是什么?
这不是我的项目,但为了简单起见,让我们假设我只需要打印移动的方块(这实际上是我作为中间步骤所做的)。我见过很多在Qt中制作图形的方法,但我真的不清楚什么是“最好”的方法 当然,我希望我的图形在一个单独的线程中运行,我通过简单地在一个小部件上绘制,或者通过将绘制功能移动到它自己的线程中,以及通过在另一个线程中绘制图像,然后显示图像来实现这一点。为此,我创建一个QObject,然后将其分配给一个QThread,而不是子类化QThread并重新实现run() 在屏幕上移动了几千个方块后,这些方式都会变慢。我原以为这是显示图形的最简单方法,但我知道使用openGL或QGraphicsView还有其他方法。我希望堆栈溢出可以节省我的研究时间,并为我指明正确的方向 此外,如果我用正确的方法进行操作,但实现错误,下面是我的一些代码: 在渲染区域中:C++ 在Qt中显示线程化图形最有效的方法是什么?,c++,qt,opengl,qt5,qt5.4,C++,Qt,Opengl,Qt5,Qt5.4,这不是我的项目,但为了简单起见,让我们假设我只需要打印移动的方块(这实际上是我作为中间步骤所做的)。我见过很多在Qt中制作图形的方法,但我真的不清楚什么是“最好”的方法 当然,我希望我的图形在一个单独的线程中运行,我通过简单地在一个小部件上绘制,或者通过将绘制功能移动到它自己的线程中,以及通过在另一个线程中绘制图像,然后显示图像来实现这一点。为此,我创建一个QObject,然后将其分配给一个QThread,而不是子类化QThread并重新实现run() 在屏幕上移动了几千个方块后,这些方式都会变
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();
}
ImageMaker::ImageMaker(QSize parentSize,QObject*parent):QObject(parent)
{
m_数量=128;
m_上界=0;
m_rightbund=parentSize.width();
m_lowerBound=parentSize.height();
m_leftBound=0;
//为随机数生成器设定种子
QTime time=QTime::currentTime();
qsrand((uint)time.msec());
m_mutex.lock();
随机性();
m_mutex.unlock();
}
ImageMaker::~ImageMaker()
{
}
void ImageMaker::makeImage(常量QSize和parentSize){
m_mutex.lock();
高级实体();
m_rightbund=parentSize.width();
m_lowerBound=parentSize.height();
QImage图像(parentSize,QImage::Format_ARGB32_预乘);
QPainter imagePainter(&image);
setRenderInt(qPaint::抗锯齿,true);
设置笔(QColor(0,0,0));
对于(int i=0;im_rightbund&&xVel>0))xVel*=-1;
如果((yPos<0&&yVel<0)| |(yPos>m_lowerBound&&yVel>0))yVel*=-1;
thisRow.clear();
thisRow如果您试图通过处理多线程优化来优化渲染速度,那么您就错了
如果可能,您应该尝试在一次绘制调用中批处理渲染的基本体(因此,您应该尝试一次绘制1000个正方形,而不是绘制1000乘以1的正方形)
我建议您将您的研究指向OpenGl渲染优化方向(并学习QT如何批处理对象)。您可以使用QGraphicscene和1000 QGraphicsRecitem。
这是实现这一点的最方便、最有效的方法(QGraphicscene经过了相当优化)
最有效的方法可能是使用OpenGL,但我不确定这是否值得付出努力。一次绘制1000个正方形的具体方法是什么?我可以在循环外声明正方形,然后在循环内反复绘制,在每一步之间转换,但这不会加快速度。从长远来看,这将更加复杂让图形在一个单独的线程中运行是让UI在做其他事情时保持活动状态的一个要求。我怀疑OpenGL可能会提供最佳解决方案,但上级拒绝了这条路径,甚至不同意我探索它,但我可能还是会这样做……使用verti,你可以在同一个顶点缓冲区中创建1000个正方形ces位置已经在世界空间中移动,然后有一个索引缓冲区,以正确的顺序引用顶点以生成正方形(它应该类似于0,1,2,2,3,4,5,6,7,8,9…)。然后您只有一个绘制调用。如果您的正方形是静态几何体,则此方法有效。如果您有在2d中移动的动态对象,则可以考虑将2d位置作为一个变量而不是均匀变量,将旋转作为单个浮点数(如果需要)并将缩放作为另一个浮点数(如果需要)。此外,如果您的二维坐标位于屏幕空间中(很可能是这样)你可以在一个由2个短字符组成的整数中同时给出x和y信息,然后使用位掩蔽和移位在顶点着色器中获取信息。这将允许你在VS中重新创建世界矩阵,然后你将相机和透视矩阵产品作为一个统一体发送。