[英]SDL2 Rendering into QWidget
我希望將我在GTK2和SDL1.2中編寫的模擬器移植到使用QT5和SDL2.0.3。 到目前為止,只要模擬器在SDL2中獨立工作並且GUI在QT中工作正常,它們都可以互動。 我的問題是嘗試讓SDL2出現在QWidget中。 這是使用窗口黑客方法在SDL1.2中正常工作的東西。
從我在線閱讀的所有內容中,可以通過SDL_CreateWindowFrom
函數從QT Widget獲取winId()
並將其傳遞給它。 但是,這似乎適用於使用openGL的情況。
我想知道的是當我只使用SDL2的標准繪圖功能到紋理,沒有OpenGL時如何做到這一點。 到目前為止,我所有的嘗試都失敗了,SDL根本沒有出現在小部件中。
或者,我知道您可以將SDL_Suface轉換為QImage。 每當QImage更新(60fps)時,我將如何讓QT Widget更新。 與此相關的復雜因素是QT和SDL在單獨的線程中運行。
如果有人知道指向我的方向或一些可能導致答案的有用示例代碼,我會非常感激。 我沒有提供代碼,因為我還不確定如何做,提供任何代碼。
EDIT
經過一些SDL和QT的試驗。 我已經設法使用標准繪圖工具將SDL2繪制到QWidget中。 但是,這僅在將渲染器設置為SDL_RENDERER_SOFTWARE
。 如果我嘗試使用SDL_RENDERER_ACCELERATED
,我也SDL_RENDERER_ACCELERATED
。 它導致QT窗口完全鎖定並且無法繪制任何內容(包括窗口中的所有其他小部件),並最終因操作系統沒有響應而終止(在我的測試用例中為Kubuntu)。
有沒有人知道為什么會這樣。 我可以用SDL_RENDERER_ACCELERATED
罰款時,SDL2是借鑒與創造它自己的窗口SDL_CreateWindow
和SDL_CreateRenderer
。 繪制到QWidget時它只是一個問題。
這是我到目前為止的代碼:
main.cpp中
#include "mainwindow.h"
#include <QApplication>
#include <SDL2/SDL.h>
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->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();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ptr_gfx = ui->gfx;
ui->gfx->setUpdatesEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="gfx" native="true">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>191</width>
<height>131</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
使用新數據更新QImage
,只需調用窗口小部件的repaint()
方法,使其在屏幕上繪制自己。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.