当前位置:   article > 正文

QML组件---ToolTip自定义文本详情_qml tooltip

qml tooltip

描述:ToolTip是用来描述控件信息的一小段文本,通常位控件之上或之下。提示文本可以是任何富文本信息。

1、使用附加属性设置控件ToolTip

  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. import QtQuick.Controls 2.14
  4. Window {
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9. Button {
  10. id: btn
  11. text: qsTr("Button Info")
  12. anchors.centerIn: parent
  13. ToolTip {
  14. delay: 500 //tooltip 500ms后出现
  15. timeout: 5000 //tooltip 5s后自动消失
  16. visible: btn.hovered //鼠标进入button中
  17. text: qsTr("This tool tip is text detailed info.")
  18. }
  19. }
  20. }

2、自定义ToolTip

  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. import QtQuick.Controls 2.14
  4. Window {
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9. Text {
  10. id: btn
  11. text: qsTr("Text Info")
  12. anchors.centerIn: parent
  13. TextArea{
  14. id: textInfoArea
  15. anchors.fill: parent
  16. }
  17. ToolTip{
  18. delay: 500
  19. timeout: 5000
  20. visible: textInfoArea.hovered
  21. contentItem: Text{
  22. text: qsTr("This tool tip is text detailed info.")
  23. font.pixelSize: 16
  24. font.bold: true
  25. color: "green"
  26. }
  27. background: Rectangle {
  28. border.color: "green"
  29. radius: 4
  30. }
  31. }
  32. }
  33. }

3、只有折叠或省略的文本显示详情

MyCommonText.qml

  1. import QtQuick 2.0
  2. Text {
  3. property var fontSize: 14
  4. property var fontFamliy: localFont.name
  5. property var fontColor: "#DDDDDD"
  6. property var bkColor: Qt.rgba(28/255, 30/255, 35/255, 0.85)
  7. property var hAlit: Text.AlignLeft
  8. property var vAlit: Text.AlignVCenter
  9. horizontalAlignment: hAlit
  10. verticalAlignment: vAlit
  11. elide: Text.ElideRight //不换行,超出部分省略
  12. font.family: fontFamliy
  13. font.pixelSize: fontSize
  14. color: fontColor
  15. MouseArea{
  16. id: textInfoArea
  17. anchors.fill: parent
  18. hoverEnabled: true
  19. propagateComposedEvents: true //鼠标事件继续下传到父控件
  20. onClicked: {
  21. mouse.accepted = false //不阻截鼠标
  22. }
  23. onCanceled: {
  24. mouse.accepted = false //不阻截鼠标
  25. }
  26. onDoubleClicked:{
  27. mouse.accepted = false //不阻截鼠标
  28. }
  29. onPositionChanged:{
  30. mouse.accepted = false //不阻//不阻截鼠标
  31. }
  32. onPressAndHold:{
  33. mouse.accepted = false //不阻截鼠标
  34. }
  35. onPressed:{
  36. mouse.accepted = false //不阻截鼠标
  37. }
  38. onReleased:{
  39. mouse.accepted = false //不阻截鼠标
  40. }
  41. }
  42. MyTextDetial {
  43. anchors.fill: parent
  44. fullText: parent.text
  45. showDetial: truncated && textInfoArea.containsMouse
  46. }
  47. }
  1. //MyTextDetial.qml
  2. import QtQuick 2.0
  3. import QtQuick.Controls 2.12
  4. Item{
  5. property var fullText: qsTr("textDetial")
  6. property var fontSize: 14
  7. property var fontFamliy: localFont.name
  8. property var fontColor: "#DDDDDD"
  9. property var bkColor: Qt.rgba(28/255, 30/255, 35/255, 0.85)
  10. property bool showDetial: false
  11. ToolTip{
  12. delay: 0 //立即出现出现tooltip
  13. //timeout: 5000 //5s后消失
  14. visible: showDetial
  15. contentItem: Text{
  16. text: fullText
  17. font.pixelSize: fontSize
  18. font.family: fontFamliy
  19. font.bold: true
  20. color: fontColor
  21. }
  22. background: Rectangle {
  23. color: bkColor
  24. }
  25. }
  26. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/805444
推荐阅读
相关标签
  

闽ICP备14008679号