How to create a OpenGL widget in Qt and Visual Studio?
-
I'm trying to create a
OpenGL
widget usingQt 6.4
andVStudio 2022
, i have enable theQt OpenGL
widget under the modules, but i still getting a lot of compile errors:LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QOpenGLWidget::QOpenGLWidget(class QWidget *,class QFlags<enum Qt::WindowType>)" (__imp_??0QOpenGLWidget@@QEAA@PEAVQWidget@@V?$QFlags@W4WindowType@Qt@@@@@Z) referenced in function "public: void __cdecl Ui_MainWindowClass::setupUi(class QMainWindow *)" (?setupUi@Ui_MainWindowClass@@QEAAXPEAVQMainWindow@@@Z)
I'm testing in a 'clean' project containing nothing more than a
OpenGlWidget
added trough QT Designer.The includes i have added:
#include <QtWidgets> #include <QtOpenGL>
My project configuration:
I have also tried adding, into
Linker -> General -> Additional Library directories
:D:\Qt\6.4.0\msvc2019_64\lib;
And at
Linker -> Input -> Additional Dependencies
:Qt6OpenGL.lib
But i continue getting the same errors.
I have installed everything in the
Qt Maintenance tool
, what i'm missing? -
It's just an unfortunate omission of the plugin. It doesn't list QtOpenGLWidgets in the module list popup.
Don't add linker stuff manually. In the project settings simply enter the name of the module manually instead of using the popup list i.e. in the "Qt Modules" property replace
core;gui;widgets;opengl
with
core;gui;widgets;opengl;openglwidgets
for both Release and Debug configs. -
It's just an unfortunate omission of the plugin. It doesn't list QtOpenGLWidgets in the module list popup.
Don't add linker stuff manually. In the project settings simply enter the name of the module manually instead of using the popup list i.e. in the "Qt Modules" property replace
core;gui;widgets;opengl
with
core;gui;widgets;opengl;openglwidgets
for both Release and Debug configs.@Chris-Kawa thank you! now it is compiled correctly!
Is OpenGL Widget a good choice of widget to display images in a GUI?
-
@Chris-Kawa thank you! now it is compiled correctly!
Is OpenGL Widget a good choice of widget to display images in a GUI?
@Marcia3x said:
Is OpenGL Widget a good choice of widget to display images in a GUI?
It's a very broad question. It depends.
OpenGL is a hardware accelerated low level api. It's very powerful but for simple stuff requires more work - writing shaders, managing transformations and GPU resources etc.
If all you want to do is display a static image you can just use a QLabel with pixmap set on it. If you need more control over how the image is painted you can subclass a widget and use QPainter to draw the image the way you want. If you need to compose it into a larger scene you can use QGraphicsView with an image element in it etc. If you need more advanced animations or shading OpenGL can be a good way to go.
-
@Marcia3x said:
Is OpenGL Widget a good choice of widget to display images in a GUI?
It's a very broad question. It depends.
OpenGL is a hardware accelerated low level api. It's very powerful but for simple stuff requires more work - writing shaders, managing transformations and GPU resources etc.
If all you want to do is display a static image you can just use a QLabel with pixmap set on it. If you need more control over how the image is painted you can subclass a widget and use QPainter to draw the image the way you want. If you need to compose it into a larger scene you can use QGraphicsView with an image element in it etc. If you need more advanced animations or shading OpenGL can be a good way to go.
@Chris-Kawa drawing a image in a qlabel, when you resize or animate it, it uses too much cpu.
I wonder if in a opengl widget what will be used is the gpu?
-
I wonder if in a opengl widget what will be used is the gpu?
It depends on what you do with it. If you just upload the image as a texture and display it as a quad then yes, the work is done mostly by the GPU. If you modify the image and need to re-upload it often then both CPU and GPU will be involved.
Widgets are rendered on the CPU, so the load in case of animation is expected, especially if the image is large or the widget is big. Widgets are not generally suited for animations, rather for static UIs.
If you're interested in animated UIs you might also want to take a look at QtQuick. It is backed by OpenGL (or Vulkan or Direct3D depending on your platform) but has a easier and higher level declarative syntax.