Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Events in Qt creator  (Read 7438 times)

0 Members and 1 Guest are viewing this topic.

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
SFML Events in Qt creator
« on: March 21, 2014, 08:05:59 am »
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
« Last Edit: March 23, 2014, 05:34:57 pm by delio »

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: SFML Events in Qt creator
« Reply #1 on: March 23, 2014, 05:34:00 pm »
Anybody know?
Please don't bored my stupid question.
I've really no idea .

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: SFML Events in Qt creator
« Reply #2 on: March 23, 2014, 06:19:39 pm »
Can you give us some more code? Not your whole code, just enough code that causes the problem.

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: SFML Events in Qt creator
« Reply #3 on: March 24, 2014, 08:12:48 am »
My source code : https://www.mediafire.com/?3yqk3cdi22m5cbv
I don't know what is important part to show to you, So I give all my source.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Events in Qt creator
« Reply #4 on: March 24, 2014, 09:08:38 am »
You should rather give us a minimal and complete example. Most people don't want to download a whole project on an external site and skim through it to guess what the mistake might be.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: SFML Events in Qt creator
« Reply #5 on: March 24, 2014, 12:35:54 pm »
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
 
« Last Edit: March 24, 2014, 02:04:29 pm by delio »

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: SFML Events in Qt creator
« Reply #6 on: March 24, 2014, 01:30:24 pm »
In your first post, you write:
cout << "work" << endl;

In your last post, your code contains:
cout << "work";

Last one will not print "work" unless you call std::endl sometime.

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: SFML Events in Qt creator
« Reply #7 on: March 24, 2014, 02:06:33 pm »
I've call endl, but nothing changed.

The Point is I want to mean that Qt can't detect event mouse wheel move.
« Last Edit: March 24, 2014, 02:28:53 pm by delio »

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: SFML Events in Qt creator
« Reply #8 on: March 25, 2014, 12:23:36 am »
In your first post, you write:
cout << "work" << endl;

In your last post, your code contains:
cout << "work";

Last one will not print "work" unless you call std::endl sometime.


First of all cout does not work with Qt, you must use the qDebug class (See official website for more detail)
http://qt-project.org/doc/qt5.1/qtcore/qdebug.html


Also, endl is only a keyword to say end line and does not affect console printing. It only write the following text on a new line.


I've call endl, but nothing changed.

The Point is I want to mean that Qt can't detect event mouse wheel move.

Secondly, Qt can detect mouse wheel event, using qScrollEvent. However, I can not say in general if the SFML event are supported in Qt.
« Last Edit: March 25, 2014, 12:27:43 am by math1992 »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: SFML Events in Qt creator
« Reply #9 on: March 25, 2014, 05:34:20 am »
Also, endl is only a keyword to say end line and does not affect console printing. It only write the following text on a new line.

That is not correct. It does affect console output quite a bit.
std::endl flushes the output stream - std::cout is (usually) buffered, so you may never see your output until you output std::endl (or std::flush if you don't want the newline). If you just want a newline without the flush, then output "\n".
Details here: http://en.cppreference.com/w/cpp/io/manip/endl
« Last Edit: March 25, 2014, 05:46:32 am by Jesper Juhl »

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: SFML Events in Qt creator
« Reply #10 on: March 26, 2014, 04:36:48 pm »
I found this

http://www.qtcentre.org/threads/52568-Qt5-native-messages-not-propogated

So, Qt don't receive event from third party?

Is there any c++ GUI creator that support all of SFML event?
and easy to use. Please recommend.

« Last Edit: March 26, 2014, 04:43:04 pm by delio »

poca

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: SFML Events in Qt creator
« Reply #11 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 :

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();
}