简体   繁体   中英

Segmentation Fault when using some Qt5 classes in a QtQuick 2 application

When trying to use some Qt-5 classes I'm experiencing crashes. I first discovered this trying to use QFileSystemModel . Trying to call setRootPath immediately leads to a crash. The callstack isn't of much help (all of it is assembly code) except that QFileIconProvider::icon() is the last function called before the seg fault occurs.

So next I tried using QFileIconProvider manually and -to no surprise- it also crashed the program.

I'm using QtCreator 4 and the type of project is "Qt Quick Application". When I instead create a project of type "Qt Widgets Application", I can use both QFileIconProvider and QFileSystemModel without problems.

Here's where I'm out of ideas. I don't know enough about the Qt environment to know what difference between the two types of projects could lead to the seg fault.

Both projects use the same Kit (same gcc, same Qt 5.6.1) and the default settings as set by QtCreator.

This is my project.pro file:

TEMPLATE = app

QT += qml quick widgets //default .pro file except for widgets
CONFIG += c++11

SOURCES += main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

This is main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QDir>
#include <QFileSystemModel>
#include <QQmlContext>
#include <QFileIconProvider>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    //If trying to use QFileSystemMode...
    QFileSystemModel model;
    model.setRootPath("/somefolder/"); //..the crash happens here

    //Attempting to use QFileIconProvider also crashes
    //QFileIconProvider fip;
    //fip.icon( QFileInfo("/somefolder/somefile") ); //<- here

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

I'd appreciate any help or pointers as to how to debug that mess.

听起来可能令人困惑,但是QFileSystemModel是QtWidgets的一部分,因此需要您创建QApplication实例,而不是QGuiApplication实例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM