您尚未登录。

#1 2023-07-28 18:46:17

Fanpu_Xue
会员
注册时间: 2023-04-12
帖子: 29

编写QML程序,运行后报错 ERROR: No native FileDialog implementation available.

使用qt6,跟着qt book编写图片查看器示例,在arch上运行报错:

ERROR: No native FileDialog implementation available.
Qt Labs Platform requires Qt Widgets on this setup.
Add 'QT += widgets' to .pro and create QApplication in main().

同样的代码在windows下运行没有报错。
根据上述报错我尝试了在cmakeLists.txt中添加widgets,但是无济于事

有没有人遇到过?怎么解决的呀?

qml文件的代码如下:

import QtQuick
import QtQuick.Controls
import Qt.labs.platform as Platform

ApplicationWindow {

    // ...

    visible: true
    width: 640
    height: 480
    title: qsTr("Image Viewer")

    header: ToolBar {
        Flow {
            anchors.fill: parent
            ToolButton {
                text: qsTr("Open")
                icon.name: "document-open"
                onClicked: fileOpenDialog.open()
            }
        }
    }

    Image {
        id: image
        anchors.fill: parent
        fillMode: Image.PreserveAspectFit
        asynchronous: true
    }

    Platform.FileDialog {
        id: fileOpenDialog
        title: "Select an image file"
        folder: Platform.StandardPaths.writableLocation(Platform.StandardPaths.DocumentsLocation)
        nameFilters: [
            "Image files (*.png *.jpeg *.jpg)",
        ]
        onAccepted: {
            image.source = fileOpenDialog.file
        }
    }

    Dialog {
        id: aboutDialog
        title: qsTr("About")
        Label {
            anchors.fill: parent
            text: qsTr("QML Image Viewer\nA part of the QmlBook\nhttp://qmlbook.org")
            horizontalAlignment: Text.AlignHCenter
        }

        standardButtons: Dialog.Ok
    }

    // ...

}

离线

#2 2023-07-28 20:18:34

zhuangzhuang
会员
注册时间: 2022-08-14
帖子: 164
个人网站

Re: 编写QML程序,运行后报错 ERROR: No native FileDialog implementation available.

FileDialog应该是指的是用于选择文件的弹出窗口吧,你用的是什么桌面环境?


上学中

离线

#3 2023-07-28 20:29:10

Fanpu_Xue
会员
注册时间: 2023-04-12
帖子: 29

Re: 编写QML程序,运行后报错 ERROR: No native FileDialog implementation available.

zhuangzhuang 说:

FileDialog应该是指的是用于选择文件的弹出窗口吧,你用的是什么桌面环境?

对的,是用于选择文件的弹出窗口。桌面环境用的是kde

最近编辑记录 Fanpu_Xue (2023-07-28 20:31:11)

离线

#4 2023-07-30 10:43:54

Fanpu_Xue
会员
注册时间: 2023-04-12
帖子: 29

Re: 编写QML程序,运行后报错 ERROR: No native FileDialog implementation available.

找到原因了,查看了官方文档发现:

A native platform file dialog is currently available on the following platforms:

- Android
- iOS
- Linux (when running with the GTK+ platform theme)
- macOS
- Windows

The Qt Labs Platform module uses Qt Widgets as a fallback on platforms that do not have a native implementation available. Therefore, applications that use types from the Qt Labs Platform module should link to QtWidgets and use QApplication instead of QGuiApplication.

得是GTK+的才可以。
所以KDE的不能用Qt.labs.platform提供的fileDialog,不过可以使用QtQuick.Dialogs的(虽然有点丑,有点难用)

离线

页脚