Squish/Clicking a QLabel Link: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
= Clicking link in label =
= Clicking link in label =


As is commonly known, you can put in hyper text links in a regular QLabel in Qt, by using simple HTML syntax. Then of course, you want to be able to click this link in your Squish tests. However, Squish does not have a clickLink() function, as it does for e.g. clickButton() or clickTab(), so what happens during recording is that you will get code similar to:<br /><code>mouseClick(waitForObject(&quot;:MyQLabel&amp;quot;), 31, 14, 0, Qt.LeftButton)<code>
As is commonly known, you can put in hyper text links in a regular QLabel in Qt, by using simple HTML syntax. Then of course, you want to be able to click this link in your Squish tests. However, Squish does not have a clickLink() function, as it does for e.g. clickButton() or clickTab(), so what happens during recording is that you will get code similar to:
<code>mouseClick(waitForObject(":MyQLabel"), 31, 14, 0, Qt.LeftButton)<code>


That is, hard coded coordinates for where the link is. So if the position of the link would change for whatever reason (different platform, new text added, different font) the test will break.
That is, hard coded coordinates for where the link is. So if the position of the link would change for whatever reason (different platform, new text added, different font) the test will break.
Line 9: Line 10:
In Squish 4.1, a new function named installSignalHandler() was added. This lets your test script react to signals emitted from the application code. We can use this function combined with the fact that a signal is emitted from a QLabel whenever a link is hovered. This will let us implement a function for clicking arbitrary links in a label.
In Squish 4.1, a new function named installSignalHandler() was added. This lets your test script react to signals emitted from the application code. We can use this function combined with the fact that a signal is emitted from a QLabel whenever a link is hovered. This will let us implement a function for clicking arbitrary links in a label.


</code>_lookingFor = None<br />_found = False
</code>_lookingFor = None
_found = False


def handleLinkHovered(obj, link):<br /> global _found<br /> if link == _lookingFor:<br /> _found = True
def handleLinkHovered(obj, link):
global _found
if link == _lookingFor:
_found = True


def findLink(objectName, link):<br /> global _lookingFor<br /> global _found<br /> _lookingFor = link<br /> _found = False
def findLink(objectName, link):
global _lookingFor
global _found
_lookingFor = link
_found = False


object = waitForObject(objectName)
object = waitForObject(objectName)


installSignalHandler(object, &quot;linkHovered(QString)&quot;, &quot;handleLinkHovered&amp;quot;)
installSignalHandler(object, "linkHovered(QString)", "handleLinkHovered")


width = object.width<br /> height = object.height<br /> y = 0<br /> while y &lt; height:<br /> x = 0<br /> while x &lt; width:<br /> sendEvent(&quot;QMouseEvent&amp;quot;, object, QEvent.MouseMove, x, y, Qt.NoButton, 0)<br /> if _found:<br /> return (x,y)<br /> x ''= 5<br /> y''= 10
width = object.width
height = object.height
y = 0
while y < height:
x = 0
while x < width:
sendEvent("QMouseEvent", object, QEvent.MouseMove, x, y, Qt.NoButton, 0)
if _found:
return (x,y)
x ''= 5
y''= 10


return (–1,–1)
return (–1,–1)


def clickLink(objectName, link):<br /> (x, y) = findLink(objectName, link)<br /> if x [[Image:= -1 and y |= -1 and y ]]= 1:<br /> mouseClick(objectName, x, y, 0, Qt.LeftButton)<code>
def clickLink(objectName, link):
(x, y) = findLink(objectName, link)
if x [[Image:= -1 and y |= -1 and y ]]= 1:
mouseClick(objectName, x, y, 0, Qt.LeftButton)<code>


Usage is simply:<br /></code>clickLink(&quot;:MyQLabel&amp;quot;, &quot;some-action&amp;quot;)</code>
Usage is simply:
</code>clickLink(":MyQLabel", "some-action")</code>

Revision as of 10:34, 25 February 2015


Clicking link in label

As is commonly known, you can put in hyper text links in a regular QLabel in Qt, by using simple HTML syntax. Then of course, you want to be able to click this link in your Squish tests. However, Squish does not have a clickLink() function, as it does for e.g. clickButton() or clickTab(), so what happens during recording is that you will get code similar to:

mouseClick(waitForObject(":MyQLabel"), 31, 14, 0, Qt.LeftButton)<code>

That is, hard coded coordinates for where the link is. So if the position of the link would change for whatever reason (different platform, new text added, different font) the test will break.

In Squish 4.1, a new function named installSignalHandler() was added. This lets your test script react to signals emitted from the application code. We can use this function combined with the fact that a signal is emitted from a QLabel whenever a link is hovered. This will let us implement a function for clicking arbitrary links in a label.

_lookingFor = None

_found = False

def handleLinkHovered(obj, link):

global _found
if link == _lookingFor:
_found = True

def findLink(objectName, link):

global _lookingFor
global _found
_lookingFor = link
_found = False

object = waitForObject(objectName)

installSignalHandler(object, "linkHovered(QString)", "handleLinkHovered")

width = object.width

height = object.height
y = 0
while y < height:
x = 0
while x < width:
sendEvent("QMouseEvent", object, QEvent.MouseMove, x, y, Qt.NoButton, 0)
if _found:
return (x,y)
x = 5
y= 10

return (–1,–1)

def clickLink(objectName, link):

(x, y) = findLink(objectName, link)
if x = -1 and y= 1:

mouseClick(objectName, x, y, 0, Qt.LeftButton)

Usage is simply:

clickLink(":MyQLabel", "some-action")