这不是我的项目,但为了简单起见,我想说我只需要打印移动方块(这实际上是我作为中间步骤所做的)。我已经看到很多方法在Qt中制作图形,但我真的不清楚什么是“最好”的方式。
我希望我的图形在一个单独的线程中运行,我通过简单地绘制到一个小部件或者将绘制功能移动到它自己的线程中,并通过绘制到另一个线程中的图像然后我来实现这一点。显示图像。我这样做是通过创建一个QObject,然后将其分配给QThread,而不是通过继承QThread并重新实现run()。
这些方式在屏幕上移动了几千个方格后都会变慢。我认为这是显示图形的最简单方法,但我知道还有其他方法可以使用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();
}
答案 0 :(得分:2)
如果您尝试通过解决多线程优化来优化渲染速度,那么您做错了。
你应该尝试做的是在可能的情况下仅在一次绘制调用中批量渲染渲染的图元(因此,不应绘制1000次1平方,而应尝试一次绘制1000个正方形)。
我建议您将您的研究指向OpenGl渲染优化方向(并了解QT如何批处对象)。
答案 1 :(得分:0)
您可以使用QGraphicsScene和1000 QGraphicsRectItem。 这是最便携,最有效的方法(QGraphicsScene已经过优化)。
最有效的方法可能是使用OpenGL,但我不确定它是否值得付出努力。