How to set ToolTip's properties (delay, timeout, etc) globally?



  • Is there a way to set ToolTip's properties like "delay", "timeout" and "visible" globally in an application to avoid boilerplate code which sets the same properties in each item which needs a ToolTip? It seems like a standard use case when all controls have the same behavior of tooltips in an app, isn't it?
    Below is an example of using setTooltipProperties() to minimize code duplication, but still it looks like boilerplate code. It works for both desktop (mouse) and mobile (touch).

    import QtQuick 2.8
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.2
    import QtQuick.Layouts 1.3
    
    Window {
        id: window
        visible: true
        width: 640
        height: 480
        property bool isDesktop: switch (Qt.platform.os) {
                                 case "android":
                                 case "ios":
                                     return false;
                                 default:
                                     return true;
                                 }
    
        function setTooltipProperties(tooltip, target)
        {
            tooltip.visible = Qt.binding(function () {
                return tooltip.text === "" ?
                            false
                          :  isDesktop ? target.hovered : target.pressed
            })
    
            tooltip.delay = 2000
            tooltip.timeout = 3000
        }
    
        ColumnLayout {
            Button {
                text: "1"
                ToolTip.text: "Tooltip 1"
                Component.onCompleted: setTooltipProperties(ToolTip, this)
            }
    
            Button {
                text: "2"
                ToolTip.text: "Tooltip 2"
                Component.onCompleted: setTooltipProperties(ToolTip, this)
            }
        }
    }
    
    


  • You could attempt to build your own ToolTip and self manage it?

    Using a Timer to count after a onEnter for the specific control, and then count again for a timeout, stopping the timer on an exit...

    Use rectangle with a label inside and size it in accordance with the text size and a bit of padding to make it fancy... Will give you much more control and you can basically add anything to the tooltip

    use connections between your controls events to functions that manage the timer


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.