在派生的QOpenGLWidget

问题描述

我在派生的 qopenglwidget 上遇到问题:

enter image description here

正方形边框应使用red绘制:

#include "HunterOpenglWidget.hpp"
#include <iostream>

HunterOpenGLWidget::HunterOpenGLWidget(QWidget* widget,Environment* e): qopenglwidget(widget),environment(e)
{

}

void HunterOpenGLWidget::initializeGL() {
    glClearColor(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 HunterOpenGLWidget::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    uint16_t cell_size = 10;
    QPainter painter(this);

    for(uint64_t i = 0; i < environment->getWidth() * environment->getHeight(); i++){
        QColor cell_color = Qt::black;
        uint32_t y = i / environment->getWidth();
        uint16_t x = i % environment->getWidth();
        std::unique_ptr<Agent> agent = environment->getAgent(x,y);
        if(agent != nullptr){
            std::cout << "agent not null " << x << "," << y << std::endl;
            //cell_color = agent->getColor();
            cell_color = Qt::red;
        }
        painter.setBrush(cell_color);
        painter.drawRect(x * cell_size,y * cell_size,cell_size,cell_size);
    }
}


void HunterOpenGLWidget::resizeGL(int w,int h){
    glViewport(0,w,h);
}

我验证了我的边界坐标是有效的(从cout中显示),并且是这种情况:

agent not null 0,0
agent not null 1,0
agent not null 2,0
agent not null 3,0
agent not null 4,0
agent not null 5,0
agent not null 6,0
agent not null 7,0
agent not null 8,0
agent not null 9,0
agent not null 0,1
agent not null 9,1
agent not null 0,2
agent not null 9,2
agent not null 0,3
agent not null 9,3
agent not null 0,4
agent not null 9,4
agent not null 0,5
agent not null 9,5
agent not null 0,6
agent not null 9,6
agent not null 0,7
agent not null 9,7
agent not null 0,8
agent not null 9,8
agent not null 0,9
agent not null 1,9
agent not null 2,9
agent not null 3,9
agent not null 4,9
agent not null 5,9
agent not null 6,9
agent not null 7,9
agent not null 8,9
agent not null 9,9

这是我将窗口小部件添加到窗口的方式:

#include "ui_mainwindow.h"
#include "MainWindow.hpp"
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),ui(new Ui::MainWindow)
{
    uint16_t env_width = 10;
    uint16_t env_height = 10;
    uint16_t cell_size = 10;
    ui->setupUi(this);
    environment = new Environment(env_width,env_height);
    opengl_widget = new HunterOpenGLWidget(nullptr,environment);
    opengl_widget->setFixedSize(cell_size * environment->getWidth(),cell_size * environment->getHeight());
    ui->scrollAreaWidgetContents->layout()->addWidget(opengl_widget);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete environment;
}

有人知道为什么不画我的边界吗?我从已经完成的项目中导入了大多数逻辑,但我没有遇到任何问题,但是在这里我不明白为什么它不能正常工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)