Here is the code
sfmlcanvas.h
#ifndef QSFMLCANVAS_H
#define QSFMLCANVAS_H
#include <QWidget>
#include <SFML/Graphics.hpp>
#include <QTimer>
class QSFMLCanvas : public QWidget, public sf::RenderWindow
{
//Q_OBJECT
public:
explicit QSFMLCanvas(QWidget *parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime = 1);
virtual void showEvent(QShowEvent*);
virtual QPaintEngine* paintEngine() const;
virtual void paintEvent(QPaintEvent*);
virtual ~QSFMLCanvas();
virtual void OnInit();
virtual void OnUpdate();
private:
QTimer myTimer;
bool myInitialized;
HWND _hWND;
protected:
virtual void resizeEvent(QResizeEvent * event );
};
#endif // QSFMLCANVAS_H
sfmlcanvas.cpp
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
_hWND = sf::WindowHandle( winId());
RenderWindow::create(_hWND);
// 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
display();
}
void QSFMLCanvas::OnInit() {}
void QSFMLCanvas::OnUpdate() {}
void QSFMLCanvas::resizeEvent(QResizeEvent *event){}
mycanvas.h
#ifndef MYCANVAS_H
#define MYCANVAS_H
#include "qsfmlcanvas.h"
#include <SFML/Graphics.hpp>
class MyCanvas : public QSFMLCanvas
{
public :
MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size);
void OnInit();
void OnUpdate();
void resizeEvent(QResizeEvent *event);
private :
sf::Clock myClock;
sf::RectangleShape rect;
sf::Font font;
sf::Text text;
};
#endif // MYCANVAS_H
mycanvas.cpp
#include "mycanvas.h"
#include <iostream>
#include <string>
#include <QDir>
#include <QWheelEvent>
#include <qdebug.h>
MyCanvas::MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size) : QSFMLCanvas(Parent, Position, Size)
{
}
void MyCanvas::OnInit()
{
myClock.restart();
}
void MyCanvas::OnUpdate()
{
// Clear screen
clear(sf::Color(69, 78, 83));
sf::Font font;
font.loadFromFile("arial.ttf");
// Create a text
sf::Text text("hello", font);
text.setCharacterSize(30);
text.setStyle(sf::Text::Bold);
text.setColor(sf::Color::Red);
text.setPosition(width() - 100,height()/2);
// Draw it
draw(text);
myClock.restart();
}
void MyCanvas::resizeEvent(QResizeEvent *event)
{
qDebug() << "RESIZED SFML";
// setSize(sf::Vector2u(event->size().width(),event->size().height()));
QWidget::resizeEvent(event);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include "MyCanvas.h"
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
MyCanvas Canvas;
private:
protected:
void resizeEvent( QResizeEvent * event);
void paintEvent(QPaintEvent *event);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QPainter>
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
Canvas(this,QPoint(5,20),QSize(width() - 20,height() - 40))
{
//setWindowFlags ( Qt :: FramelessWindowHint);
}
MainWindow::~MainWindow()
{
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
Canvas.move( 5, 22);
Canvas.resize(width() - 10,height() - 44);
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Just resize the window and you will see how the text disappear to the right.