I can't use mousewheel event in Qt.
This is my code
while(RenderWindow::pollEvent(event)) {
if (event.type == sf::Event::MouseWheelMoved)
{
cout << "work" << endl;
}
}
When I scroll my mouse wheel, It won't cout "work".
How can I fix it
Thanks
Void OnUpdate
void MyCanvas::OnUpdate()
{
while(RenderWindow::pollEvent(event)) {
if (event.type == sf::Event::MouseWheelMoved)
{
cout << "works!" << endl ;
}
}
TileMap map;
if (!map.load("d:/tileset.png", sf::Vector2u(3, 3), level, x * 4, y * 4))
{
system("pause");
}
// Clear screen
RenderWindow::clear(sf::Color::White);
// Rotate the sprite
RenderWindow::draw(map);
mySprite.rotate(myClock.getElapsedTime().asSeconds() * 100.f);
// Draw it
for (int j = 0; j <= x * 4; ++j)
{
sf::Vertex outlinex1[2] =
{
sf::Vertex(sf::Vector2f(j * 3, 0), sf::Color(154,154,154)),
sf::Vertex(sf::Vector2f(j * 3, y * 12), sf::Color(154, 154, 154))
};
RenderWindow::draw(outlinex1, 2, sf::Lines);
}
for (int j = 0; j <= y * 4; ++j)
{
sf::Vertex outliney1[2] =
{
sf::Vertex(sf::Vector2f(0, j * 3), sf::Color(154, 154, 154)),
sf::Vertex(sf::Vector2f(x * 12, j * 3), sf::Color(154, 154, 154))
};
RenderWindow::draw(outliney1, 2, sf::Lines);
}
for (int i = 0; i <= x; ++i)
{
sf::Vertex outlinex[2] =
{
sf::Vertex(sf::Vector2f(i * 12, 0), sf::Color::Black),
sf::Vertex(sf::Vector2f(i * 12, y * 12), sf::Color::Black)
};
RenderWindow::draw(outlinex, 2, sf::Lines);
}
for (int i = 0; i <= y; ++i)
{
sf::Vertex outliney[2] =
{
sf::Vertex(sf::Vector2f(0, i * 12), sf::Color::Black),
sf::Vertex(sf::Vector2f(x * 12, i * 12), sf::Color::Black)
};
RenderWindow::draw(outliney, 2, sf::Lines);
}
myClock.restart();
}
Function that called OnUpdate
QSFMLCanvas::QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime) : QWidget(Parent),
myInitialized (false)
{
// Setup some states to allow direct rendering into the widget
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
// Set strong focus to enable keyboard events to be received
setFocusPolicy(Qt::StrongFocus);
// Setup the widget geometry
move(Position);
resize(Size);
// Setup the timer
myTimer.setInterval(FrameTime);
}
QSFMLCanvas::~QSFMLCanvas() {}
void QSFMLCanvas::showEvent(QShowEvent*)
{
if (!myInitialized)
{
// 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
//RenderWindow::create((void *) winId());
RenderWindow::create(reinterpret_cast<sf::WindowHandle>(winId()));
// Let the derived class do its specific stuff
OnInit();
// Setup the timer to trigger a refresh at specified framerate
connect(&myTimer, SIGNAL(timeout()), this, SLOT(repaint()));
myTimer.start();
myInitialized = true;
}
}
QPaintEngine* QSFMLCanvas::paintEngine() const
{
return 0;
}
void QSFMLCanvas::paintEvent(QPaintEvent*)
{
// Let the derived class do its specific stuff
OnUpdate();
// Display on screen
RenderWindow::display();
}
void QSFMLCanvas::OnInit() {}
void QSFMLCanvas::OnUpdate() {}
Thanks
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 :
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
}
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();
}
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();
}