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

Author Topic: Qt & SFML  (Read 10920 times)

0 Members and 1 Guest are viewing this topic.

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Qt & SFML
« Reply #15 on: September 28, 2015, 02:58:33 pm »
Before trying to integrate SFML into a Qt GUI, did you first learn Qt alone? Have you already successfully created a GUI without SFML?
Absolutely, I actually had a Qt project and an SFML project, now I'm simply merging them and trying to integrate the SFML project into the Qt one. Compilation is no longer a problem, execution is.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Qt & SFML
« Reply #16 on: September 28, 2015, 03:06:56 pm »
So... if WindowFrame::SFMLWindow is not a SFML window, but instead, let's say, a QFrame, your application works flawlessly -- or at least doesn't block at startup?
Laurent Gomila - SFML developer

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Qt & SFML
« Reply #17 on: September 28, 2015, 05:09:25 pm »
My application doesn't block at startup if I give up using the method show.
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    WindowFrame::SFMLFrame = std::make_shared<QFrame>();
    WindowFrame::SFMLFrame->setWindowTitle("Qt SFML");
    WindowFrame::SFMLFrame->resize(GS::resWidth, GS::resHeight);
    WindowFrame::SFMLFrame->show();
   
    WindowFrame::SFMLWindow = std::make_shared<MyCanvas>(WindowFrame::SFMLFrame.get(), QPoint(0, 0), QSize(GS::resWidth, GS::resHeight));
    WindowFrame::SFMLWindow->show(); // blocks here
   
    return app.exec();
}

The following code doesn't block at all:
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    WindowFrame::SFMLFrame = std::make_shared<QFrame>();
    WindowFrame::SFMLFrame->setWindowTitle("Qt SFML");
    WindowFrame::SFMLFrame->resize(GS::resWidth, GS::resHeight);
    WindowFrame::SFMLFrame->show();
   
    WindowFrame::SFMLWindow = std::make_shared<MyCanvas>(WindowFrame::SFMLFrame.get(), QPoint(0, 0), QSize(GS::resWidth, GS::resHeight));
    //WindowFrame::SFMLWindow->show();
   
    return app.exec();
}
(... but the window is kept blank indefinitely.)

The following code doesn't block at all as well, but no window shows up:
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    WindowFrame::SFMLFrame = std::make_shared<QFrame>();
    WindowFrame::SFMLFrame->setWindowTitle("Qt SFML");
    WindowFrame::SFMLFrame->resize(GS::resWidth, GS::resHeight);
    //WindowFrame::SFMLFrame->show();
   
    WindowFrame::SFMLWindow = std::make_shared<MyCanvas>(WindowFrame::SFMLFrame.get(), QPoint(0, 0), QSize(GS::resWidth, GS::resHeight));
    WindowFrame::SFMLWindow->show();
   
    return app.exec();
}

Now, in either of these last two codes, if I later try to show the other widget (that is, WindowFrame::SFMLFrame for the third code, and WindowFrame::SFMLWindow for the second code) by pressing a QPushButton that is connected to a function that calls the show method, the program blocks just like it does in the first code.
« Last Edit: September 28, 2015, 05:16:35 pm by Law »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Qt & SFML
« Reply #18 on: September 28, 2015, 05:34:08 pm »
Quote
My application doesn't block at startup if I give up using the method show.
I already know that, this is not what I asked.
Laurent Gomila - SFML developer

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Qt & SFML
« Reply #19 on: September 28, 2015, 07:11:12 pm »
Quote
My application doesn't block at startup if I give up using the method show.
I already know that, this is not what I asked.
In this case there is no blocking. I see another blank window when I execute the program.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Qt & SFML
« Reply #20 on: September 28, 2015, 07:50:14 pm »
And have you used the debugger to find out where the execution is blocked?
Laurent Gomila - SFML developer

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Qt & SFML
« Reply #21 on: September 28, 2015, 08:34:26 pm »
I'm not sure if I did the right thing, but I step 1000'ed until the program blocked, then interrupted it and where'd:

(gdb) where
#0  0x00007ffff3e5f089 in _XReadEvents () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#1  0x00007ffff3e47777 in XIfEvent () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#2  0x00007ffff3e7e525 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#3  0x00007ffff3e803f6 in _XimProtoOpenIM () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#4  0x00007ffff3e847e8 in _XimOpenIM () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#5  0x00007ffff776f851 in sf::priv::WindowImplX11::WindowImplX11(unsigned long) () from /usr/local/lib/libsfml-window.so.2.3
#6  0x00007ffff7765211 in sf::priv::WindowImpl::create(unsigned long) () from /usr/local/lib/libsfml-window.so.2.3
#7  0x00007ffff7764e5e in sf::Window::create(unsigned long, sf::ContextSettings const&) () from /usr/local/lib/libsfml-window.so.2.3
#8  0x00000000004d525c in QSFMLCanvas::showEvent (this=0x160e2b0) at QSFMLCanvas.cpp:39
#9  0x00007ffff67cd2a7 in QWidget::event (this=0x160e2b0, event=0x7fffffffdc10) at kernel/qwidget.cpp:8161
#10 0x00007ffff6791c8c in QApplicationPrivate::notify_helper (this=this@entry=0x1192ba0, receiver=receiver@entry=0x160e2b0, e=e@entry=0x7fffffffdc10) at kernel/qapplication.cpp:3486
#11 0x00007ffff6796e56 in QApplication::notify (this=0x7fffffffdd20, receiver=0x160e2b0, e=0x7fffffffdc10) at kernel/qapplication.cpp:3236
#12 0x00007ffff5bcbc2d in QCoreApplication::notifyInternal (this=0x7fffffffdd20, receiver=receiver@entry=0x160e2b0, event=event@entry=0x7fffffffdc10) at kernel/qcoreapplication.cpp:881
#13 0x00007ffff67ca57e in sendEvent (event=0x7fffffffdc10, receiver=0x160e2b0) at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:232
#14 QWidgetPrivate::show_helper (this=this@entry=0x160e660) at kernel/qwidget.cpp:7164
#15 0x00007ffff67cc67a in QWidget::setVisible (this=0x160e2b0, visible=<optimized out>) at kernel/qwidget.cpp:7376
#16 0x000000000058cc7e in main (argc=1, argv=0x7fffffffdf18) at Menu.cpp:39

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Qt & SFML
« Reply #22 on: September 28, 2015, 10:03:56 pm »
Ah... a X11 problem at window creation. I guess only binary1248 can help then. Any chance to try the same code on Windows or OS X?
Laurent Gomila - SFML developer

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Qt & SFML
« Reply #23 on: September 29, 2015, 09:34:50 am »
What Linux distribution are you using? And what version of it? Reading around the internet, it looks like others had similar problems in the past. XCB and Xlib still contain bugs, and that is not even taking into account all of the buggy window managers out there. If you haven't already, make sure your system and all of your installed packages are up to date. This bug might have already been fixed by now.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Qt & SFML
« Reply #24 on: September 29, 2015, 01:54:49 pm »
Oh, well as I mentioned in post 6, I commented the XFlush thing because I thought I didn't need it (and also because I couldn't manage to find the right libraries and compile it). That might be the root of my problem then. I'm using Ubuntu 14.04 LTS.

The headers of QSFMLCanvas.cpp are now:
#include "QSFMLCanvas.hpp"
#include <QX11Info>
#include <X11/Xlib.h>
#include <iostream>

I uncommented the XFlush line and everything compiles just fine. When I execute the program though, I have the same problems. I'm going to check if the packages are up to date like binary said.

I updated everything and still have the problem :(

My current QSFMLCanvas.cpp file:
#include "QSFMLCanvas.hpp"
#include <QX11Info>
#include <X11/Xlib.h>
#include <iostream>

QSFMLCanvas::QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime) :
        QWidget(Parent),
        myInitialized(false)
{
        // Setup some states to allow direct rendering into the widget
        setAttribute(Qt::WA_PaintOnScreen);
        setAttribute(Qt::WA_OpaquePaintEvent);
        setAttribute(Qt::WA_NoSystemBackground);
        // Set strong focus to enable keyboard events to be received
        setFocusPolicy(Qt::StrongFocus);
        // Setup the widget geometry
        move(Position);
        resize(Size);
        // Setup the timer
        myTimer.setInterval(FrameTime);
}

QSFMLCanvas::QSFMLCanvas(void)
{
}

QSFMLCanvas::~QSFMLCanvas(void)
{
}

void QSFMLCanvas::showEvent(QShowEvent*)
{
        if (not 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
                XFlush(QX11Info::display());
                // Create the SFML window with the widget handle
                RenderWindow::create(static_cast<sf::WindowHandle>(winId()));
                // 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;
        }
       
        std::cout << "show event" << std::endl;
}

QPaintEngine* QSFMLCanvas::paintEngine(void) const
{
        std::cout << "paint engine" << std::endl;
        return 0;
}

void QSFMLCanvas::paintEvent(QPaintEvent*)
{
        // Let the derived class do its specific stuff
        OnUpdate();
        // Display on screen
        RenderWindow::display();
       
        std::cout << "paint event" << std::endl;
}
void QSFMLCanvas::OnInit(void)
{
}

void QSFMLCanvas::OnUpdate(void)
{
}

The std::cout lines are never reached.
« Last Edit: September 29, 2015, 06:33:52 pm by Law »

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Qt & SFML
« Reply #25 on: October 01, 2015, 04:04:21 pm »
So, what should I do? Is there some specific package that I should have installed? Program also seems to block at _XSend():

#0  0x00007ffff7ae0dab in _XSend () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#1  0x00007ffff7ae1089 in _XReadEvents () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#2  0x00007ffff7ac9777 in XIfEvent () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#3  0x00007ffff7b00525 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#4  0x00007ffff7b023f6 in _XimProtoOpenIM () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#5  0x00007ffff7b067e8 in _XimOpenIM () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#6  0x00007ffff743a851 in sf::priv::WindowImplX11::WindowImplX11(unsigned long) () from /usr/local/lib/libsfml-window.so.2.3
#7  0x00007ffff7430211 in sf::priv::WindowImpl::create(unsigned long) () from /usr/local/lib/libsfml-window.so.2.3
#8  0x00007ffff742fe5e in sf::Window::create(unsigned long, sf::ContextSettings const&) () from /usr/local/lib/libsfml-window.so.2.3
#9  0x00000000004d5329 in QSFMLCanvas::showEvent (this=0x160df80) at QSFMLCanvas.cpp:39
#10 0x00007ffff62942a7 in QWidget::event (this=0x160df80, event=0x7fffffffdc10) at kernel/qwidget.cpp:8161
#11 0x00007ffff6258c8c in QApplicationPrivate::notify_helper (this=this@entry=0x1192ba0, receiver=receiver@entry=0x160df80, e=e@entry=0x7fffffffdc10) at kernel/qapplication.cpp:3486
#12 0x00007ffff625de56 in QApplication::notify (this=0x7fffffffdd20, receiver=0x160df80, e=0x7fffffffdc10) at kernel/qapplication.cpp:3236
#13 0x00007ffff5692c2d in QCoreApplication::notifyInternal (this=0x7fffffffdd20, receiver=receiver@entry=0x160df80, event=event@entry=0x7fffffffdc10) at kernel/qcoreapplication.cpp:881
#14 0x00007ffff629157e in sendEvent (event=0x7fffffffdc10, receiver=0x160df80) at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:232
#15 QWidgetPrivate::show_helper (this=this@entry=0x160e330) at kernel/qwidget.cpp:7164
#16 0x00007ffff629367a in QWidget::setVisible (this=0x160df80, visible=<optimized out>) at kernel/qwidget.cpp:7376
#17 0x000000000058cd8c in main (argc=1, argv=0x7fffffffdf18) at Menu.cpp:39

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Qt & SFML
« Reply #26 on: October 01, 2015, 04:29:45 pm »
The fact that this stack is 1 layer deeper makes me think that your application isn't really blocking in the stricter sense. If you break and continue running the application in your debugger multiple times, you might notice that something is happening in the background. The thing is that you don't see anything happen window-wise and thus you think the program must have hung somewhere.

SFML makes a call to XOpenIM in its window initialization code. If XOpenIM doesn't return because it is waiting for some other X event to arrive that never does, SFML will not be able to proceed since it doesn't have any influence over that. The fact is that this isn't supposed to happen, even in this scenario where you are using Qt and SFML side by side. Something "deeper down" that SFML doesn't have control over is misbehaving and I can't think of any workarounds.

Like I said, I read on the internet that other people have had similar issues in the past that were attributed to bugs in Xlib and XCB. Some of those bugs have been fixed by now, however I don't know if those fixes have propagated into Ubuntu 14.04 LTS. You can try updating all your installed packages using your package manager. If they haven't, then you are out of luck. If you have another system at your disposal, you can test your code on an Ubuntu 14.10 or 15.04 system and see if the problem persists.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Qt & SFML
« Reply #27 on: October 01, 2015, 05:00:38 pm »
Thank you for taking the time to break it down for me.

It does seem that I'm out of luck. I have other computers (all Windows) at my disposal, but I would have to install everything (Qt, SFML, something that compiles C++ could be useful as well). Not to mention, I'm now used to Ubuntu and have gotten quite unfamiliar with Windows and C++ compilation in general. I think I'm going to use a laptop I have, install Ubuntu 15.04 on it and see how this plays out.

As a matter of fact, I have already encountered several un-explainable Qt problems with the computer I'm currently using. Qt::AlignJustify doesn't work for no reason, for instance. I've never had any problems with SFML though, as a result I've always assumed that most of my problems were due to Qt, not due to my computer. It's strange in a way because I chose 14.04 LTS, over the regular version of Ubuntu, to avoid silly problems, precisely.

I suppose I'll update my status in the near future :)

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Qt & SFML
« Reply #28 on: October 04, 2015, 08:23:08 pm »
Well I've tested my program on Ubuntu 15.04 and I have different problems. I -think- that the above problems are solved, but I can't be sure because I have another issue to take care of before I can finally see for myself.

It seems that sf::Texts cause problems when I try to sf::RenderWindow::draw them. Some traces:

./Menu(_Z7Handleri+0x56)[0x54916c]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x10d10)[0x7fe704668d10]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0xa4d2a)[0x7fe70437ad2a]
/usr/local/lib/libsfml-graphics.so.2.3(_ZNSt8_Rb_treeIjSt4pairIKjN2sf4Font4PageEESt10_Select1stIS5_ESt4lessIjESaIS5_EE29_M_get_insert_hint_unique_posESt23_Rb_tree_const_iteratorIS5_ERS1_+0x41)[0x7fe7064caea1]
/usr/local/lib/libsfml-graphics.so.2.3(_ZNK2sf4Font8getGlyphEjjb+0x2b5)[0x7fe7064ca0b5]
/usr/local/lib/libsfml-graphics.so.2.3(_ZNK2sf4Text20ensureGeometryUpdateEv+0x171)[0x7fe7064f4ca1]
/usr/local/lib/libsfml-graphics.so.2.3(_ZNK2sf4Text4drawERNS_12RenderTargetENS_12RenderStatesE+0x1b)[0x7fe7064f5c5b]
/usr/local/lib/libsfml-graphics.so.2.3(_ZN2sf12RenderTarget4drawERKNS_8DrawableERKNS_12RenderStatesE+0x39)[0x7fe7064ea8e9]

Even if I comment some segfaulting lines, sf::Text::getLocalBounds also seems to be a problem sometimes:

./Menu(_Z7Handleri+0x56)[0x54a3a0]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x10d10)[0x7fa60931fd10]
/usr/local/lib/libsfml-graphics.so.2.3(_ZNK2sf4Font14setCurrentSizeEj+0x13)[0x7fa60b17e383]
/usr/local/lib/libsfml-graphics.so.2.3(_ZNK2sf4Font20getUnderlinePositionEj+0x16)[0x7fa60b17e656]
/usr/local/lib/libsfml-graphics.so.2.3(_ZNK2sf4Text20ensureGeometryUpdateEv+0x134)[0x7fa60b1abc64]
/usr/local/lib/libsfml-graphics.so.2.3(_ZNK2sf4Text14getLocalBoundsEv+0xd)[0x7fa60b1acbad]

Again, this is some code that works perfectly on its own, if it is completely separated from Qt. I checked that separately on Ubuntu 15.04 too. Any ideas? :)

Thank you for your help so far!

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Qt & SFML
« Reply #29 on: October 04, 2015, 09:15:23 pm »
Tip: if you run those stacktraces through 'c++filt' they should become a lot more readable since the tool will undo (demangle) the symbol name mangling the compiler did.
« Last Edit: October 04, 2015, 09:21:10 pm by Jesper Juhl »