1
Window / Re: SFML Events in Qt creator
« on: April 23, 2014, 10:46:07 am »
Hello I have a problem that I think is similar.
I have a sfml window as the central window of my MainWindow and i want my sfml window to handle the mouse events but it seems that sfml receive all the events : mouseMoved, keyPressed, even joystick events but not buttonPressedEvent. The Qt widget receive them but not the sfml window.
here's my code :
The smfl window which only handle events and print an image
the main :
I have a sfml window as the central window of my MainWindow and i want my sfml window to handle the mouse events but it seems that sfml receive all the events : mouseMoved, keyPressed, even joystick events but not buttonPressedEvent. The Qt widget receive them but not the sfml window.
here's my code :
SfmlWidget::SfmlWidget(QWidget *parent, frameTime) :
QWidget(parent),
myInitialized(false)
{
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
setFocusPolicy(Qt::StrongFocus);
myTimer.setInterval(frameTime);
}
SfmlWidget::~SfmlWidget() {}
#ifdef Q_WS_X11
#include <Qt/qx11info_x11.h>
#include <X11/Xlib.h>
#endif
void SfmlWidget::showEvent(QShowEvent *)
{
if (!myInitialized)
{
//si X11
#ifdef Q_WS_X11
XFlush(QX11Info::display());
#endif
sf::RenderWindow::create(winId());
onInit();
connect(&myTimer, SIGNAL(timeout()), this ,SLOT(repaint()));
myTimer.start();
myInitialized = true;
}
}
void SfmlWidget::onInit()
{
// Nothing to do by default...
}
void SfmlWidget::onUpdate()
{
// Nothing to do by default...
}
QPaintEngine* SfmlWidget::paintEngine() const
{
return 0;
}
void SfmlWidget::paintEvent(QPaintEvent *)
{
onUpdate();
window->display();
}
void MapWidget::mousePressEvent(QMouseEvent * event)
{
std::cerr << "mouse pressed Qt "; //Even handled correctly by Qt
}
QWidget(parent),
myInitialized(false)
{
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
setFocusPolicy(Qt::StrongFocus);
myTimer.setInterval(frameTime);
}
SfmlWidget::~SfmlWidget() {}
#ifdef Q_WS_X11
#include <Qt/qx11info_x11.h>
#include <X11/Xlib.h>
#endif
void SfmlWidget::showEvent(QShowEvent *)
{
if (!myInitialized)
{
//si X11
#ifdef Q_WS_X11
XFlush(QX11Info::display());
#endif
sf::RenderWindow::create(winId());
onInit();
connect(&myTimer, SIGNAL(timeout()), this ,SLOT(repaint()));
myTimer.start();
myInitialized = true;
}
}
void SfmlWidget::onInit()
{
// Nothing to do by default...
}
void SfmlWidget::onUpdate()
{
// Nothing to do by default...
}
QPaintEngine* SfmlWidget::paintEngine() const
{
return 0;
}
void SfmlWidget::paintEvent(QPaintEvent *)
{
onUpdate();
window->display();
}
void MapWidget::mousePressEvent(QMouseEvent * event)
{
std::cerr << "mouse pressed Qt "; //Even handled correctly by Qt
}
The smfl window which only handle events and print an image
#include <SFML/Graphics.hpp>
#include <iostream>
#include "testsfml.h"
testSfml::testSfml() :
sf::RenderWindow()
{
// I set the image to print
}
void testSfml::onInit()
{
clear();
displayAll();
display();
}
void testSfml::onUpdate()
{
sf::Event event;
while (pollEvent(event)) {
switch(event.type)
{
case (sf::Event::Closed) :
close();
break;
case (sf::Event::MouseButtonPressed) :
std::cerr << "mouse clicked"; // Event never handled
mousePressedEventHandling(event);
break;
case (sf::Event::MouseMoved) :
std::cerr << "mouse moved"; // Event handled
mouseMovedEventHandling(event);
break;
case (sf::Event::KeyPressed) :
std::cerr << "key pressed"; // Event handled
break;
case (sf::Event::JoystickButtonPressed) :
std::cerr << "joystick "; // Event handled
break;
default:
break;
}
}
display();
}
#include <iostream>
#include "testsfml.h"
testSfml::testSfml() :
sf::RenderWindow()
{
// I set the image to print
}
void testSfml::onInit()
{
clear();
displayAll();
display();
}
void testSfml::onUpdate()
{
sf::Event event;
while (pollEvent(event)) {
switch(event.type)
{
case (sf::Event::Closed) :
close();
break;
case (sf::Event::MouseButtonPressed) :
std::cerr << "mouse clicked"; // Event never handled
mousePressedEventHandling(event);
break;
case (sf::Event::MouseMoved) :
std::cerr << "mouse moved"; // Event handled
mouseMovedEventHandling(event);
break;
case (sf::Event::KeyPressed) :
std::cerr << "key pressed"; // Event handled
break;
case (sf::Event::JoystickButtonPressed) :
std::cerr << "joystick "; // Event handled
break;
default:
break;
}
}
display();
}
the main :
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow* window = new MainWindow();
SfmlWidget* wid = new SfmlWidget(window, 60);
window->setCentralWidget(wid);
window->show();
return app.exec();
}
{
QApplication app(argc, argv);
MainWindow* window = new MainWindow();
SfmlWidget* wid = new SfmlWidget(window, 60);
window->setCentralWidget(wid);
window->show();
return app.exec();
}