Qt und SDL KeyEvents unter MacOs

Alles rund um die Programmierung mit Qt
Antworten
AndOne
Beiträge: 10
Registriert: 19. März 2011 20:57

Qt und SDL KeyEvents unter MacOs

Beitrag von AndOne »

Hallo Leute,

ich bin dabei eine Qt Anwendung zu schreiben die Plattform unabhängig ist, und noch zusätzlich SDL verwendet. Das SDL Fenster soll extern und "nicht" in einem Widget dargestellt werden. Das ganze funktioniert auch soweit ganz gut, nur habe ich unter MAC OS 10.6 Probleme mit dem empfangen von SDL_KEYDOWN und SDL_KEYUP. Es scheint das unter MAC OS die Events nicht mehr zum SDL Fenster kommen. Unter Windows und Ubuntu läuft das Tadellos nur Mac macht da zicken. Komisch ist das SDL_QUIT und SDL_VIDEORESIZE ausglöst werden. Achso und unter MAC kommt ein Sound wenn ich das SDL Fenster aktiviere und eine Taste drücke. Als wenn das Fenster nicht aktiviert ist. Ich hoffe ihr könnt mir helfen.

MfG AndOne

Ich habe mal den kompletten Code hier eingefügt.

SDL_Qt_Demo_01.pro

Code: Alles auswählen

QT       += core gui
CONFIG += app_bundle
TARGET = SDL_Qt_Demo_01
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

win32{
LIBS += -lSDL
}

linux-g++{
LIBS += -lSDL
}

macx{
SOURCES += /Library/Frameworks/SDL.framework/devel-lite/SDLMain.m
LIBS += -framework SDL
LIBS += -framework cocoa
}
main.cpp

Code: Alles auswählen

#include <QtGui/QApplication>
#include "mainwindow.h"
#include "SDL/SDL.h"

SDL_Surface *screen;

#ifdef WIN32
    int qMain(int argc, char *argv[])
#else
    int main(int argc, char *argv[])
#endif

{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Surface *screen = SDL_SetVideoMode(400,300,32,SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption("SDL Screen",0);


    QApplication a(argc, argv);
    MainWindow *w = new MainWindow(0,screen);
    w->show();

    return a.exec();
}
mainwindow.cpp

Code: Alles auswählen

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

int SDLThreadFunc(void* param);

MainWindow::MainWindow(QWidget *parent, SDL_Surface *sdl_surface) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    screen = sdl_surface;
    thread = SDL_CreateThread(SDLThreadFunc,this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

int SDLThreadFunc(void* param)
{
    MainWindow *mw = (MainWindow*)param;

    SDL_Event event;
    SDL_Rect rec={0,0,100,100};
    static unsigned long color1 = 0;

    bool ende = false;
    while (!ende)
    {
      while (SDL_PollEvent(&event))
        {
          switch (event.type)
            {
            case SDL_VIDEORESIZE:
                mw->screen = SDL_SetVideoMode(event.resize.w,event.resize.h,32,SDL_RESIZABLE | SDL_HWSURFACE | SDL_DOUBLEBUF);
                break;
            case SDL_KEYDOWN:
                /// WIRD UNTER MAC NICHT ANGESPRUNGEN ///
                break;
            case SDL_KEYUP:
                /// WIRD UNTER MAC NICHT ANGESPRUNGEN ///
                break;
            case SDL_QUIT:
                ende = true;
                break;

            default: /* unbeachteter Event */
                break;
            }
        }
      SDL_FillRect(mw->screen,&rec,color1++);
      SDL_Flip(mw->screen);
      SDL_UpdateRect(mw->screen,0,0,400,300);
      SDL_Delay(1);
    }
    SDL_Quit();
}
mainwindow.h

Code: Alles auswählen

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <SDL/SDL.h>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0, SDL_Surface *sdl_surface = 0);
    ~MainWindow();
    SDL_Surface* screen;
private:
    Ui::MainWindow *ui;
    SDL_Thread* thread;

private slots:

};

#endif // MAINWINDOW_H
mainwindow.ui

Code: Alles auswählen

<?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>245</width>
    <height>137</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget"/>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>245</width>
     <height>21</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>
AndOne
Beiträge: 10
Registriert: 19. März 2011 20:57

Beitrag von AndOne »

Schade, hat denn keiner eine Idee ...?
Ich wollte das meine Anwendung auch unter Mac läuft.
AndOne
Beiträge: 10
Registriert: 19. März 2011 20:57

Re: Qt und SDL KeyEvents unter MacOs

Beitrag von AndOne »

Ich habe das Problem selber lösen können. :D
Falls jemand mal vor dem selben Problem steht, hier die Lösung.
Ich habe einfach den SDLThread in der Klasse weg gelassen, dafür einen SDL-Loop in main.cpp gleich nach dem
w.show() eingefügt (also das was in der SDLThreadFunc steht). Somit wird return a.exec(); erst am ende wenn der SDL-Loop beendet wird ausgeführt. Somit funktioniert das Qt MainWindow und weitere Dialoge. Und SDL funktioniert so auch ohne Probleme mit den KeyEvents. Und wie gesagt den Zirkus muss man nur für MAC anwenden unter Linux und Win32 geht auch die obige Variante.

MfG AndOne
Antworten