Пример связки Qt 4.0.1 и SDL 1.2.8-08:
main.pro
TEMPLATE = app
LANGUAGE = C++
TARGET = qtSDL
PROJECTNAME = SPARTA Video Tracking Demo
CONFIG += qt thread warn_on debug
INCLUDEPATH += /usr/include/SDL
LIBS += -lSDL
SOURCES = SDLWidget.cpp main.cpp
HEADERS = SDLWidget.h
main.cpp
/*
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
*/
#include <qapplication.h>
#include <QMainWindow>
#include <QDesktopWidget>
#include "SDLWidget.h"
int main(int argc, char **argv)
{
QApplication
app(argc,argv);
QMainWindow
mw;
QSDLScreenWidget
*sdlWidget = new QSDLScreenWidget(&mw);
mw.setCentralWidget(sdlWidget);
// Query the desktop to determine the size of the desktop and
// to center the application when it opens.
//
int
width = QApplication::desktop()->width(),
height = QApplication::desktop()->height();
mw.setGeometry((width - 800)/2, (height - 600)/2, 800, 600);
mw.show();
return app.exec();
}
SDLWidget.h
/*
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
*/
#ifndef SDLWIDGET_H
#define SDLWIDGET_H
#include <Qt>
#include <QWidget>
struct SDL_Surface;
class QSDLScreenWidget : public QWidget
{
Q_OBJECT
public:
QSDLScreenWidget(QWidget *parent=0, Qt::WFlags flags=0);
protected:
void resizeEvent(QResizeEvent *);
void paintEvent(QPaintEvent *);
private:
SDL_Surface
*screen;
};
#endif
SDLWidget.cpp
/*
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
*/
#include <iostream>
#include <QX11Info>
#include <SDL.h>
#include "SDLWidget.h"
#ifdef Q_WS_X11
#include <X11/Xlib.h>
#endif
QSDLScreenWidget::QSDLScreenWidget(QWidget *parent, Qt::WFlags flags) :
QWidget(parent, flags),
screen(0)
{
// Turn off double buffering for this widget. Double buffering
// interferes with the ability for SDL to be properly displayed
// on the QWidget.
//
setAttribute(Qt::WA_PaintOnScreen);
}
void QSDLScreenWidget::resizeEvent(QResizeEvent *)
{
// We could get a resize event at any time, so clean previous mode.
// You do this because if you don't you wind up with two windows
// on the desktop: the Qt application and the SDL window. This keeps
// the SDL region synchronized inside the Qt widget and the subsequent
// application.
//
screen = 0;
SDL_QuitSubSystem(SDL_INIT_VIDEO);
// Set the new video mode with the new window size
//
char
variable[64];
sprintf(variable, "SDL_WINDOWID=0x%lx", winId());
putenv(variable);
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
{
std::cerr << "Unable to init SDL: " << SDL_GetError() << std::endl;
return;
}
screen = SDL_SetVideoMode(width(), height(), 0, 0);
if (!screen)
{
std::cerr << "Unable to set video mode: " << SDL_GetError() <<
std::endl;
return;
}
}
void QSDLScreenWidget::paintEvent(QPaintEvent *)
{
#ifdef Q_WS_X11
// Make sure we're not conflicting with drawing from the Qt library
//
XSync(QX11Info::display(), FALSE);
#endif
if (screen)
{
SDL_FillRect(screen, NULL, 0);
SDL_Surface
*image = SDL_LoadBMP("sample.bmp");
if (image)
{
SDL_Rect
dst;
dst.x = (screen->w - image->w)/2;
dst.y = (screen->h - image->h)/2;
dst.w = image->w;
dst.h = image->h;
SDL_BlitSurface(image, NULL, screen, &dst);
}
SDL_Flip(screen);
}
}