Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML with Qt - Cannot create OpenGL context  (Read 3446 times)

0 Members and 1 Guest are viewing this topic.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
SFML with Qt - Cannot create OpenGL context
« on: January 21, 2015, 05:05:26 pm »
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.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: SFML with Qt - Cannot create OpenGL context
« Reply #1 on: January 25, 2015, 09:39:00 pm »
Since it has been a few days and noone has replied I'm assuming either noone can help or noone wants to help. In this case, would I be better off asking about this somewhere else?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML with Qt - Cannot create OpenGL context
« Reply #2 on: January 25, 2015, 09:52:15 pm »
Does it work if the SFML widget is the top-level one, instead of being a child of a QFrame?
Laurent Gomila - SFML developer

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: SFML with Qt - Cannot create OpenGL context
« Reply #3 on: January 26, 2015, 04:29:56 pm »
Removing the QFrame and default constructing QWidget in QTSFMLWindow results in the same behaviour.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: SFML with Qt - Cannot create OpenGL context
« Reply #4 on: January 31, 2015, 10:57:33 pm »
Its been almost a week, so I'm bumping this. Any help is appreciated.