我有一个完全工作的应用程序,它利用QGraphicsView + QGLWidget和QGraphicsScene绘制一个与用户交互的3D场景。这个概念在Qt5的方框示例中进行了解释。
自从更新到QT5.12之后,应用程序就不再工作了。除了我已经修复的其他一些小问题之外,现在我已经做了所有的设置,但是viewport没有显示任何内容。
我创建了一个最小概念程序,它创建了一个QGraphicsView,一个QGLWidget作为一个视口,一个QGraphicsScene派生类来绘制这个视图端口。
我设置了所有的东西,但是没有调用QGraphicsScene::DrawBackground()函数。
有趣的是,应用程序在QT5.6中工作得很好,但在5.12中却不行。
这两个版本之间发生了什么变化?
以下是示例应用程序:
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(Prototypes)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets OpenGL)
add_executable(Prototypes main.cpp GraphicsView.cpp GraphicsView.h Scene.cpp Scene.h)
target_link_libraries(Prototypes Qt5::Core Qt5::OpenGL Qt5::Widgets)main.cpp
#include "GraphicsView.h"
#include "Scene.h"
#include <QApplication>
#include <QGLWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGLWidget *widget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
widget->makeCurrent();
Scene scene(1024,768);
GraphicsView view;
view.setViewport(widget);
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
view.setScene(&scene);
view.resize(800, 600);
view.show();
return app.exec();
}Scene.h
#ifndef PROTOTYPES_SCENE_H
#define PROTOTYPES_SCENE_H
#include <QGraphicsScene>
class QTimer;
class Scene : public QGraphicsScene {
Q_OBJECT
public:
Scene(int width, int height);
protected:
QTimer *m_timer;
void drawBackground(QPainter *painter, const QRectF &rect) override;
};
#endif //PROTOTYPES_SCENE_HScene.cpp
#include "Scene.h"
#include <QPainter>
#include <QDebug>
#include <QTimer>
Scene::Scene(int width, int height)
{
setSceneRect(0,0,width, height);
//m_timer = new QTimer(this);
//m_timer->setInterval(20);
//connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
//m_timer->start();
}
void Scene::drawBackground(QPainter *painter, const QRectF &rect)
{
qDebug() << "DrawBackground";
}GraphicsView.h
#ifndef PROTOTYPES_GRAPHICSVIEW_H
#define PROTOTYPES_GRAPHICSVIEW_H
#include <QGraphicsView>
class GraphicsView : public QGraphicsView {
public:
GraphicsView();
protected:
void resizeEvent(QResizeEvent *event) override;
};
#endif //PROTOTYPES_GRAPHICSVIEW_HGraphicsView.cpp
#include "GraphicsView.h"
#include <QResizeEvent>
#include <QDebug>
void GraphicsView::resizeEvent(QResizeEvent *event)
{
if (scene()) {
qDebug() << "Set Scene Rect " << event->size();
scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
}
QGraphicsView::resizeEvent(event);
}
GraphicsView::GraphicsView()
{
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
}发布于 2019-01-24 17:48:12
根据评论,QGLWidget已被标记为过时一段时间,并附带警告.
这个类是过时的。是为了保持旧的源代码正常工作而提供的。我们强烈建议不要在新代码中使用它。
解决方案是使用QOpenGLWidget代替。
https://stackoverflow.com/questions/54351163
复制相似问题