QT SFML C++

Started by
26 comments, last by Saint Squireen 11 years, 9 months ago
I have been trying to figure out how to sync/link SFML and QT together by making a custom widget and following the advice on the tutorials page of

http://www.sfml-dev.org/tutorials/1.3/graphics-qt.php

and I just dont understand it. Ive never tried to make a custom widget before and I apparently cant wrap my mind around whats being shown and what I need to do.

Could somebody please try to help me understand this?

~Saint Squireen
Advertisement
You linked to the SFML 1.3 version tutorial. The latest stable version of SFML is 1.6, and the current officially unofficial build is version 2.0.
Which version of SFML are you actually using? Also, which version of Qt are you using? Most likely Qt would be either: 4.6.x, 4.7.x, 4.8.x, or (unofficial) 5.0.x.
Well i wanted to use SFML version 2.0 but I swapped that idea with the one to download and use SFML 1.6 for windows(64bit) because I think it will be much less riskier. Also I have never used SFML before. The QT that I have downloaded says it is 2.4.0 but thats not one of your acceptable answers so I dont have a clue. I have been working on QT for a very,VERY short amout of time.

~Saint Squireen
The Qt you downloaded was probably the QtSDK 2.4.0 which is the version of QtCreator (I think?) but ships with the Qt api version 4.7.x or there-abouts, so you're fine.

If you are using SFML 1.6, you want to use this tutorial to be up to date.

Essentially, Qt and SFML are both two separate APIs, and gluing them together is definitely possible (my own game does that), but neither is designed with that in mind, which is why it may seem weird to you.

Do you understand C++ inheritance? The class being created in the SFML tutorial is inheriting a QWidget (from the Qt api) to override some virtual functions QWidget provides, so the new class can interact with the Qt API and other Qt widgets. The new class also inherits a sf::RenderWindow (from the SFML api) to allow SFML to control the drawing and input of that class, when Qt tells the class that it has input or that it is ready to draw.

Qt controls the lifetime of the widget, and calls the virtual functions, but SFML handles the virtual functions when called.
The class has to set some Qt-specific attributes to let Qt know that Qt itself won't be handling the drawing, among other things. Qt also uses something called Signals-and-Slots, which is a non-standard addition to C++. SFML takes advantage of that, to tell Qt to re-render the window at a specific framerate (this is the "connect(.., SIGNAL(...), ..., SLOT(...));" line of code.

Looking at the tutorial, is there a particular piece of code that you have questions about? There are a number of oddities in there, if you've never used Qt before.
First off, thanks for countinuously responding to my questions!!! And secondly, I do understand inheritance(at least I am pretty sure I do). I know what signal and slot do but from that point on (both in standard addition C++, SFML,QT)I know practically nothing else. Now Im just going to review the link you posted and Ill come back here in a couple minutes to update you. Thanks again!!!!!

~Saint Squireen
Actually I do have a question, and this is probably the biggest and probably the noobiest/stupidest question ever:

Where do I make these projects or builds? :D

~Saint Squireen
Using Qt Creator, to start a new project using the Qt API:
File -> New file or project -> Qt Widget Project -> Qt Gui Application

This will start a wizard to create a project.

First name your project and choose where to place the project, hit "Next" and on the "Target Setup" screen, select just two builds - one for release, and one for debug - using the same MinGW install and the same Qt api version.

The final page of the wizard sets up a Qt main window, and asks you to name it. The defaults are fine, but you can name it something different if you like. Uncheck "Generate form", if your SFML window is going to be your sole Qt window. Hit 'Next', and then the last page can be skipped if you're not using a version control system.

This will create an initial project, which includes your .pro file. Your .pro file is the makefile that QMake uses to compile your project.

It also creates a main.cpp, mainwindow.cpp, and mainwindow.h. Leave main.cpp as it is, but you can modify the 'MainWindow' class to be like the SFML tutorial describes, first changing:
//In the header:
#include <QtGui/QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};

//In the source file:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}


To this instead:
//In the header:
#include <QWidget> //I changed <QtGui/QMainWindow> to <QWidget>

class MainWindow : public QWidget //I changed QMainWindow to QWidget
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};

//In the source file:
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent) //I changed QMainWindow to QWidget
{
}


Then you can follow the tutorial for the SFML-specific tweaks.
In your .pro file, you need to add some variables: (this is in addition to the other variables already present in the .pro file)
#Location to save your executable:
DESTDIR = "../MyPath"

#Name of your executable:
TARGET = MyGame

#SFML libraries:
LIBS += -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system -lsfml-main

#Path to the linker import files: (sfml-audio.a or whatever)
LIBS += -L"D:/my/path/to/SFML/lib/"

#Path to SFML headers:
INCLUDEPATH = "D:/my/path/to/SFML/include/"

Well i wanted to use SFML version 2.0 but I swapped that idea with the one to download and use SFML 1.6 for windows(64bit) because I think it will be much less riskier.

For what it's worth, using SFML 2.0 is probably less risky than using 1.6, actually. Even though SFML 2.0 isn't technically "done," Laurent himself has said that SFML 1.6 is deprecated. It's ok if you go with 1.6 (the world will keep on revolving just fine), but I'd personally recommend upgrading to 2.0.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Okay so I have just finished up word for word what you have done and did a bunch of copy and pasting. The only part I did not get, was when you said to look for SFML specific tweaks.... Could you clarify a little bit more? Also in that last picture of the variables that needed to go into my "project".pro class, is it okay if I copied the whole picture and pasted it into my "project".pro?

ps- Thanks so much again!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! smile.png

pps-Cornstalks-Thanks for the insight but I think I might just stick with 1.6 for now just to get started. smile.png



ppps-I did nothing else to the project besides copy and paste what you said for me to do and my question now is, "what now?". Do I just save it? Do I just run it?

~Saint Squireen
Well, did you follow the tutorial yet?

What I explained above was to get you to a starting place from which you can follow the tutorial. By "SFML-specific tweaks", I meant the tutorial SFML provides that walks through how to get SFML running with Qt. When following the tutorial, if you don't understand something, then let us know - but until you follow the tutorial, there's no point in us walking you through the exact same steps the tutorial walks you through, because the tutorial will do a superior job in explaining things. smile.png

You're definitely welcome to copy+paste the .pro settings I showed you. Ofcourse, you have to fill in the details with what works for your environment, because "D:/my/path/to/SFML/lib/" doesn't actually exist on your computer. You have to replace it with the path to wherever you installed your copy of SFML's .a files. Same with "D:/my/path/to/SFML/include/" - that location doesn't actually exist, you have to replace it with the real location that is specific to where you installed SFML's header files.

This topic is closed to new replies.

Advertisement