运行使用 CMake 构建的 QML/C++ 应用程序无法从 QtCreator 或 snap 包运行

运行使用 CMake 构建的 QML/C++ 应用程序无法从 QtCreator 或 snap 包运行

我正在尝试将 QML/C++ 应用程序打包为 snap。我使用 CMake 作为构建系统。应用程序构建良好,我可以通过双击来运行可执行文件。当我尝试从 QtCreator (Ubuntu SDK) 运行它时,我收到以下错误:

This application failed to start because it could not find or load the Qt platform plugin "xcb".

Reinstalling the application may fix this problem.
Aborted (core dumped)

尝试从 snap 包运行应用程序时出现同样的错误。

当尝试使用 QtCreator 4.0.2(手动下载并安装)和相同的 Qt 版本(Ubuntu 16.04 附带的 5.5.1)构建和运行应用程序时,应用程序运行时没有任何问题。

CMakeLists.txt

snapcraft.yaml

所有测试均在 Ubuntu 16.04 上完成,并添加了 ubuntu-sdk ppa。

更新:在装有 Ubuntu 16.04 的新虚拟机上进行了测试,希望这只是一个环境问题,但问题仍然重现

答案1

看起来你的 stage-packages 中缺少 libqt5gui5

更新:

因此,为了进一步扩展我的答案。这是一个简单的工作示例,用于捕捉 CMake/Qt/QML 应用程序并使其可从 qtcreator 运行

// main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>

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

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

    return app.exec();
}
//main.qml
import QtQuick 2.4
import QtQuick.Window 2.1
import Ubuntu.Components 1.3

Window {
    minimumHeight: units.gu(80)
    minimumWidth: minimumHeight
    MainView {
        applicationName: "simpleapp"
        anchors.fill: parent
        Page {
            title: "Hello world"

            Button {
                anchors.centerIn: parent
                text: "Quit"
                onClicked: Qt.quit()
            }
        }
    }
    Component.onCompleted: show()
}
#CMakeLists.txt
project(simpleapp)
cmake_minimum_required(VERSION 2.8)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Core  REQUIRED)
find_package(Qt5Gui   REQUIRED)
find_package(Qt5Qml   REQUIRED)
find_package(Qt5Quick REQUIRED)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

FILE(GLOB_RECURSE QML_FILES "${CMAKE_SOURCE_DIR}/*.qml")
add_custom_target(qml_files SOURCES ${QML_FILES})

set(CPP_FILES ${CMAKE_SOURCE_DIR}/main.cpp)

add_executable(${PROJECT_NAME} ${CPP_FILES} ${CMAKE_SOURCE_DIR}/qml.qrc)
qt5_use_modules(${PROJECT_NAME} Core Gui Qml Quick)

install(TARGETS ${PROJECT_NAME} DESTINATION /bin)
#snapcraft.yml
name: simpleapp
version: 1.0
summary: Qt Application Example
description: A simple app
confinement: devmode

apps:
  simpleapp:
    command: qt5-launch simpleapp
    plugs:
      - unity7
      - home

parts:
  application:
    plugin: cmake
    source: .
    build-packages:
      - qtbase5-dev
      - qtdeclarative5-dev
    stage-packages:
      - libqt5gui5
      - libgtk2.0-0
      - ubuntu-sdk-libs # probably overkill but fine for this example
    after: [qt5conf] # use the qt5-launch wiki part to setup env

要构建 snap 并运行它:

$ snapcraft snap
$ sudo snap install simpleapp_1.0_amd64.snap
$ simpleapp

只需使用 qtcreator 中的标准运行配置。

相关内容