我想在Qt中非常有效地绘制基本2D形状(正方形),因此我继承了 QOpenGLWidget ,如下所示:
WatorOpenGLWidget.hpp

#include <QOpenGLWidget>
#include <QPainter>
#include "Environment.hpp"

#ifndef WATOROPENGLWIDGET_HPP
#define WATOROPENGLWIDGET_HPP


class WatorOpenGLWidget : public QOpenGLWidget
{
private:
    Environment* environment;
public:
    WatorOpenGLWidget(QWidget* widget, Environment* environment);
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int w, int h) override;
    void setEnvironment(Environment *e) {this->environment = e;};
};

#endif // WATOROPENGLWIDGET_HPP
WatorOpenGLWidget.cpp
#include "WatorOpenGLWidget.hpp"
#include <iostream>

WatorOpenGLWidget::WatorOpenGLWidget(QWidget* widget, Environment* e): QOpenGLWidget(widget), environment(e){
}
void WatorOpenGLWidget::initializeGL() {
    glClearColor(0, 0, 0, 1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);
}

void WatorOpenGLWidget::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QPainter painter(this);
    painter.setBrush(QBrush(Qt::blue));
    painter.drawRect(0, 0, 3 * environment->getWidth(), 3 * environment->getHeight());

    std::vector<Agent*> agents = environment->getAgents();
    for(uint32_t i = 0; i < agents.size(); i++){
        painter.setBrush(agents[i]->getColor());
        painter.drawRect(agents[i]->getX() * 3, agents[i]->getY() * 3, 3, 3);
    }
}

void WatorOpenGLWidget::resizeGL(int w, int h){
    glViewport(0, 0, w, h);
}
然后从mainwindow类创建自定义窗口小部件 WatorOpenGLWidget :
#include "Mainwindow.hpp"
#include "ui_mainwindow.h"
#include "Environment.hpp"
#include "Fish.hpp"
#include "Shark.hpp"
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateEnvironment()));
    timer->start(20);
    tick = 0;

    initializeEnvironment();
    opengl_widget = new WatorOpenGLWidget(nullptr, environment);
    opengl_widget->setFixedSize(3 * environment->getWidth(), 3 * environment->getHeight());
    ui->scrollAreaWidgetContents->layout()->addWidget(opengl_widget);
    updateGUI();
}

void MainWindow::initializeEnvironment(){
    this->environment = new Environment(100, 100, 0);

    std::vector<Agent*> agents;
    for(uint32_t i = 0; i < 5000; i++){
        agents.push_back(new Fish());
    }

    for(uint32_t i = 0; i < 1000; i++){
        agents.push_back(new Shark());
    }

    this->environment->placeAgents(agents);
}

void MainWindow::updateEnvironment(){
    initializeEnvironment();
    std::vector<Agent*> agents = environment->getAgents();
    for(uint32_t i = 0; i < agents.size(); i++){
        agents[i]->decide();
    }

    updateGUI();
}

void MainWindow::updateGUI(){
    opengl_widget->setEnvironment(environment);
    opengl_widget->paintGL();
    tick++;
    std::string tick_text = "tick " + std::to_string(tick);
    ui->tickLabel->setText(QString::fromStdString(tick_text));
}

MainWindow::~MainWindow()
{
    delete timer;
    delete environment;
    delete ui;
}
每次计时器触发绘图时,绘图效果都很好,但是在运行时会生成以下输出:
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active
我应该怎么做才能避免这种情况?另外,使用QPainter对象是否比使用OpenGL本机函数消耗更多的CPU?

最佳答案

您不应该直接调用paintGL(),而应该调用update()方法,该方法将调用paintEvent()方法,而这又将调用paintGL()

void MainWindow::updateGUI(){
    opengl_widget->setEnvironment(environment);
    opengl_widget->update();
    tick++;
    ui->tickLabel->setText(QString("tick %1").arg(tick));
}

10-08 18:45