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

Author Topic: [Solved] Cant get SFML2 to work with Qt!  (Read 9557 times)

0 Members and 2 Guests are viewing this topic.

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« on: April 05, 2011, 03:27:40 pm »
Hi

Is there a way to attach files?

Anyway, here is my current code:

QSFMLCanvas.h
Code: [Select]
#ifndef QSFMLCANVAS_H
#define QSFMLCANVAS_H

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <Qt/qwidget.h>
#include <Qt/qtimer.h>


////////////////////////////////////////////////////////////
/// QSFMLCanvas allows to run SFML in a Qt control
////////////////////////////////////////////////////////////
class QSFMLCanvas : public QWidget, public sf::RenderWindow
{
public :

    ////////////////////////////////////////////////////////////
    /// Construct the QSFMLCanvas
    ///
    /// \param Parent :    Parent of the widget
    /// \param Position :  Position of the widget
    /// \param Size :      Size of the widget
    /// \param FrameTime : Frame duration, in milliseconds (0 by default)
    ///
    ////////////////////////////////////////////////////////////
    QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime = 0);

    ////////////////////////////////////////////////////////////
    /// Destructor
    ///
    ////////////////////////////////////////////////////////////
    virtual ~QSFMLCanvas();

private :

    ////////////////////////////////////////////////////////////
    /// Notification for the derived class that moment is good
    /// for doing initializations
    ///
    ////////////////////////////////////////////////////////////
    virtual void OnInit();

    ////////////////////////////////////////////////////////////
    /// Notification for the derived class that moment is good
    /// for doing its update and drawing stuff
    ///
    ////////////////////////////////////////////////////////////
    virtual void OnUpdate();

    ////////////////////////////////////////////////////////////
    /// Called when the widget is shown ;
    /// we use it to initialize our SFML window
    ///
    ////////////////////////////////////////////////////////////
    virtual void showEvent(QShowEvent*);

    ////////////////////////////////////////////////////////////
    /// Called when the widget needs to be painted ;
    /// we use it to display a new frame
    ///
    ////////////////////////////////////////////////////////////
    virtual void paintEvent(QPaintEvent*);

    ////////////////////////////////////////////////////////////
    // Member data
    ////////////////////////////////////////////////////////////
    QTimer myTimer;       ///< Timer used to update the view
    bool   myInitialized; ///< Tell whether the SFML window has been initialized or not
};


#endif // QSFMLCANVAS_H


QSFMLCanvas.cpp
Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "QSFMLCanvas.h"

// Platform-specific headers
#ifdef Q_WS_X11
    #include <Qt/qx11info_x11.h>
    #include <X11/Xlib.h>
#endif


////////////////////////////////////////////////////////////
/// Construct the QSFMLCanvas
////////////////////////////////////////////////////////////
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_NoSystemBackground);

    // Setup the widget geometry
    move(Position);
    resize(Size);

    // Setup the timer
    myTimer.setInterval(FrameTime);
}


////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
QSFMLCanvas::~QSFMLCanvas()
{
    // Nothing to do...
}


////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing initializations
////////////////////////////////////////////////////////////
void QSFMLCanvas::OnInit()
{
    // Nothing to do by default...
}


////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing its update and drawing stuff
////////////////////////////////////////////////////////////
void QSFMLCanvas::OnUpdate()
{
    // Nothing to do by default...
}


////////////////////////////////////////////////////////////
/// Called when the widget is shown ;
/// we use it to initialize our SFML window
////////////////////////////////////////////////////////////
void QSFMLCanvas::showEvent(QShowEvent*)
{
    if (!myInitialized)
    {
        // Create the SFML window with the widget handle
        Create(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;
    }
}


////////////////////////////////////////////////////////////
/// Called when the widget needs to be painted ;
/// we use it to display a new frame
////////////////////////////////////////////////////////////
void QSFMLCanvas::paintEvent(QPaintEvent*)
{
    // Let the derived class do its specific stuff
    OnUpdate();

    // Display on screen
    Display();
}


main.cpp
Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "QSFMLCanvas.h"
#include <Qt/qapplication.h>
#include <Qt/qframe.h>


////////////////////////////////////////////////////////////
/// Custom SFML canvas
////////////////////////////////////////////////////////////
class MyCanvas : public QSFMLCanvas
{
public :

    ////////////////////////////////////////////////////////////
    /// Construct the canvas
    ///
    ////////////////////////////////////////////////////////////
    MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size) :
    QSFMLCanvas(Parent, Position, Size)
    {

    }

private :

    ////////////////////////////////////////////////////////////
    /// /see QSFMLCanvas::OnInit
    ///
    ////////////////////////////////////////////////////////////
    void OnInit()
    {
        // Change the background color

    }

    ////////////////////////////////////////////////////////////
    /// /see QSFMLCanvas::OnUpdate
    ///
    ////////////////////////////////////////////////////////////
    void OnUpdate()
    {

    }

    ////////////////////////////////////////////////////////////
    /// Member data
    ////////////////////////////////////////////////////////////
};


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
    QApplication App(argc, argv);

    // Create the main frame
    QFrame* MainFrame = new QFrame;
    MainFrame->setWindowTitle("Qt SFML");
    MainFrame->resize(400, 400);
    MainFrame->show();

    // Create a SFML view inside the main frame
    MyCanvas* SFMLView = new MyCanvas(MainFrame, QPoint(20, 20), QSize(360, 360));
    SFMLView->show();

    return App.exec();
}


My Qt Creator project file:
Code: [Select]

#-------------------------------------------------
#
# Project created by QtCreator 2011-04-05T12:51:44
#
#-------------------------------------------------

QT       += core gui

TARGET = LekeLand
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp \
    QSFMLCanvas.cpp

HEADERS  += widget.h \
    QSFMLCanvas.h

FORMS    += widget.ui

INCLUDEPATH += /home/agrualon/SFML-2.0-build/include
LIBS += /home/agrualon/SFML-2.0-build/lib/libsfml-system.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-window.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-graphics.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-audio.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-network.so


The problem is that when I build it, it returns:
Code: [Select]

collect2: id returned 1 exit status


Nothing more..

I created the project using Qt Creator, File -> New File or Project -> Qt C++ Project. In the wizard I selected QWidget as my base-class..

Any halp?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Cant get SFML2 to work with Qt!
« Reply #1 on: April 05, 2011, 03:34:59 pm »
Quote
Is there a way to attach files?

Nop.

Quote
Code: [Select]
LIBS += /home/agrualon/SFML-2.0-build/lib/libsfml-system.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-window.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-graphics.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-audio.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-network.so

Should be:
Code: [Select]
LIBS += -L/home/agrualon/SFML-2.0-build/lib -lsfml-graphics -lsfml-window -lsfml-network -lsfml-audio -lsfml-system

Quote
collect2: id returned 1 exit status

Is there something more precise if you look at the direct compiler output, rather than the formatted one?
Laurent Gomila - SFML developer

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« Reply #2 on: April 06, 2011, 12:01:42 am »
Ah, didn't see that tab-control.

Here is the compiler outpur

Code: [Select]

Running build steps for project LekeLand...
Configuration unchanged, skipping qmake step.
Starting: "/usr/bin/make" -w
make: Entering directory `/home/agrualon/LekeLand/LekeLand-build-desktop'
/usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ CONFIG+=debug -o Makefile ../LekeLand/LekeLand.pro
make: Leaving directory `/home/agrualon/LekeLand/LekeLand-build-desktop'
make: Entering directory `/home/agrualon/LekeLand/LekeLand-build-desktop'
g++  -o LekeLand main.o widget.o QSFMLCanvas.o    -L/usr/lib -L/home/agrualon/SFML-2.0-build/lib -lsfml-graphics -lsfml-window -lsfml-network -lsfml-audio -lsfml-system -lQtGui -lQtCore -lpthread
widget.o:(.rodata._ZTV6Widget[vtable for Widget]+0x7c): undefined reference to `Widget::paintEvent(QPaintEvent*)'
widget.o:(.rodata._ZTV6Widget[vtable for Widget]+0xe0): undefined reference to `Widget::OnInit()'
widget.o:(.rodata._ZTV6Widget[vtable for Widget]+0xe4): undefined reference to `Widget::OnUpdate()'
collect2: ld returned 1 exit status
make: Leaving directory `/home/agrualon/LekeLand/LekeLand-build-desktop'
make: *** [LekeLand] Error 1
The process "/usr/bin/make" exited with code %2.
Error while building project LekeLand (target: Desktop)
When executing build step 'Make'

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« Reply #3 on: April 06, 2011, 12:31:17 am »
Nevermind this post.

Edit:
Tried to compile the example only, just editing a few things.. Now I get

Code: [Select]

Starting /home/agrualon/LekeLand/QtGuiTest-build-desktop/LekeLand...
/home/agrualon/LekeLand/QtGuiTest-build-desktop/LekeLand: error while loading shared libraries: libsfml-graphics.so.2.0: cannot open shared object file: No such file or directory
/home/agrualon/LekeLand/QtGuiTest-build-desktop/LekeLand exited with code 127


which I think is cause of some errors in here
Code: [Select]

QT += core gui

TARGET = LekeLand
TEMPLATE = app

SOURCES += graphics-qt.cpp \
    QSFMLCanvas.cpp

HEADERS  += \
    QSFMLCanvas.hpp

INCLUDEPATH += /home/agrualon/SFML-2.0-build/include

LIBS += -L/home/agrualon/SFML-2.0-build/lib -lsfml-graphics -lsfml-window -lsfml-network -lsfml-audio -lsfml-system



Am I rite?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Cant get SFML2 to work with Qt!
« Reply #4 on: April 06, 2011, 07:59:04 am »
The directory containing the SFML libraries has to be in LD_LIBRARY_PATH so that they can be found when starting your executable.
Laurent Gomila - SFML developer

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« Reply #5 on: April 06, 2011, 07:18:23 pm »
Project (at the left side) -> Build Env. Click the details button, add it.


Now I get "Failed to activate the window's context" in the log, while the program is running. And in the SFML context there is only a frozen picture of the images behind the context @ startup.

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« Reply #6 on: April 06, 2011, 09:37:52 pm »
Okey, did some more research. This seems to be the first 7 lines the logger puts out.. after that it just spams the last message..

Code: [Select]

&"warning: GDB: Failed to set controlling terminal: Invalid argument\n"
Failed to get the window attributes
Failed to get the window attributes
Failed to activate the window's context
Failed to activate the window's context
Failed to activate the window's context

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« Reply #7 on: April 07, 2011, 11:27:34 am »
Okey, got it working now.

But now I am having trouble registering events.

Ex. a button
Code: [Select]

int main(int argc, char **argv)
{
    QApplication App(argc, argv);

    // Create the main frame
    QFrame* MainFrame = new QFrame;
    MainFrame->setWindowTitle("Qt SFML");
    MainFrame->resize(15+200+800, 610);
    MainFrame->show();

    // Create a SFML view inside the main frame
    MyCanvas* SFMLView = new MyCanvas(MainFrame, QPoint(210, 5), QSize(800, 600));
    SFMLView->show();

    QPushButton* btn = new QPushButton(MainFrame);
    btn->resize( 100 , 100 );
    btn->move( 0 , 0 );
    btn->show();

    return App.exec();
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Cant get SFML2 to work with Qt!
« Reply #8 on: April 07, 2011, 11:31:38 am »
Quote
Okey, got it working now.

How? It might help others that have similar problems.

Quote
But now I am having trouble registering events.

Well, if you don't say what's wrong we won't be able to find a solution ;)
What events are you talking about? What do you expect the code above to do?
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
[Solved] Cant get SFML2 to work with Qt!
« Reply #9 on: April 07, 2011, 12:35:22 pm »
He linked the wrong Qt libs (via chat yesterday). ;)

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« Reply #10 on: April 07, 2011, 01:35:02 pm »
Thanks Tank, forgot to mention that sorry :/

Well, lets say I want to execute the function "Clicked()" which is declared inside the "MyCanvas" class when the pushbutton is clicked.

Need more info?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Cant get SFML2 to work with Qt!
« Reply #11 on: April 07, 2011, 01:49:07 pm »
Quote
Well, lets say I want to execute the function "Clicked()" which is declared inside the "MyCanvas" class when the pushbutton is clicked.

And what's your problem? This is regular Qt code, connect signals and slots and that's it. Read the doc if you're not familiar with signals and slots.
Laurent Gomila - SFML developer

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« Reply #12 on: April 07, 2011, 02:19:29 pm »
Well, as far as I understand this should be right

Code: [Select]

    g_guiBtn = new QPushButton( "Test Button" , g_guiMainFrame );
    g_guiBtn->show();

    g_guiBtn->connect( g_guiBtn , SIGNAL(clicked()) , g_guiSFML , SLOT(MYEVENT()) );


and inside MyCanvas
Code: [Select]

public slots:
    void MYEVENT()
    {
        this->g_Draw = false;
        this->g_ClearColor = sf::Color::Red;
    }


still clear color dosen't change..

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Cant get SFML2 to work with Qt!
« Reply #13 on: April 07, 2011, 02:39:54 pm »
Can you show a complete and minimal code that reproduces this problem?
Laurent Gomila - SFML developer

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
[Solved] Cant get SFML2 to work with Qt!
« Reply #14 on: April 07, 2011, 02:50:42 pm »
Okey, posting from my android atm - Im at the store.

Well, last thing I tested before I left was that the
Code: [Select]

g_guiMainFrame->connect( g_guiBtn , SIGNAL(pressed()) , g_guiSFML , SLOT(hide()));


That made the SFML window go away(as it should), but my own declared slots doesn't work.

 

anything