Qt Quick 2 应用程序中的非本机对话框

Non-native Dialog in Qt Quick 2 application

如何使来自 import QtQuick.Dialogs 的对话成为非本地的、非 QDialog 派生的(QFileDialog 等)?

可以将 QFileDialog 设为非本地 (QFileDialog::Option::DontUseNativeDialog)。但是如何使 QML 中的对话框以类似的方式呈现在 xcb QPA 和 eglfs QPA 上?

改变这个

QApplication app(argc, argv);

至此

QGuiApplication app(argc, argv);

Dialog 有效,但对 FileDialog 无效。它基本上告诉 QtQuick.Dialogs 您没有使用小部件,但它也会影响所使用的样式。

检查正在使用哪个应用程序的代码是here:

static QString defaultStyleName()
{
    //Only enable QStyle support when we are using QApplication
#if defined(QT_WIDGETS_LIB) && !defined(Q_OS_IOS) && !defined(Q_OS_ANDROID) && !defined(Q_OS_BLACKBERRY) && !defined(Q_OS_QNX) && !defined(Q_OS_WINRT)
    if (QCoreApplication::instance()->inherits("QApplication"))
        return QLatin1String("Desktop");
#elif defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
    if (QtAndroidPrivate::androidSdkVersion() >= 11)
        return QLatin1String("Android");
#elif defined(Q_OS_IOS)
    return QLatin1String("iOS");
#elif defined(Q_OS_WINRT) && 0 // Enable once style is ready
    return QLatin1String("WinRT");
#endif
    return QLatin1String("Base");
}

void QQuickAbstractDialog::setVisible(bool)) 似乎可以控制显示哪种类型的对话框。我不确定是否有办法使用 public QML API 强制非本地对话框,但你总是可以修补 Qt:

diff --git a/src/dialogs/qquickabstractdialog.cpp b/src/dialogs/qquickabstractdialog.cpp
index ce87d56..416f796 100644
--- a/src/dialogs/qquickabstractdialog.cpp
+++ b/src/dialogs/qquickabstractdialog.cpp
@@ -81,7 +81,7 @@ void QQuickAbstractDialog::setVisible(bool v)
     if (m_visible == v) return;
     m_visible = v;

-    if (m_dialogHelperInUse || v) {
+    if (0 /*m_dialogHelperInUse || v*/) {
         // To show the dialog, we first check if there is a dialog helper that can be used
         // and that show succeeds given the current configuration. Otherwise we fall back
         // to use the pure QML version.

仅此补丁就足以强制使用 QML 对话框实现。

对于FileDialog,有this paragraph说明过程:

The implementation of FileDialog will be a platform file dialog if possible. If that isn't possible, then it will try to instantiate a QFileDialog. If that also isn't possible, then it will fall back to a QML implementation, DefaultFileDialog.qml. In that case you can customize the appearance by editing this file. DefaultFileDialog.qml contains a Rectangle to hold the dialog's contents, because certain embedded systems do not support multiple top-level windows. When the dialog becomes visible, it will automatically be wrapped in a Window if possible, or simply reparented on top of the main window if there can only be one window.