├── LICENSE ├── README.md ├── SDL2Widget.cpp ├── SDL2Widget.h ├── SDL2Widget.png ├── SDL2Widget.pro └── main.cpp /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 hubenchang0515 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SDL2Widget-for-Qt5 2 | Paint QWidget by SDL2 3 | ![image](https://github.com/hubenchang0515/SDL2Widget-for-Qt5/blob/master/SDL2Widget.png?raw=true) 4 | -------------------------------------------------------------------------------- /SDL2Widget.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | File : SDL2Widget.cpp 3 | Author : hubenchang0515@outlook.com 4 | Blog : www.kurukurumi.com 5 | */ 6 | 7 | #include 8 | 9 | SDL2Widget::SDL2Widget(QWidget* parent):QWidget(parent) 10 | { 11 | /* SDL2初始化 */ 12 | SDL_Init(SDL_INIT_EVERYTHING); 13 | /* 把QWidget转换为SDL_Window */ 14 | window = SDL_CreateWindowFrom((void*)this->winId()); 15 | /* SDL2 Image初始化 */ 16 | IMG_Init(IMG_INIT_PNG); 17 | /* 加载图片,创建surface */ 18 | surface = IMG_Load("demo.jpg"); 19 | } 20 | 21 | SDL2Widget::~SDL2Widget() 22 | { 23 | 24 | } 25 | 26 | /* 绘图操作必须在paintEvent事件里进行 */ 27 | void SDL2Widget::paintEvent(QPaintEvent* event) 28 | { 29 | /* 在window上创建一个render */ 30 | render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED 31 | | SDL_RENDERER_PRESENTVSYNC); 32 | /* 创建一个texture */ 33 | texture = SDL_CreateTextureFromSurface(render, surface); 34 | /* 清空render的内容 */ 35 | SDL_RenderClear(render); 36 | /* 将texture复制到render上 */ 37 | SDL_RenderCopy(render, texture, NULL, NULL); 38 | /* 将render显示到window上 */ 39 | SDL_RenderPresent(render); 40 | SDL_RenderPresent(render); //我的电脑上,需要调用两次才能显示出来,原因不明 41 | /* 销毁render和texture,释放内存 */ 42 | SDL_DestroyTexture(texture); 43 | SDL_DestroyRenderer(render); 44 | 45 | } -------------------------------------------------------------------------------- /SDL2Widget.h: -------------------------------------------------------------------------------- 1 | /* 2 | File : SDL2Widget.h 3 | Author : hubenchang0515@outlook.com 4 | Blog : www.kurukurumi.com 5 | */ 6 | 7 | #ifndef SDL2WIDGET_H 8 | #define SDL2WIDGET_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | class SDL2Widget : public QWidget 16 | { 17 | Q_OBJECT 18 | public: 19 | SDL2Widget(QWidget* parent=0); 20 | ~SDL2Widget(); 21 | protected: 22 | void paintEvent(QPaintEvent* event); 23 | 24 | private: 25 | SDL_Window *window; 26 | SDL_Renderer *render; 27 | SDL_Surface *surface; 28 | SDL_Texture *texture; 29 | }; 30 | 31 | 32 | #endif -------------------------------------------------------------------------------- /SDL2Widget.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hubenchang0515/SDL2Widget-for-Qt5/b3680bd4a4102db45324bd19dee578a330367cb5/SDL2Widget.png -------------------------------------------------------------------------------- /SDL2Widget.pro: -------------------------------------------------------------------------------- 1 | ###################################################################### 2 | # Automatically generated by qmake (3.0) ?? 12? 1 00:11:16 2016 3 | ###################################################################### 4 | 5 | QT+=widgets 6 | LIBS+=-lSDL2 -lSDL2_image 7 | TEMPLATE = app 8 | TARGET = SDL2Widget 9 | INCLUDEPATH += . 10 | 11 | # Input 12 | HEADERS += SDL2Widget.h 13 | SOURCES += main.cpp SDL2Widget.cpp 14 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | File : main.cpp 3 | Author : hubenchang0515@outlook.com 4 | Blog : www.kurukurumi.com 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc,char* argv[]) 13 | { 14 | QApplication app(argc,argv); 15 | QVBoxLayout* layout = new QVBoxLayout; 16 | SDL2Widget* sdlWidget = new SDL2Widget; 17 | QPushButton* button = new QPushButton("Button"); 18 | layout->addWidget(sdlWidget); 19 | layout->addWidget(button); 20 | QWidget w; 21 | w.setLayout(layout); 22 | w.show(); 23 | w.resize(640,400); 24 | QObject::connect(button,&QPushButton::clicked, 25 | sdlWidget,(void(SDL2Widget::*)(void))&SDL2Widget::repaint); 26 | return app.exec(); 27 | } 28 | --------------------------------------------------------------------------------