I'm trying to get SFML 2.2 working with Qt 5.4 (Free) but it is giving me some problems. I've tried researching the problem I'm currently having but it seems I'm the only one having it so I guess I'm doing something wrong.
The error is:
Failed to set pixel format for device context -- cannot create OpenGL context. I understand that this is an SFML error, however, all of my other SFML projects work perfectly fine, so I'm assuming this is to do with how I'm using Qt.
My current project consists of 3 files, main.cpp, QTSFMLWindow.cpp/hpp. I have attached the source code and a test image, but I'll paste the code here as well.
main.cpp
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qframe.h>
#include "QTSFMLWindow.hpp"
#include <memory>
int main(int argc, char** argv)
{
QApplication oQApp(argc, argv);
QFrame oWindowFrame;
oWindowFrame.setWindowTitle("QT SFML Test");
oWindowFrame.resize(800, 600);
oWindowFrame.show();
QTSFMLWindow oWindow{oWindowFrame, {5, 5}, {oWindowFrame.size().width() / 2u, oWindowFrame.height() - 10u}};
oWindow.show();
system("pause"); // uncomment this line for the proper error
return oQApp.exec();
}
QTSFMLWindow.hpp
#ifndef QTSFMLWINDOW_HPP
#define QTSFMLWINDOW_HPP
#include <SFML/Graphics.hpp>
#include <QtWidgets/qwidget.h>
#include <QtCore/qtimer.h>
#include <cstdint>
#include <memory>
class QTSFMLWindow : public QWidget, public sf::RenderWindow
{
private:
bool m_bInitialized;
QTimer m_oQTimer;
sf::Texture m_oTexture;
sf::Sprite m_oSprite;
public:
QTSFMLWindow(QWidget& roParent, const sf::Vector2f v2fOffset, const sf::Vector2u v2uSize);
~QTSFMLWindow();
void showEvent(QShowEvent* poEvent);
void paintEvent(QPaintEvent* poPaintEvent);
};
#endif // QTSFMLWINDOW_HPP
QTSFMLWindow.cpp
#include "QTSFMLWindow.hpp"
#include <iostream>
QTSFMLWindow::QTSFMLWindow(QWidget& roParent, const sf::Vector2f v2fOffset, const sf::Vector2u v2uSize)
: QWidget{&roParent}
, m_bInitialized{false}
{
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
setFocusPolicy(Qt::StrongFocus);
resize({static_cast<int>(v2uSize.x), static_cast<int>(v2uSize.y)});
move({static_cast<int>(v2fOffset.x), static_cast<int>(v2fOffset.y)});
m_oQTimer.setInterval(0);
if (!m_oTexture.loadFromFile("vent.png")) {
std::cout << "vent loading failed" << std::endl;
return;
}
m_oSprite.setTexture(m_oTexture);
m_oSprite.setPosition({5, 5});
}
QTSFMLWindow::~QTSFMLWindow()
{
//
}
void QTSFMLWindow::showEvent(QShowEvent* poEvent)
{
if (m_bInitialized)
return;
sf::RenderWindow::create({WId()});
connect(&m_oQTimer, SIGNAL(timeout()), this, SLOT(repaint()));
m_oQTimer.start();
m_bInitialized = true;
}
void QTSFMLWindow::paintEvent(QPaintEvent* poPaintEvent)
{
clear(sf::Color::White);
draw(m_oSprite);
display();
}
The clear, draw and display functions are all called when they should be (Each frame, stepped through with a debugger), however nothing gets rendered.
The error:
Failed to activate the window's context is also spammed twice in the console per frame and also when the application changes focus, but I'm assuming it is related to the original error.
I understand this should probably be in General Help or something but considering that the error message is in the graphics part of the code, I thought I would post it here. Any help resolving this issue would be appreciated.