[Solved]QDesktopServices::openUrl crashes
-
I have a small application which just starts a local server, waits for a connection and then launches another application.
Here's the complete source:
@
#ifndef SERVER_H
#define SERVER_H
#include <QObject>
#include <QLocalSocket>
#include <QLocalServer>
#include <QDesktopServices>
#include <QDir>
#include <QUrl>
#include <QDebug>class server : public QObject
{
Q_OBJECT
signals:
void shutdown();
private:
QLocalServer* server_;
private slots:
void executeIt(){
QLocalSocket *clientConnection = server_->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
clientConnection->disconnectFromServer();QString appPath="file:///"+QDir::currentPath()+"/"+"app.exe"; qDebug() << appPath; QDesktopServices::openUrl(QUrl(appPath,QUrl::TolerantMode)); //QDesktopServices::openUrl(QUrl::fromLocalFile(appPath)); emit shutdown(); }
public:
explicit server(QObject *parent = 0){
server_ = new QLocalServer(this);
connect(server_,SIGNAL(newConnection()),this,SLOT(executeIt()));
server_->listen("myapp/starter");
}
};#endif // SERVER_H
@@
#include "server.h"
#include <QCoreApplication>int main(int argc, char argv[])
{
QCoreApplication a(argc, argv);
server serv=new server();
QObject::connect(serv,SIGNAL(shutdown()),&a,SLOT(quit()));
return a.exec();
}@
The app crashes at line 27. If i remove this line everything works fine. I tried the line below (which is commented), same behaviour.
I found this, but that couldnt solve the problem: http://qt-project.org/forums/viewthread/6021The really strange thing is that the appPath which is printed by qDebug is correct (i.e. "file:///C:/path/to/app.exe") and I use the EXACTLY SAME LINE in my rather big app.exe to start this application. So it cannot be the line itself i guess.
Anyone knows if this is a bug or what is wrong with my code?
I use Win7, Qt5.2.0-msvc2012-opengl.
Thank you all :) -
You are running a Qt Core application, yet you are trying to use a GUI class (QDesktopServices comes from QtGui module). I'm not sure, but that might be the reason.
Are you using Qt 4 or Qt 5?
-
As i mentioned, I run Qt5 and perhaps thats the problem.
I just ported from 4 to 5 and in 4, this call wasnt a problem (actually, the function doesnt need a GUI).
I dont list "gui" in my .pro-File and it compiles, so that seems like a Bug to me.
I'm not that experienced with reporting Bugs, but I'll do that this evening I guess. -
core and gui are 2 default modules, just as they were in Qt 4. So it's probably included implicitly for you by qmake. What I have meant is that perhaps you should start a QGuiApplication instead of a QCoreApplication.
Otherwise, the code looks fine.
-
Ok, I got it now, thank you :)
I used QApplication and it works. Maybe QGuiApplication does work too, but actually the app should not show up any GUI, so QApplication makes more sense. -
[quote author="Banshee" date="1391703890"]Ok, I got it now, thank you :)
I used QApplication and it works. Maybe QGuiApplication does work too, but actually the app should not show up any GUI, so QApplication makes more sense.[/quote]Well, that's nice deduction on your part, but it ain't right ;) No worries, though, the names of those classes are misleading. In Qt 5, the QtGui was split into 2 modules: QtWidgets (which contains all the widgets from Qt 4 + some new stuff) and QtGui (which contains only basic "building blocks" of a GUI and is a common base for QtWidgets and QtQuick modules).
Now, the name "QApplication" was already taken by the QtWidgets: because it needed to be source-almost-compatible with Qt 4. So for the new QtGui a new application class was created: the QGuiApplication.
So, in short: QGuiApplication, despite it's name, is more "low level" than QApplication.
Anyway, that is just a detail. I am really happy that it works for you now. Happy coding!