这不是我的项目,但为了简单起见,假设我只需要打印移动方块(这是我实际上作为中间步骤所做的)。我已经看到了很多在 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();
}