I use Qt 4.8 with SFML 2.0, but I just migrated to Qt 5.0. I'm programming on Windows 7, 32 bit, and I use MinGW (4.7.2) as my compiler.
The basic layout of my application is a QWidget as the "main window", with several QWidget side panels, and in the center, a QWidget mixed with SFML ("sfml window"), that SFML draws and handles events for.
In Qt 4.8, SFML keyboard events for that central SFML window worked just fine, but in Qt 5.0, the keyboard events aren't reaching SFML. The keyboard events are reaching the QWidget that SFML is attached to (I breakpointed on keyPressEvent() to test), but SFML's keyboard events stopped working in Qt 5. Not all events, mouse events work fine, but the keyboard events don't get forwarded to SFML by Qt.
I even tried to grabKeyboard() on the QWidget/SFML window to check. Didn't help.
class AreaWindow
: public QWidget,
public sf
::RenderWindow{
Q_OBJECT
public:
AreaWindow();
~AreaWindow();
//...other functions...
}
class AreaWindow : public QWidget, public sf::RenderWindow
{
Q_OBJECT
public:
AreaWindow();
~AreaWindow();
//...other functions...
}
To copy to clipboard, switch view to plain text mode
AreaWindow::AreaWindow()
: QWidget(nullptr
), isInitialized
(false) {
//Setup some states to allow direct rendering into the widget
QWidget::setAttribute(Qt
::WA_PaintOnScreen);
QWidget::setAttribute(Qt
::WA_OpaquePaintEvent);
QWidget::setAttribute(Qt
::WA_NoSystemBackground);
QWidget::setAttribute(Qt
::WA_PaintUnclipped);
//Set strong focus to enable keyboard events to be received
QWidget::setFocusPolicy(Qt
::StrongFocus);
}
{
if(!this->isInitialized)
{
//Under X11, we need to flush the commands sent to the server to ensure that
//SFML will get an updated view of the windows
#ifdef Q_WS_X11
#endif
//Create the SFML window with the widget handle
sf::RenderWindow::create((HWND)this->winId());
this->isInitialized = true;
}
}
{
//We make the paintEvent function return a null paint engine. This functions works together with
//the WA_PaintOnScreen flag to tell Qt that we're not using any of its built-in paint engines.
return nullptr;
}
{
//Clear the window (using SFML).
sf::RenderWindow::clear(Global::Window::ClearColor.ToSfmlColor());
//Start the camera off on World mode.
this->SetCameraMode(CameraMode::World);
//...draw code goes here...
//Display on screen (using SFML).
sf::RenderWindow::display();
}
AreaWindow::AreaWindow()
: QWidget(nullptr), isInitialized(false)
{
//Setup some states to allow direct rendering into the widget
QWidget::setAttribute(Qt::WA_PaintOnScreen);
QWidget::setAttribute(Qt::WA_OpaquePaintEvent);
QWidget::setAttribute(Qt::WA_NoSystemBackground);
QWidget::setAttribute(Qt::WA_PaintUnclipped);
//Set strong focus to enable keyboard events to be received
QWidget::setFocusPolicy(Qt::StrongFocus);
}
void AreaWindow::showEvent(QShowEvent*)
{
if(!this->isInitialized)
{
//Under X11, we need to flush the commands sent to the server to ensure that
//SFML will get an updated view of the windows
#ifdef Q_WS_X11
XFlush(QX11Info::display());
#endif
//Create the SFML window with the widget handle
sf::RenderWindow::create((HWND)this->winId());
this->isInitialized = true;
}
}
QPaintEngine *AreaWindow::paintEngine() const
{
//We make the paintEvent function return a null paint engine. This functions works together with
//the WA_PaintOnScreen flag to tell Qt that we're not using any of its built-in paint engines.
return nullptr;
}
void AreaWindow::paintEvent(QPaintEvent *event)
{
//Clear the window (using SFML).
sf::RenderWindow::clear(Global::Window::ClearColor.ToSfmlColor());
//Start the camera off on World mode.
this->SetCameraMode(CameraMode::World);
//...draw code goes here...
//Display on screen (using SFML).
sf::RenderWindow::display();
}
To copy to clipboard, switch view to plain text mode
To be clear, this worked in Qt 4.8 - some change between Qt 4.8 and Qt 5.0 stopped native Win32 keyboard events from being forwarded to SFML. Any ideas?
[Edit:] SFML also isn't receiving the focus/unfocus events (sf::Event::GainedFocus and sf::Event::LostFocus), even though the QWidget is (QWidget::focusInEvent(), QWidget::focusOutEvent()).
This is not an SFML-related problem, the QWidget is stopping the Win32 events (it seems to me).
Bookmarks