我将 qopenglwidget 推到主小部件中,但由于某种原因它没有启动。
我也不能使用 qt 3D 和 Quick 3D
这是代码
对话窗口.h
#ifndef DIALOGOKNO_H
#define DIALOGOKNO_H
#include <QMainWindow>
#include <QFile>
#include <QPushButton>
#include <QLabel>
#include <QSize>
#include <QApplication>
#include <QDesktopWidget>
#include <QRect>
#include <QDebug>
#include <QLineEdit>
#include <QGridLayout>
#include <QSpacerItem>
#include <QObject>
#include "qopenglwidget.h"
class DialogOkno : public QWidget
{
Q_OBJECT
public:
DialogOkno(QWidget *parent = 0);
~DialogOkno();
protected:
QFile* file; // загрузить файл
QGridLayout* gl_layaout[4];
QPushButton* b_load; // кнопка для выбора и загрузки файла
QLabel* l_label[3];
QLineEdit* le_edit[7];
QSpacerItem* si_spacer[4];
QOpenGLWidget *openGLWidget;
//опен гл
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
};
#endif // DIALOGOKNO_H
对话窗口.cpp
#include "dialogokno.h"
DialogOkno::DialogOkno(QWidget *parent)
: QWidget(parent)
{
// обявл элементы
gl_layaout[0] = new QGridLayout(parent);
gl_layaout[1] = new QGridLayout(parent);
gl_layaout[2] = new QGridLayout(parent);
gl_layaout[3] = new QGridLayout(parent);
openGLWidget = new QOpenGLWidget(parent);
l_label[0] = new QLabel(parent);
l_label[1] = new QLabel(parent);
l_label[2] = new QLabel(parent);
le_edit[0] = new QLineEdit(parent);
le_edit[1] = new QLineEdit(parent);
le_edit[2] = new QLineEdit(parent);
le_edit[3] = new QLineEdit(parent);
le_edit[4] = new QLineEdit(parent);
le_edit[5] = new QLineEdit(parent);
le_edit[6] = new QLineEdit(parent);
// настройки элеметов (стилизация)
// заполнение элементов
l_label[0]->setText("hla_global");
l_label[1]->setText("hla_local");
l_label[2]->setText("logger");
le_edit[0]->setText("server_ip_global");
le_edit[1]->setText("server_port_global");
le_edit[2]->setText("server_port_local");
le_edit[3]->setText("server_port_local");
le_edit[4]->setText("log");
le_edit[5]->setText("log_period_ms");
le_edit[6]->setText("log_size_in_mb");
//разложение элементов по группам
gl_layaout[0]->addWidget(l_label[0], 0, 0, 1, 1);
gl_layaout[0]->addWidget(le_edit[0], 1, 0, 1, 1);
gl_layaout[0]->addWidget(le_edit[1], 2, 0, 1, 1);
gl_layaout[1]->addWidget(l_label[1], 0, 0, 1, 1);
gl_layaout[1]->addWidget(le_edit[2], 1, 0, 1, 1);
gl_layaout[1]->addWidget(le_edit[3], 2, 0, 1, 1);
gl_layaout[2]->addWidget(l_label[2], 0, 0, 1, 1);
gl_layaout[2]->addWidget(le_edit[4], 1, 0, 1, 1);
gl_layaout[2]->addWidget(le_edit[5], 2, 0, 1, 1);
gl_layaout[2]->addWidget(le_edit[6], 3, 0, 1, 1);
gl_layaout[3]->addLayout(gl_layaout[0], 0, 0, 1, 1);
si_spacer[0] = new QSpacerItem(120, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gl_layaout[3]->addItem(si_spacer[0], 0, 1, 1, 1);
gl_layaout[3]->addLayout(gl_layaout[1], 0, 2, 1, 1);
si_spacer[1] = new QSpacerItem(120, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gl_layaout[3]->addItem(si_spacer[1], 0, 3, 1, 1);
si_spacer[2] = new QSpacerItem(20, 82, QSizePolicy::Minimum, QSizePolicy::Expanding);
gl_layaout[3]->addItem(si_spacer[2],1, 0, 1, 1);
gl_layaout[3]->addLayout(gl_layaout[2], 2, 0, 1, 1);
si_spacer[3] = new QSpacerItem(20, 82, QSizePolicy::Minimum, QSizePolicy::Expanding);
gl_layaout[3]->addItem(si_spacer[3],3,0);
gl_layaout[3]->addWidget(openGLWidget, 1, 1, 3, 3);
this->setLayout(gl_layaout[3]);
}
DialogOkno::~DialogOkno()
{
}
void QOpenGLWidget::initializeGL()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
void QOpenGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex3f (0.51, 0.51, 0.51);
glVertex3f (-0.51, 0.51, 0.51);
glVertex3f (-0.51, -0.51, 0.51);
glVertex3f (0.51, -0.51, 0.51);
glEnd();
}
void QOpenGLWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
主文件
#include "dialogokno.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DialogOkno w;
QDesktopWidget desktop;
QRect rect = desktop.availableGeometry(desktop.primaryScreen()); // прямоугольник с размерами экрана
QPoint center = rect.center(); //координаты центра экрана
qDebug() << "rect.x()/2 " <<rect.y()/2 << "rect.y()/2" << rect.y()/2;
qDebug() << "center.x()/2 " <<center.y() << "center.y()/2" << center.y();
w.resize(center.x(), center.y());
w.setWindowTitle("программа");
w.show();
return a.exec();
}
dialog_window_qmake.pro
#-------------------------------------------------
#
# Project created by QtCreator 2021-11-24T16:28:03
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = dialog_okno_qmake
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
dialogokno.cpp
HEADERS += \
dialogokno.h
INCLUDEPATH += $$PWD/glfw_bin/include/GLFW/ \ # добавляем для удобства чтоб напрямую писать
$$PWD/glew_bin/include/GL/ \ # только название заголовчного файла без полного пути
$$PWD/stb_image/
HEADERS += \
glfw_bin/include/GLFW/glfw3.h \
glfw_bin/include/GLFW/glfw3native.h \
glew_bin/include/GL/glew.h \
# --- linux ---
unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/x86_64-linux-gnu/ -lOpenGL
INCLUDEPATH += $$PWD/../../../../usr/include/GL
DEPENDPATH += $$PWD/../../../../usr/include/GL
unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/x86_64-linux-gnu/ -lGLEW
INCLUDEPATH += $$PWD/../../../../usr/include/GL
DEPENDPATH += $$PWD/../../../../usr/include/GL
unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/x86_64-linux-gnu/ -lglut
INCLUDEPATH += $$PWD/../../../../usr/include/GL
DEPENDPATH += $$PWD/../../../../usr/include/GL
unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/x86_64-linux-gnu/ -lGLU
INCLUDEPATH += $$PWD/../../../../usr/include/GL
DEPENDPATH += $$PWD/../../../../usr/include/GL
输出:
Запускается /home/dima/dima_project/dialog_okno_qmake/dialog_okno_qmake...
rect.x()/2 0 rect.y()/2 0
center.x()/2 515 center.y()/2 515
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
QOpenGLContext::makeCurrent() called with non-opengl surface 0x59b7a92e99a0
composeAndFlush: makeCurrent() failed
/home/dima/dima_project/dialog_okno_qmake/dialog_okno_qmake завершился с кодом 0
一般来说,这是一个在 Astra Linux 中跳出来的错误。
这是答案(https://wiki.astralinux.ru/pages/viewpage.action?pageId=43614493)
我使用了第一个选项
添加了一行
一切正常:3