[Solved] QDesktopServices::openUrl segfaults (qt5, linux)



  • Newbie, having this simple program main.cpp:

    @
    #include <QString>
    #include <QDesktopServices>
    #include <QUrl>

    int main()
    {
    QString str = "http://qt-project.org/";
    QDesktopServices::openUrl(QUrl(str));
    return 0;
    }
    @

    and this .pro file:

    @
    QT += core gui

    TARGET = openTest
    TEMPLATE = app
    CONFIG += console

    SOURCES += main.cpp
    @

    It segfaults on the openUrl statement.
    Archlinux, standard Qt5.01 package.

    What am I doing wrong?

    thanks in advance!


  • Moderators

    Maybe it needs event loop to be present? I doubt it, though. Try with QUrl::fromUserInput();



  • Well, everything :)

    Your project setup is wrong, and the code in main.cpp is also wrong.
    You can not use QDesktopServices in a console application, and you are also missing QApplication instance.

    Here is correct .pro file:
    @
    QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = openTest
    CONFIG -= console

    TEMPLATE = app

    SOURCES += main.cpp
    @

    The correct main.cpp:
    @
    #include <QApplication>
    #include <QString>
    #include <QDesktopServices>
    #include <QUrl>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QString str = "http://qt-project.org/";
    QDesktopServices::openUrl(QUrl(str));
    return a.exec();
    }
    @



  • Thanks, that solved it. Turns out the a.exec() is not needed.

    Funny thing is that in qt4 the code I originally posted did work.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.