Qt Quick - FontDialog font dialog

I. Overview

FontDialog allows the user to select a font. The dialog is initially invisible. You need to first set the property as desired, then set visible to true or call open().

A FontDialog window is automatically temporary with respect to its parent window. So, whether you declare the dialog in the project or in the window, the dialog will be displayed in the center of the window containing the project or the window you declare it in.

The implementation of FontDialog will be a platform font dialog. If this fails, then it tries to instantiate a QFontDialog. If that fails too, then it falls back to a QML implementation, DefaultFontDialog.qml.

In this case, you can customize the appearance by editing this file. DefaultFontDialog. The qml contains a rectangle to hold the content of the dialog, because some embedded systems do not support multiple top-level windows. When the dialog becomes visible, it is automatically wrapped in a window if possible, or simply re-parents on top of the main window if there can only be one.

Two, use

Here's a minimal example that opens a font dialog and exits after the user selects a font:
insert image description here

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import QtQuick.Dialogs 1.3

Window {
    
    
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Button{
    
    
        id:button
        text: "字体选择"

        anchors.centerIn: parent

        onClicked: {
    
    
            fontDialog.open();
        }

        FontDialog {
    
    
              id: fontDialog
              title: "Please choose a font"
              font: Qt.font({
    
     family: "Arial", pointSize: 24, weight: Font.Normal })
              onAccepted: {
    
    
                  console.log("You chose: " + fontDialog.font)
                  button.font = fontDialog.font;
              }
              onRejected: {
    
    
                  console.log("Canceled")
              }
          }
    }
}

3. Introduction to common attributes

modality : Qt::WindowModality

  • It is the flag of the modal dialog box, and the default is Qt.NonModal.

Guess you like

Origin blog.csdn.net/qq_43680827/article/details/130171257