1

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

2 回答 2

2

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

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

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

于 2015-04-16T12:48:12.523 回答
0

您可以使用 QGraphicsScene 和 1000 QGraphicsRectItem。这是执行此操作的最便携和最有效的方法(QGraphicsScene 已进行了相当优化)。

最有效的方法可能是使用 OpenGL,但我不确定这是否值得。

于 2015-04-17T19:52:43.003 回答