简体   繁体   English

在QtQuick 2应用程序中使用某些Qt5类时出现分段错误

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

When trying to use some Qt-5 classes I'm experiencing crashes. 当尝试使用一些Qt-5类时,我遇到了崩溃。 I first discovered this trying to use QFileSystemModel . 我首先发现了这种尝试使用QFileSystemModel Trying to call setRootPath immediately leads to a crash. 尝试调用setRootPath立即导致崩溃。 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. 除了QFileIconProvider::icon()是seg错误发生之前调用的最后一个函数外, QFileIconProvider::icon()没有太大帮助(全部都是汇编代码)。

So next I tried using QFileIconProvider manually and -to no surprise- it also crashed the program. 因此,接下来我尝试手动使用QFileIconProvider ,并且-毫不奇怪-它也使程序崩溃了。

I'm using QtCreator 4 and the type of project is "Qt Quick Application". 我正在使用QtCreator 4,项目类型为“ Qt Quick Application”。 When I instead create a project of type "Qt Widgets Application", I can use both QFileIconProvider and QFileSystemModel without problems. 当我创建一个“ Qt Widgets Application”类型的项目时,可以同时使用QFileIconProviderQFileSystemModel

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. 我对Qt环境的了解不足,无法知道两种类型的项目之间的哪些差异可能导致段错误。

Both projects use the same Kit (same gcc, same Qt 5.6.1) and the default settings as set by QtCreator. 两个项目都使用相同的Kit(相同的gcc,相同的Qt 5.6.1)和QtCreator设置的默认设置。

This is my project.pro file: 这是我的project.pro文件:

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: 这是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实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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