QSFML
Public Member Functions | Protected Member Functions | List of all members
qsf::QSFMLWidget Class Reference

Qt widget used as SFML window. More...

#include <QSFMLCanvas.hpp>

Inheritance diagram for qsf::QSFMLWidget:
Inheritance graph
[legend]

Public Member Functions

 QSFMLWidget (QWidget *parent, const QPoint &Position, const QSize &Size, unsigned int FrameTime=7)
 Default constructor. More...
 
virtual ~QSFMLWidget ()
 Destructor. More...
 
virtual void showEvent (QShowEvent *)
 Basic widget init. More...
 
virtual QPaintEngine * paintEngine () const
 Does nothing. More...
 
virtual void paintEvent (QPaintEvent *)
 Manages OnUpdate() More...
 
virtual void OnInit ()
 Called after creating sf::RenderWindow.
 
virtual void OnUpdate ()
 Called every frame.
 
virtual void OnDestroy ()
 Called after destroying widget. More...
 
bool pollEvent (sf::Event &ev)
 Get event. More...
 

Protected Member Functions

bool isInitialized ()
 Is initialized? More...
 
sf::Time getDeltaTime ()
 Frame delta time. More...
 
void pushEvent (sf::Event &ev)
 Push event to queue. More...
 

Detailed Description

Qt widget used as SFML window.

This class inherits QWidget and sf::RenderWindow, so sometimes you have to specify which methods you want to use as some of them (eg. setSize) coexist in both. You can use this class to move your SFML apps into Qt widget without too big changes in code.

Constructor & Destructor Documentation

qsf::QSFMLWidget::QSFMLWidget ( QWidget *  parent,
const QPoint &  Position,
const QSize &  Size,
unsigned int  FrameTime = 7 
)
explicit

Default constructor.

Parameters
[in]parent– Parent widget
[in]Position– Defines QSFMLWidget position
[in]Size– Defines QSFMLWidget size
[in]FrameTimeOnUpdate() call interval

Sets widget Qt attributes, sets proper size and position, sets up frame timer (requred to measure delta time) and prepares QTimer calling OnUpdate() every FrameTime miliseconds.

qsf::QSFMLWidget::~QSFMLWidget ( )
virtual

Destructor.

Calls derived classes destructors due to virtual keyword and then OnDestroy() method of exactly this class, not derived one.

Member Function Documentation

sf::Time qsf::QSFMLWidget::getDeltaTime ( )
protected

Frame delta time.

Returns last frame duration, required to move object smoothly etc.

bool qsf::QSFMLWidget::isInitialized ( )
protected

Is initialized?

Returns wether widget was already initialized or not.

void qsf::QSFMLWidget::OnDestroy ( )
virtual

Called after destroying widget.

It needs to be implemented semi-explictly to work. OnDestroy() is called by destructor, so only derived class destructor can do so. To keep the code clean, use OnDestroy() to clean SFML-related things and destructor to free resources etc.

Use similar code to enable OnDestroy():

class SampleClass
: public qsf::QSFMLWidget {
// ...
~SampleClass() {
// ...
}
// ...
};
QPaintEngine * qsf::QSFMLWidget::paintEngine ( ) const
virtual

Does nothing.

Returns
0

Exists just to fit Qt requirements

void qsf::QSFMLWidget::paintEvent ( QPaintEvent *  )
virtual

Manages OnUpdate()

Parameters
[in](noname)– not used due to class structure
  • Measures delta time and saves it to _frameTime;
  • Calls OnUpdate();
  • Calls sf::RenderWindow::display().
bool qsf::QSFMLWidget::pollEvent ( sf::Event &  ev)

Get event.

Parameters
[out]ev– non-const reference to event
Returns
True if there was event to use, False otherwise

Checks if in the queue (actually vector) is any event, if no – returns false, otherwise copies event to given &ev and returns true.
Sample usage:

for(sf::Event ev; this->pollEvent(ev);){
if(ev.type == sf::Event::KeyPressed) {
std::cout << "Key pressed!" << std::endl;
}
// Handle other events
}
void qsf::QSFMLWidget::pushEvent ( sf::Event &  ev)
protected

Push event to queue.

Parameters
[in]ev– Reference to event that will be pushed to the queue

Copies given event to the end of queue (aka vector), use only if you have some specific source of events, eg. when you want to receive them from network etc.

void qsf::QSFMLWidget::showEvent ( QShowEvent *  )
virtual

Basic widget init.

Parameters
[in](noname)– not used due to class structure
  • Connects sf::RenderWindow to instance defined by *this;
  • Calls OnInit();
  • Connects QTimer to repaint() slot and starts it.

The documentation for this class was generated from the following files: