代码之家  ›  专栏  ›  技术社区  ›  HappyCactus

在qt 5.12上实现qglwidget作为视区

  •  0
  • HappyCactus  · 技术社区  · 6 年前

    我有一个完全可以使用的应用程序 QGraphicsView + QGLWidget QGraphicsScene 使用用户交互绘制三维场景。 概念在qt5的方框示例中进行了解释。

    由于更新到Qt5.12,应用程序不再工作。除了我已经解决的其他一些小问题外,现在我已经设置了所有内容,但是视区不显示任何内容。

    我创建了一个最小概念程序,创建了一个 QLogigsVIEW A QGLWITGET 作为一个视区, 纪实场景 用于绘制视区的派生类。

    我设置了一切,但 QGraphicsScene::DrawBackground() 未调用函数。

    有趣的是,Qt5.6中的应用程序运行良好,但5.12中没有。

    两个版本之间发生了什么变化?

    以下是示例应用程序:

    CMAKLISTS.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)
    

    主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();
    }
    

    场景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_H
    

    扫描电镜

    #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";    
    }
    

    图形视图

    #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_H
    

    图形视图.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);
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   G.M.    6 年前

    根据评论, QGLWidget 已被标记为过时一段时间,并附带警告…

    此类已过时。 它用于保留旧的源代码 工作。我们强烈建议不要在新代码中使用它。

    解决方案是使用 QOpenGLWidget 相反。