QGraphicsView vs QOpenGLWidget?



  • I'm working on a way to switch between two different view implementations at runtime, which currently works statically. On one hand, we have a QsmGraphicsView, which is a derived class of QGraphicsView. On the other, we have QsmOpenGLWidget, which is a derived class of QOpenGLWidget.

    QsmOpenGLWidget is basically a re-implementation of the QsmGraphicsView that supports adding a QGraphicsScene as well as OpenGL effects.

    In order to transition over to the new QsmOpenGLWidget, we need to have a parent class that we can use to replace all of our QsmGraphicsView pointers. This works very well at compile time. Based on a #define set up in the main pro file, we either inherit from one class or another.

    #ifdef OPENGL_WIDGET
    #define VIEWWIDGET QsmOpenGLWidget
    #else
    #define VIEWWIDGET QsmGraphicsView
    #endif
    
    class QsmNewGraphicsView : public VIEWWIDGET
    {
        Q_OBJECT
    
        public:
    
            explicit QsmNewGraphicsView(QWidget * parent = 0);
    };
    

    My question is if this is possible to do at runtime. So far the best solution I can come up with, to prevent a ton of duplicated code, is to write a container class like this. All functions we used a pointer to the QsmGraphicsView for, we now re-implement by using one pointer or another depending on a runtime config option.

    class QsmNewGraphicsView
    {
        Q_OBJECT
    
        public:
    
            QsmNewGraphicsView(QWidget * view);
    
            //all used functions go here
    
        private:
    
            bool m_opengl = false;
    
            QsmGraphicsView * graphicsView = nullptr;
            QsmOpenGLWidget * openglWidget = nullptr;
    };
    

    But I have a feeling that there's something missing, like I'm overlooking a really good design pattern or Qt feature. Since there has to be a better way than adding a thousand lines of stub functions. Thanks!


  • Lifetime Qt Champion

    Hi,

    What kind of function do you have in common ? Can you show an example ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.