You are on page 1of 10

Ring Documentation, Release 1.

nX = 0
nY = 0

new qApp {

win1 = new qWidget()


{

setWindowTitle("Move this label!")


setGeometry(100,100,400,400)
setstylesheet("background-color:white;")

Label1 = new qLabel(Win1){


setGeometry(100,100,200,50)
setText("Welcome")
setstylesheet("font-size: 30pt")
myfilter = new qallevents(label1)
myfilter.setEnterevent("pEnter()")
myfilter.setLeaveevent("pLeave()")
myfilter.setMouseButtonPressEvent("pPress()")
myfilter.setMouseButtonReleaseEvent("pRelease()")
installeventfilter(myfilter)
}

show()
}

exec()
}

Func pEnter
Label1.setStyleSheet("background-color: purple; color:white;font-size: 30pt;")

Func pLeave
Label1.setStyleSheet("background-color: white; color:black;font-size: 30pt;")

Func pPress
lPress = True
nX = myfilter.getglobalx()
ny = myfilter.getglobaly()

Func pRelease
lPress = False
pEnter()

Func pMove
nX2 = myfilter.getglobalx()
ny2 = myfilter.getglobaly()
ndiffx = nX2 - nX
ndiffy = nY2 - nY
if lPress
Label1 {
move(x()+ndiffx,y()+ndiffy)
setStyleSheet("background-color: Green;
color:white;font-size: 30pt;")
nX = nX2
ny = nY2
}

47.43. Moving Objects using the Mouse 440


Ring Documentation, Release 1.2

ok

The application during the runtime

47.43. Moving Objects using the Mouse 441


Ring Documentation, Release 1.2

47.43. Moving Objects using the Mouse 442


Ring Documentation, Release 1.2

47.44 Inheritance from GUI Classes

Example :
Load "guilib.ring"

New MyWindow()

new qApp { exec() }

class mywindow from qwidget


Func init
super.init()
setwindowtitle("First Window")
setgeometry(100,100,400,400)
setstylesheet("background-color: purple;")
settooltip("my first window!")
show()

The application during the runtime

47.44. Inheritance from GUI Classes 443


Ring Documentation, Release 1.2

47.45 Using QDesktopWidget Class

In the next example we will learn about using the QDesktopWidget class
Load "guilib.ring"

New qApp {
win1 = New qWidget()
{
resize(400,400)
btn1 = new qPushbutton(win1)
{
setText("Center")
move(100,100)
resize(100,30)
setClickEvent("pCenter()")
}

Show()
}

exec()
}

Func pCenter
oDesktop = new qDesktopWidget()

47.45. Using QDesktopWidget Class 444


Ring Documentation, Release 1.2

oRect = oDesktop.screenGeometry( oDesktop.primaryScreen() )


win1.move((oRect.width()-win1.width()) /2 , (oRect.Height()-win1.Height())/2 )
win1.show()

The application during the runtime

47.46 Rotate Text

The next example rotate text using a Timer.


Load "guilib.ring"

nAngle = 0

New qapp {
win1 = new qwidget() {
setwindowtitle("Rotate Text")
resize(800,600)
label1 = new qlabel(win1) {
settext("")
myfilter = new qallevents(win1)
myfilter.setMouseButtonPressevent("pClick()")
installeventfilter(myfilter)
}
new qtimer(win1) {
setinterval(50)

47.46. Rotate Text 445


Ring Documentation, Release 1.2

settimeoutevent("pTime()")
start()
}
pDraw()
L1 = new qVBoxLayout() { AddWidget(Label1) } SetLayout(L1)
showMaximized()
}
exec()
}

Func pDraw
p1 = new qpicture()
color = new qcolor() {
setrgb(0,0,255,255)
}
pen = new qpen() {
setcolor(color)
setwidth(50)
}
painter = new qpainter() {
begin(p1)
setpen(pen)
myfont = font()
myfont.setpointsize(50)
setfont(myfont)
rotate(nAngle)
drawtext(350,0*nAngle,"welcome")
drawtext(0,0*nAngle,"welcome")
endpaint()
}
label1 {
setpicture(p1)
show()
}

Func pClick
win1 { setwindowtitle("Click Event") }

Func pTime
nAngle++
if nAngle = 90
nAngle = 10
ok
pDraw()

The application during the runtime

47.46. Rotate Text 446


Ring Documentation, Release 1.2

47.47 Change Focus

The next example change the focus using the ENTER key.
load "guilib.ring"

new qApp {
win = new qWidget() {
resize(600,600)
SetWindowTitle("Change Focus")
text1 = new qLineEdit(win)
text2 = new qLineEdit(win)
text3 = new qLineEdit(win)
text4 = new qLineEdit(win)
layout1 = new qVBoxLayout() {
AddWidget(text1)
AddWidget(text2)
AddWidget(text3)
AddWidget(text4)

}
setLayout(Layout1)

47.47. Change Focus 447


Ring Documentation, Release 1.2

aList = [text1,text2,text3,text4]
oFilter = new qallevents(win)
oFilter.setKeyPressEvent("pWork()")
installeventfilter(oFilter)
show()
}
exec()
}

func pWork
nCode = oFilter.getkeycode()
if nCode = 16777220 # ENTER Key
for x=1 to len(aList)
if aList[x].HasFocus()
t = x+1
if t > len(aList) t=1 ok
aList[t].SetFocus(0)
exit
ok
next
ok

47.48 Regular Expressions

The next example uses the Regular Expressions classes.


load "guilib.ring"

new qApp
{
see "Using Regular Expressions" + nl

exp = new qregularexpression() {


setPattern("\d\d \w+")
see pattern() + nl
match = match("33 one",0,0,0)
see match.hasmatch() + nl
match = match("3 one",0,0,0)
see match.hasmatch() + nl
match = match("welcome 11 one",0,0,0)
see match.hasmatch() + nl
matched = match.captured(0)
see matched + nl
}
exp = new qregularexpression() {
setPattern("^(\d\d)/(\d\d)/(\d\d\d\d)$")
see pattern() + nl
match = match("08/12/1985",0,0,0)
see match.hasmatch() + nl
day = match.captured(1)
month = match.captured(2)
year = match.captured(3)
see day + nl + month + nl + year + nl
see "(" + match.capturedStart(1) + "," + match.capturedEnd(1)+ ")" + nl
see "(" + match.capturedStart(2) + "," + match.capturedEnd(2)+ ")" + nl
see "(" + match.capturedStart(3) + "," + match.capturedEnd(3)+ ")" + nl

47.48. Regular Expressions 448


Ring Documentation, Release 1.2

Output
Using Regular Expressions
\d\d \w+
1
0
1
11 one
^(\d\d)/(\d\d)/(\d\d\d\d)$
1
08
12
1985
(0,2)
(3,5)
(6,10)

47.49 Simple Client and Server Example

In this section we will learn about creating simple Client and Server Application
Load "guilib.ring"

new qApp {
oClient = new Client { client() }
oServer = new Server { server() }
exec()
}

Class Client

win1 lineedit1 cOutput=""


oTcpSocket

func client

win1 = new qwidget()

new qpushbutton(win1) {
setgeometry(50,50,100,30)
settext("connect")
setclickevent("oClient.Connect()")
}

lineedit1 = new qtextedit(win1) {


setGeometry(150,50,200,300)
}

win1 {
setwindowtitle("client")
setgeometry(10,100,400,400)
show()

47.49. Simple Client and Server Example 449

You might also like