Getting A Tooltip (Qt)

Last edited on

How to use the Qt API via Squish to get a tooltip text

It is easy to use the Qt API to access tooltips or other application object properties.

Qt 4 and higher

You can directly check an object's tooltip text (or any other property using the approach shown here).

JavaScript

import * as names from 'names.js'

function main() {
    startApplication("addressbook");



    // Verify a statically set tooltip text:
    var newButton = waitForObject(names.AddressBook_New_QToolButton);
    test.compare(newButton.toolTip, "New", "tool tip text for 'New'-button is set");



    // Verify a dynamically set tooltip text:

    // Move mouse over button and wait to ensure the
    // tooltip has been shown:
    mouseMove(newButton);
    snooze(2);

    // Use static method QToolTip.text():
    // https://doc.qt.io/qt-4.8/qtooltip.html#text
    test.compare(QToolTip.text(), "New", "tool tip for 'New'-button is shown");
}
test.js

Python

import names

def main():
    startApplication("addressbook")



    # Verify a statically set tooltip text:
    new_button = waitForObject(names.AddressBook_New_QToolButton");
    test.compare(new_button.toolTip, "New", "tool tip text for 'New'-button is set")



    # Verify a dynamically set tooltip text:

    # Move mouse over button and wait to ensure the
    # tooltip has been shown:
    mouseMove(new_button)
    snooze(2)

    # Use static method QToolTip.text():
    # https://doc.qt.io/qt-4.8/qtooltip.html#text
    test.compare(QToolTip.text(), "New", "tool tip for 'New'-button is shown")
test.py

Qt 3

For Qt 3 it is necessary to use the QToolTip class (which is wrapped by Squish) to fetch the tooltip for a widget:

Python

import names

def main():
  # Access the object using the symbolic name
  button = waitForObject(names.PressMe_QPushButton)

  # Get the tooltip text
  toolTipText = QToolTip.textFor( button )

  # Print it into the test log
  test.log("This is the tooltip text: " + str(toolTipText))

  # Compare it to a string, the result is printed to the test log
  test.compare(str(toolTipText), "this will fail")
test.py

Tcl

source [findFile "scripts" "names.tcl"]

proc main {} {
  # Access the object using the symbolic name
  set button [ waitForObject $names::Press_Me_QPushButton ]

  # Get the tooltip text
  set toolTipText [ toString [ invoke QToolTip textFor $button ] ]

  # Print it into the test log
  test log "This is the tooltip text: $toolTipText"

  # Compare it to a string, the result is printed to the test log
  test compare $toolTipText "this will fail"
}
test.tcl