How do I use Qt and SDL together?
Solution 1
While you might get it to work like first answer suggest you will likely run into problems due to threading. There is no simple solutions when it comes to threading, and here you would have SDL Qt and OpenGL mainloop interacting. Not fun.
The easiest and sanest solution would be to decouple both parts. So that SDL and Qt run in separate processes and have them use some kind of messaging to communicate (I'd recommend d-bus here ). You can have SDL render into borderless window and your editor sends commands via messages.
Solution 2
This is a simplification of what I do in my project. You can use it just like an ordinary widget, but as you need, you can using it's m_Screen object to draw to the SDL surface and it'll show in the widget :)
#include "SDL.h"
#include <QWidget>
class SDLVideo : public QWidget {
Q_OBJECT
public:
SDLVideo(QWidget *parent = 0, Qt::WindowFlags f = 0) : QWidget(parent, f), m_Screen(0){
setAttribute(Qt::WA_PaintOnScreen);
setUpdatesEnabled(false);
// Set the new video mode with the new window size
char variable[64];
snprintf(variable, sizeof(variable), "SDL_WINDOWID=0x%lx", winId());
putenv(variable);
SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
// initialize default Video
if((SDL_Init(SDL_INIT_VIDEO) == -1)) {
std:cerr << "Could not initialize SDL: " << SDL_GetError() << std::endl;
}
m_Screen = SDL_SetVideoMode(640, 480, 8, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (m_Screen == 0) {
std::cerr << "Couldn't set video mode: " << SDL_GetError() << std::endl;
}
}
virtual ~SDLVideo() {
if(SDL_WasInit(SDL_INIT_VIDEO) != 0) {
SDL_QuitSubSystem(SDL_INIT_VIDEO);
m_Screen = 0;
}
}
private:
SDL_Surface *m_Screen;
};
Hope this helps
Note: It usually makes sense to set both the min and max size of this widget to the SDL surface size.
Solution 3
Rendering onto opengl from QT is trivial (and works very well) No direct experience of SDL but there is an example app here about mixing them. http://www.devolution.com/pipermail/sdl/2003-January/051805.html
There is a good article about mixing QT widgewts directly with the opengl here http://doc.trolltech.com/qq/qq26-openglcanvas.html a bit beyond what you strictly need but rather clever!
Comments
-
hariszhr over 3 years
I am building a physics simulation engine and editor in Windows. I want to build the editor part using Qt and I want to run the engine using SDL with OpenGL.
My first idea was to build the editor using only Qt and share as much code with the engine (the resource manager, the renderer, the maths). But, I would also like to be able to run the simulation inside the editor. This means I also have to share the simulation code which uses SDL threads.
So, my question is this: Is there a way to have an the render OpenGL to a Qt window by using SDL?
I have read on the web that it might be possible to supply SDL with a window handle in which to render. Anybody has experience dong that?
Also, the threaded part of the simulator might pose a problem since it uses SDL threads.
-
hariszhr over 15 yearsThis is for an open source project that I'm starting right now. I will post my architecture/solution as the project progresses.
-
rbaleksandar over 8 yearsBased on the task you want to accomplish I do believe you can go straight for Qt and dump SDL completely. Qt has neat OpenGL support, offers input handling, much better (no offence, SDL people) user interface capabilities etc. Using the OpenGL Qt widget is trivial as hell and the threading models offered by the Qt framework are also very easy to use (you have standard threading, concurrency etc. + the signal/slot concept). If the threading model is not to your liking you can use something like boost for example. Either way I really don't see a reason to use SDL+Qt in your scenario.
-
-
hariszhr over 15 yearsOpenGL is not the issue here. I know it works well with Qt. The problem is mostly threading.
-
Martin Beckett over 15 yearsMy mistake I thought SDL was the physics engine library!
-
hariszhr over 15 yearsThanks, this information is very valuable. I think I'll have to separate the work in 2 processes. The biggest hurdle comes from sharing the OpenGL "context". Qt's widgets do not play nicely with OpenGL/SDL. Threading seems to work well since QT and SDL use the same library (pthreads, win32 threads).
-
S B almost 12 yearsDoes the window id hack work on Mac? It does not seem to work on OSX. I have tried both putenv and SDL_putenv without any luck.
-
Whyrusleeping over 11 yearsI know this is really late, but could someone explain how this works to me?