title: SDL2 Rendering into QWidget
body: <p>I'm looking to port an emulator I wrote in GTK2 and SDL1.2 into using QT5 and SDL2.0.3. So far thats been fine as far as the emulator all works in SDL2 independantly and the GUI works fine in QT, with them both interacting. My issue is with trying to get SDL2 to appear inside a QWidget. This is something that worked fine in SDL1.2 using the window hack method.</p>
  
  <p>From everything I've read online, this can be down with the <code>SDL_CreateWindowFrom</code> function getting the <code>winId()</code> from a QT Widget and passing it into it. However this seems to be for when you are using openGL.</p>
  
  <p>What I want to know is how to do this when im only using SDL2's standard drawing functions to a texture, no OpenGL. All my attempts so far have failed and SDL just didn't appear in the widget at all.</p>
  
  <p>Alternatively, I know you can convert an SDL_Suface to a QImage. How would I then go about getting the QT Widget to update everytime the QImage is updated (60fps). The complication with this is QT and SDL are running in seperate threads.</p>
  
  <p>If anyone has some idea of a direction to point me or some useful example codes that may lead to an answer I would be very greatful. I havn't provided code because I'm not yet sure how to do it, to provide any code.</p>
  
  <pre><code>EDIT
  </code></pre>
  
  <hr>
  
  <p>So after some trialing with SDL and QT. I have managed to get SDL2 to draw into a QWidget using the its Standard Drawing tools. However this only works when setting the renderer to <code>SDL_RENDERER_SOFTWARE</code>. If i try and use <code>SDL_RENDERER_ACCELERATED</code> like I would perfer too. It results in the QT window locking up completely and failing to draw anything (including all other widgets in the window), and finally being terminated as not responding by the operating system (Kubuntu in my testing case).</p>
  
  <p>Does any one have any leads as to why this may be. I can use <code>SDL_RENDERER_ACCELERATED</code> fine when SDL2 is drawing into its own window created with <code>SDL_CreateWindow</code> and <code>SDL_CreateRenderer</code>. Its only a problem when drawing to a QWidget.</p>
  
  <p>Here is the code I have so far:</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include &lt;QApplication&gt;
  #include &lt;SDL2/SDL.h&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
  
      SDL_Init(SDL_INIT_VIDEO);
      SDL_Window* window = SDL_CreateWindowFrom((void*)w.ptr_gfx-&gt;winId());
      SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
  
      SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
      SDL_RenderFillRect(render, NULL);
      SDL_RenderPresent(render);
  
      return a.exec();
  
      SDL_DestroyWindow(window);
      SDL_DestroyRenderer(render);
      SDL_Quit();
  }
  </code></pre>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      ptr_gfx = ui-&gt;gfx;
      ui-&gt;gfx-&gt;setUpdatesEnabled(false);
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  </code></pre>
  
  <p>mainwindow.h</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
      Ui::MainWindow *ui;
      QWidget* ptr_gfx;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>mainwindow.ui</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ui version="4.0"&gt;
   &lt;class&gt;MainWindow&lt;/class&gt;
   &lt;widget class="QMainWindow" name="MainWindow"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;400&lt;/width&gt;
      &lt;height&gt;300&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="windowTitle"&gt;
     &lt;string&gt;MainWindow&lt;/string&gt;
    &lt;/property&gt;
    &lt;widget class="QWidget" name="centralWidget"&gt;
     &lt;widget class="QWidget" name="gfx" native="true"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;20&lt;/x&gt;
        &lt;y&gt;10&lt;/y&gt;
        &lt;width&gt;191&lt;/width&gt;
        &lt;height&gt;131&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
    &lt;/widget&gt;
    &lt;widget class="QMenuBar" name="menuBar"&gt;
     &lt;property name="geometry"&gt;
      &lt;rect&gt;
       &lt;x&gt;0&lt;/x&gt;
       &lt;y&gt;0&lt;/y&gt;
       &lt;width&gt;400&lt;/width&gt;
       &lt;height&gt;20&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
    &lt;/widget&gt;
    &lt;widget class="QToolBar" name="mainToolBar"&gt;
     &lt;attribute name="toolBarArea"&gt;
      &lt;enum&gt;TopToolBarArea&lt;/enum&gt;
     &lt;/attribute&gt;
     &lt;attribute name="toolBarBreak"&gt;
      &lt;bool&gt;false&lt;/bool&gt;
     &lt;/attribute&gt;
    &lt;/widget&gt;
    &lt;widget class="QStatusBar" name="statusBar"/&gt;
   &lt;/widget&gt;
   &lt;layoutdefault spacing="6" margin="11"/&gt;
   &lt;resources/&gt;
   &lt;connections/&gt;
  &lt;/ui&gt;
  </code></pre>