How to use QT UI with OF?

#1

Hi, I’m quite new to OpenFrameworks. I was able to setup msys2 with qtcreator under Windows10 and compile some examples. Now I’d like to try to use a QT to build a of application. But I have no idea how to do it. Simple example: You usually create a qt widget and display something in it. How can I use a qt widget to show my content created in of?

Qtcreator : msys2root detection failure
#2

Okay, I found an approach by jordi ofxQt, I’ll try to understand how it works.

#3

Please let me know if you need any help. It’s still under construction… so expect lots of changes. I hope it helps :slight_smile:

#4

Hi Jordi,
I just tried it again and had no success compiling the examples. I intalled Qt5.9.1 and the latest ofxQt. Do you have any ideas what could be wrong?

1>C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\include\QtCore/qnamespace.h(118): error C2059: syntax error: ‘constant’ (compiling source file src\main.cpp)
1>C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\include\QtCore/qnamespace.h(118): error C3805: ‘constant’: unexpected token, expected either ‘}’ or a ‘,’ (compiling source file src\main.cpp)
1>C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\include\QtCore/qnamespace.h(118): error C2059: syntax error: ‘constant’ (compiling source file src\ofApp.cpp)
1>C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\include\QtCore/qnamespace.h(118): error C3805: ‘constant’: unexpected token, expected either ‘}’ or a ‘,’ (compiling source file src\ofApp.cpp)
1>C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\include\QtCore/qnamespace.h(118): error C2059: syntax error: ‘constant’ (compiling source file src\QtGuiClass.cpp)
1>C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\include\QtCore/qnamespace.h(118): error C3805: ‘constant’: unexpected token, expected either ‘}’ or a ‘,’ (compiling source file src\QtGuiClass.cpp)
1>src\ofApp.cpp(28): warning C4996: ‘ofEllipse’: Use ofDrawEllipse instead
1> d:\userdatenhaggi\documents\coding\openframeworksproject\of_v20170714_vs_release\libs\openframeworks\graphics\ofGraphics.h(490): note: see declaration of ‘ofEllipse’
1>C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\include\QtCore/qnamespace.h(118): error C2059: syntax error: ‘constant’ (compiling source file GeneratedFiles\Debug\moc_QtGuiClass.cpp)
1>C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\include\QtCore/qnamespace.h(118): error C3805: ‘constant’: unexpected token, expected either ‘}’ or a ‘,’ (compiling source file GeneratedFiles\Debug\moc_QtGuiClass.cpp)

#5

I remember i had a collision with ofArduino. Not sure if that is your case. but i remember the error was not quite obvious.

There is a macro that has the same name in both sides. so i renamed the ofArduino side. I must fill out a bug in github… but i guess this only affects us, working with Qt… not sure how are the politics when solving this kind of things…

look at libs/openfrmaeworks/communication.ofArduino.h
Line 62

#define SHIFT 0x05 // shiftIn/shiftOut mode

deactivate it or rename it and let me know.

#6

Works much better, thanks a lot for the fast answer.

#7

Hey!
Just a small question. For my tool I need a ofWindow within a qt UI the same way it is done in the “ofAppsInsideQtWindows” example. In this example you use the timer event to call the newFrame() callback and you start the application with qt application->exec() whereas in other examples you start the application using the ofRunApp() and only show the qt window with the .show() method. Are there any advantages/disadvatages to use the first or the second way? Or am I restricted to one method anyway if I want to use a ofWindows inside a qt application?

#8

Yes, so far, you are restricted to use one method or the other. Either Qt opens an OF window, controlling its framerate… or otherwise, OF opens a Qt interface class.

I know using a QTimer is ugly, but I have been using it a lot now and it works pretty well. This the method I-m using for my current project, there is an example at 8_ofAppsInsideQtWindowsWithImgui_singleOfLoop.

If you have suggestions for improvements please feel free to contribute the addon. Thanks!

#9

Hey i have removed the qTimer. I think its much better now.

Please have a look:

it requires these lines to be added in ofMainLoop.h in line 30

	template<typename Window>
	void removeWindow(std::shared_ptr<Window> window) {
		ofRemoveListener(window->events().keyPressed, this, &ofMainLoop::keyPressed);

		windowsApps.erase(window);
		for (auto i : windowsApps) {
			currentWindow = i.first;
			break;
		}
	}
#10

Great! I’ll try it as soon as possible.

#11

Hi Jordi,

I just tried the update and it seems to work fine. However I have another problem. I want to run an ofApp inside a Qt widget, more or less the same way as it is done in the no_timer example, but only in the main window without the device construct. Unfortunately it does not work correctly. I’ve tried to transfer the code from device to the mainWindow.cpp like this:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

ofSetLogLevel(OF_LOG_VERBOSE);
ofLogToConsole();
ofAppPtr = make_shared<ofApp>();
windowPtr = make_shared<ofAppQtWindow>(this->parentWidget());
ofSetupOpenGL(windowPtr, 400, 400, OF_WINDOW);
// add widget to layout
layout = ui->ofLayout;
layout->addWidget(windowPtr->getQOpenGLWidget());
ui->widget->setLayout(layout);
// initialize OF must be here!
ofRunApp(windowPtr, ofAppPtr);

}

But I get the errors below and the ofFbo does not work. Maybe you have a hint what’s wrong? Thanks a lot.

[verbose] ofAppQtWindow Ctor
[verbose] setup ofAppQtWindow
[verbose] initializeGL
[verbose] resizeGL 400 400
[verbose] GL Version:4.5.0 NVIDIA 382.05
[verbose] ofFbo: GL frame buffer object supported
[verbose] ofFbo: checkGLSupport(): maxColorAttachments: -1, maxDrawBuffers: -1, maxSamples: -1
[verbose] ofFbo: GL frame buffer object supported
[verbose] ofFbo: checkGLSupport(): maxColorAttachments: -1, maxDrawBuffers: -1, maxSamples: -1
[verbose] ofFbo: GL frame buffer object supported
[verbose] ofFbo: checkGLSupport(): maxColorAttachments: -1, maxDrawBuffers: -1, maxSamples: -1
[ error ] ofFbo: UNKNOWN ERROR 0
[verbose] initializeGL
[verbose] resizeGL 390 454
[verbose] update
[warning] Framebuffer with id: 0 cannot be bound onto itself.
Most probably you forgot to end() the current framebuffer before calling begin() again or you forgot to allocate() before calling begin().
[ error ] unbalanced fbo bind/unbind binding default framebuffer
[verbose] draw
[verbose] update

#12

I tried to use the device.h directly in a subwidget of mainWindow what does not work either, the application crashes in ofGLRenderer::enableTextureTarget():

device* d = new device(ui->widget);
ui->ofLayout->addWidget(d);

Where ui->widget is my widget and ofLayout the widgets layout.

[verbose] ofAppQtWindow Ctor
[verbose] setup ofAppQtWindow
[ error ] ofAppRunner: couldn’t init GLEW: Missing GL version
[ error ] ofFbo: GL frame buffer object not supported by this graphics card
[verbose] initializeGL
[verbose] resizeGL 272 432
[verbose] update

#13

@haggi Thank you for posting,

I know the current version is not perfect, this issue could lead us to much better options.

Could you please make a branch with this example? it will be much easier to check.
There are too many files involved in this issue.

WTB when i had GLEW errors i had fixed them by making sure QT includes are placed at the end, after the OF includes. Check that in every file where you mix OF and QT includes.

Im still unhappy about the current version because some calls like windowPtr->makeCurrent(); have to be written in the qt widget file and this is not acceptable, they must be at the ofAppQtWindow but i havent found the right way to do it yet.

#14

Okay, I’ll make a branch. I really appreciate your effort.

#15

Hi Jordi,
I created a fork of the repository and added a example 10_ofAppInsideQtWidget. I simply tried to display the device, what is a QtWidget inside another QtWidget in the mainWindow. Application crashes immediatly.

#16

Not sure what are you trying to achieve. But here there is one way to go:

#17

Thanks a lot, that’s more or less the way I tried it before. My goal is simply to display the ofApp in a widget which is embedded in other QT widgets. If I start your example, the videoFBO will not draw and I get a lot of errors in the console what is exactly my problem. Does it run without problems on your system?

#18

Sorry for the late reply!
Yes i also get the errors. I need time to research about this. Would be good to involve someone who has written ofWindow code.

#19

Okay thanks for investigating. For the moment I’ll go with a seperate ofApp Window and a Qt UI window. In case the problem can be solved I’ll recombine the two windows.