Qt Quick 2应用程序中的非本机对话框 [英] Non-native Dialog in Qt Quick 2 application

查看:86
本文介绍了Qt Quick 2应用程序中的非本机对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从导入QtQuick.Dialogs 创建非本地,非 QDialog 派生的对话框( QFileDialog 等)?

How to make dialogs from import QtQuick.Dialogs to be non-native, non-QDialog-derived (QFileDialog etc)?

可以制作 QFileDialog 设为非本地( QFileDialog :: Option :: DontUseNativeDialog )。但是如何在 QML 中制作对话框以在 xcb QPA和 eglfs QPA上呈现

It is possible to make QFileDialog to be non-native (QFileDialog::Option::DontUseNativeDialog). But how to make dialogs in QML to be rendered on xcb QPA and on eglfs QPA in the similar way?

推荐答案

更改此内容

QApplication app(argc, argv);

对此

QGuiApplication app(argc, argv);

Dialog 的窍门,但不是 FileDialog 。实际上,它告诉 QtQuick.Dialogs 您没有使用窗口小部件,但也会影响所使用的样式。

does the trick for Dialog, but not FileDialog. It essentially tells QtQuick.Dialogs that you're not using widgets, but it also affects the style that is used.

检查使用哪个应用程序的代码是此处

The code that checks which application is in use is 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) )似乎可以控制显示哪种类型的对话框。我不确定是否存在使用公共QML API强制非本机对话框的方法,但您始终可以修补Qt:

void QQuickAbstractDialog::setVisible(bool)) seems to control which type of dialog is shown. I'm not sure if there's a way to force non-native Dialogs using public QML API, but you could always patch 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对话框实现。

This patch alone is enough to force the QML dialog implementations to be used.

对于 FileDialog ,有本段解释了该过程:

For FileDialog, there is this paragraph that explains the process:


实现如果可能
,FileDialog的将是平台文件对话框。如果无法实现,则它将尝试实例化
QFileDialog。如果还是不可能的话,它将退回到
QML实现DefaultFileDialog.qml。在这种情况下,您可以
通过编辑此文件来自定义外观。 DefaultFileDialog.qml
包含一个矩形,用于保存对话框的内容,因为某些
嵌入式系统不支持多个顶层窗口。当出现
对话框时,如果可能,它将自动包装在Window
中,如果
只能是一个窗口,则将其简单地重新置于主窗口顶部。

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.

这篇关于Qt Quick 2应用程序中的非本机对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆