我想在Qt(Qt 5.9.0- MinGW 32位)画一条线,但只是页面是蓝色的。这个程序运行较旧的QT。我试图改变我的程序,但它没有用。
当我将Qt + = opengl添加到专业文件Qt显示错误并最终将库添加到专业文件时。
QT += core gui
QT += opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = DrawLine
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
glwidget.cpp
HEADERS += mainwindow.h \
glwidget.h
FORMS += mainwindow.ui
LIBS += -lopengl32
函数opengl的类窗口小部件。
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QtWidgets>
#include <QGLWidget> //QT += opengl
#include <QOpenGLFunctions> //QT += gui
class GLWidget : public QGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0, QGLWidget *shareWidget = 0);
~GLWidget();
protected:
void initializeGL()override;
void paintGL()override;
void resizeGL(int width, int height)override;
void keyPressEvent(QKeyEvent *e);
private:
int sample;
};
我写了这段代码,但只有蓝屏没有出现。
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget)
: QGLWidget(parent, shareWidget)
{
sample = 0;
}
GLWidget::~GLWidget()
{
}
void GLWidget::initializeGL()
{
initializeOpenGLFunctions();
// glClearColor(0.0, 0.0, 102.0/255.0, 0.0);
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void GLWidget::paintGL()
{
glClearColor(0.1,0.6,0.7,1);
glClear(GL_COLOR_BUFFER_BIT);
// glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
switch (sample)
{
case 0:
glColor3f(0.1, 0.1, 0.1);
glBegin(GL_LINE);
glVertex2f(-50, 0);
glVertex2f(50, 0);
glEnd();
break;
//--------------------------------
case 1:
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex2f(-50, 0);
glColor3f(0, 0, 1);
glVertex2f(50, 0);
glEnd();
break;
//--------------------------------
case 2:
glBegin(GL_LINES);
glVertex2f(-50, 0);
glVertex2f(50, 0);
glVertex2f(0, -50);
glVertex2f(0, 50);
glEnd();
break;
//--------------------------------
case 3:
glBegin(GL_LINE_STRIP);
glColor3f(0, 1, 0);
glVertex2f(0, 0);
glVertex2f(50, 0);
glVertex2f(50, 50);
glVertex2f(-50, 50);
glEnd();
break;
//--------------------------------
case 4:
glColor3f(0, 0, 1);
glBegin(GL_LINE_LOOP);
glVertex2f(0, 0);
glVertex2f(50, 0);
glVertex2f(50, 50);
glVertex2f(-50, 50);
glEnd();
break;
//--------------------------------
case 5:
glLineWidth(5);
glBegin(GL_LINE_STRIP);
glVertex2f(0, 0);
glVertex2f(50, 0);
glVertex2f(50, 50);
glVertex2f(-50, 50);
glEnd();
glLineWidth(1);
break;
}
}
void GLWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);//set viewport, don't rely on default values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width/2, width/2, -height/2, height/2, -1, 1);
}
void GLWidget::keyPressEvent(QKeyEvent *e)
{
switch(e->key())
{
case 'A':
sample = sample < 5 ? sample + 1 : sample;
break;
case 'S':
sample = sample > 0 ? sample - 1 : sample;
};
((QMainWindow*)this->parent())->setWindowTitle(QString::number(sample));
updateGL();
}
在这里,我创建了一个新的布局,以便在页面中显示我的小部件。
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
glW = new GLWidget(0, 0);
QGridLayout *layout1 = new QGridLayout();
QWidget *centralWidget1 = new QWidget(this);
centralWidget1->setLayout(layout1);
layout1->addWidget(glW);
setCentralWidget(centralWidget1);
//this->layout()->addWidget(glW);
// glW->setGeometry(0, 0, 640, 480);
// glW->setFocus();
}
MainWindow::~MainWindow()
{
delete ui;
delete glW;
}
答案 0 :(得分:0)
导致其中一个问题是因为QGLWidget
无法处理焦点,因此当您按下一个键时,您永远不会调用keyPressEvent
,解决方案是使用setFocusPolicy()
设置焦点策略:
GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget)
: QGLWidget(parent, shareWidget)
{
sample = 0;
setFocusPolicy(Qt::StrongFocus);
}
注意:强>
GL_LINE
仅用作使用glPolygonMode
的参数,因此该指令不会绘制任何行。
在以下link中,您可以找到完整的示例。