【问题标题】:How to make visible both icon and text on QML ToolButton如何在 QML ToolButton 上同时显示图标和文本
【发布时间】:2014-02-23 20:44:52
【问题描述】:

我正在使用 QML 和 QtQuick.Components 创建桌面应用程序。我想像标准 MacOS 设置对话框一样放置在工具栏按钮上:

我使用 ToolBar 和 ToolButton,但我找不到方法。例如下面的代码它只显示图标:

ApplicationWindow {
    // ...

    toolBar: ToolBar {
        RowLayout {
            ToolButton {
                text: qsTr("Main")
                iconSource: "main.png"
            }
            ToolButton {
                text: qsTr("System")
                iconSource: "system.png"
            }
            ToolButton {
                text: qsTr("Items Book")
                iconSource: "itemsbook.png"
            }
        }
    }
}

似乎ToolButton 可以显示文本或图标:

Text {
    id: textitem
    text: button.text
    anchors.centerIn: parent
    visible: button.iconSource == "" // <=========
}

【问题讨论】:

  • 有什么理由不使用example中的操作?
  • @LaszloPapp 没有区别,只有 Actions 的图标也是可见的。

标签: qt qml


【解决方案1】:

一种简单而强大的方法是创建自己的 QML 组件。

  1. 创建自定义 QML 组件/文件:
    File -&gt; New File or Project -&gt; Qt -&gt; QML File (choose latest version).
    现在输入文件名,例如 MyToolButton.
    请注意,它也将用作 组件名称

  2. 添加必要的导入和代码:

MyToolButton.qml(根据您的需要定制)

import QtQuick 2.0
import QtQuick.Controls 1.4

ToolButton
{
    Image {
        source: parent.iconSource
        fillMode: Image.PreserveAspectFit // For not stretching image (optional)
        anchors.fill: parent
        anchors.margins: 2 // Leaving space between image and borders (optional)
        anchors.bottomMargin:10 // Leaving space for text in bottom
    }
    Text {
        text: parent.text

        anchors.bottom: parent.bottom // Placing text in bottom
        anchors.margins: 2 // Leaving space between text and borders  (optional)
        anchors.horizontalCenter: parent.horizontalCenter // Centering text
        renderType: Text.NativeRendering // Rendering type (optional)
    }
}

Main.QML(你想在哪里使用它):

import QtQuick 2.0
import QtQuick.Controls 1.4

// Usual toolbar declaration
ToolBar {
    id: mainToolBar
    RowLayout {

        // Create MyToolButton. Note that component name (MyToolButton) is the same as file name.
        MyToolButton {
            id:tbQuit

            // Way 1: Add here any icon 
            iconSource: "qrc:///images/quit.png" 
            text:qsTr("&Quit")

            // Way 2, my favourite: Convert your Action into Button!
            action: actQuit
        }
    }
}

Action {
    id: actQuit
    text: qsTr("&Quit")
    onTriggered: Qt.quit()
    shortcut: "Alt+Q"
    iconSource: "qrc:///Images/Exit.png"
}

注意事项:

  1. 它需要 QtQuick.Controls 1.4 并且应该在 Qt 5.2+ 上工作。 (在 Qt 5.5 上运行良好)。
  2. 别忘了添加imports
  3. 标记为(optional) 的代码部分可以跳过。
  4. ToolButton 替换为Button,它将作为按钮工作。

希望对你有帮助!

【讨论】:

  • 在 Qt 5.6 中实际上不起作用。我尝试了@Kirween 下面的另一个答案,但工具栏高度无法调整。无论我做什么,文本元素都会与图像重叠。它只是变胖了。
【解决方案2】:

您是否尝试像这样添加自己的Text 控件:

ApplicationWindow {
    // ...

    toolBar: ToolBar {
        RowLayout {
            ToolButton {
                text: qsTr("Main")
                iconSource: "main.png"
                Text {
                    text: parent.text
                    anchors.bottom: parent.bottom
                    anchors.horizontalCenter: parent.horizontalCenter
                }
            }
            ToolButton {
                text: qsTr("System")
                iconSource: "system.png"
                Text {
                    text: parent.text
                    anchors.bottom: parent.bottom
                    anchors.horizontalCenter: parent.horizontalCenter
                }
            }
            ToolButton {
                text: qsTr("Items Book")
                iconSource: "itemsbook.png"
                Text {
                    text: parent.text
                    anchors.bottom: parent.bottom
                    anchors.horizontalCenter: parent.horizontalCenter
                }
            }
        }
    }
}

并将ToolButton的高度设置为正确的值(图片+文字高度)

【讨论】:

  • 看起来不错,但图标上方有空白。
  • 您也可以将anchors.bottomMargin: 2 添加到文本中以更好地使用
猜你喜欢
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
相关资源
最近更新 更多