SFML community forums
Help => Window => Topic started by: hayer on April 05, 2011, 03:27:40 pm
-
Hi
Is there a way to attach files?
Anyway, here is my current code:
QSFMLCanvas.h
#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
////////////////////////////////////////////////////////////
// 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
////////////////////////////////////////////////////////////
// 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:
#-------------------------------------------------
#
# 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:
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?
-
Is there a way to attach files?
Nop.
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:
LIBS += -L/home/agrualon/SFML-2.0-build/lib -lsfml-graphics -lsfml-window -lsfml-network -lsfml-audio -lsfml-system
collect2: id returned 1 exit status
Is there something more precise if you look at the direct compiler output, rather than the formatted one?
-
Ah, didn't see that tab-control.
Here is the compiler outpur
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'
-
Nevermind this post.
Edit:
Tried to compile the example only, just editing a few things.. Now I get
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
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?
-
The directory containing the SFML libraries has to be in LD_LIBRARY_PATH so that they can be found when starting your executable.
-
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.
-
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..
&"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
-
Okey, got it working now.
But now I am having trouble registering events.
Ex. a button
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();
}
-
Okey, got it working now.
How? It might help others that have similar problems.
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?
-
He linked the wrong Qt libs (via chat yesterday). ;)
-
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?
-
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.
-
Well, as far as I understand this should be right
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
public slots:
void MYEVENT()
{
this->g_Draw = false;
this->g_ClearColor = sf::Color::Red;
}
still clear color dosen't change..
-
Can you show a complete and minimal code that reproduces this problem?
-
Okey, posting from my android atm - Im at the store.
Well, last thing I tested before I left was that the
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.
-
Are you sure that your class has the Q_OBJECT macro?
-
Solved;
Put the MyCanvas into a .cpp and .h file like any other class and add the Q_OBJECT.
If your using Qt Creator, Clean project, build -> works :D
-
How did you manage to link the libraries in QT?
Regards,
Ivelin Penchev