Вы находитесь на странице: 1из 10

Ring Documentation, Release 1.

57.47 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()

57.47. Using QDesktopWidget Class 653


Ring Documentation, Release 1.6

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


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

The application during the runtime

57.48 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)

57.48. Rotate Text 654


Ring Documentation, Release 1.6

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

57.48. Rotate Text 655


Ring Documentation, Release 1.6

57.49 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)

57.49. Change Focus 656


Ring Documentation, Release 1.6

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

57.50 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

57.50. Regular Expressions 657


Ring Documentation, Release 1.6

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)

57.51 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()

57.51. Simple Client and Server Example 658


Ring Documentation, Release 1.6

func connect
cOutput = "Connect to host 127.0.0.1 port 9999" + nl
lineedit1.settext(cOutput)
oTcpSocket = new qTcpSocket(win1) {
setconnectedevent("oClient.pConnected()")
setreadyreadevent("oClient.pRead()")
connecttohost("127.0.0.1",9999,3,0)
waitforconnected(5000)
}

func pConnected

cOutput += "Connected!" + nl
lineedit1.settext(cOutput)

func pRead

cOutput += "Ready Read!" + nl


lineedit1.settext(cOutput)
cOutput += oTcpSocket.readall().data() + nl
lineedit1.settext(cOutput)

Class Server

win1 lineedit1
oTcpServer oTcpClient
cOutput = ""

func server

win1 = new qwidget()

lineedit1 = new qtextedit(win1) {


setGeometry(150,50,200,300)
}

win1 {
setwindowtitle("Server")
setgeometry(450,100,400,400)
show()
}

oTcpServer = new qTcpServer(win1) {


setNewConnectionEvent("oServer.pNewConnection()")
oHostAddress = new qHostAddress()
oHostAddress.SetAddress("127.0.0.1")
listen(oHostAddress,9999)
}
cOutput = "Server Started" + nl +
"listen to port 9999" + nl

lineedit1.settext(cOutput)

Func pNewConnection

oTcpClient = oTcpServer.nextPendingConnection()

57.51. Simple Client and Server Example 659


Ring Documentation, Release 1.6

cOutput += "Accept Connection" + nl


lineedit1.settext(cOutput)
oTcpClient {
cStr ="Hello from server to client!"+char(13)+char(10)
write(cStr,len(cStr))
flush()
waitforbyteswritten(300000)
close()
}

The application during the runtime

57.52 Dynamic Objects

We may create objects in the runtime and add them to windows.


Example:
load "guilib.ring"

oFormDesigner = new FormDesigner { start("oFormDesigner") }

Class FormDesigner

winToolBox winForm

aObjects = []

func start cObjectName

oApp = new qApp

winToolBox = new qWidget()


winToolBox.setWindowTitle("ToolBox")

57.52. Dynamic Objects 660


Ring Documentation, Release 1.6

winToolBox.move(10,10)
winToolBox.resize(300,600)

btn = new qPushButton(winToolBox)


btn.resize(300,30)
btn.setText("Create Button")
btn.setClickEvent(cObjectName+".pCreateButton()")
btn.show()

winToolBox.show()

winForm = new qWidget() {


move(400,50)
setWindowTitle("Form Designer")
resize(600,600)
show()
}

oApp.exec()

func pCreateButton

nCount = len(aObjects)

aObjects + new MyButton(winForm)


{
nIndex = nCount + 1
setText("Button"+ nIndex)
Move(30*nIndex,30*nIndex)
resize(100,30)
show()
}

Class MyButton from qPushButton


nIndex = 0

57.53 Weight History Application

The next sample help in recording (Date, Time and Weight).


Load "guilib.ring"

MyApp = new qApp


{
$ApplicationObject = "oApp" # To be used when calling events
oApp = new App
exec()
oApp.CloseDatabase()
}

class App

cDir = currentdir() + "/"


oCon

57.53. Weight History Application 661


Ring Documentation, Release 1.6

aIDs = []

win1 = new qWidget()


{
setWindowTitle("Weight History")
resize(600,600)
layoutButtons = new qhboxlayout()
{
label1 = new qLabel(win1) { setText("Weight") }
text1 = new qlineedit(win1)
btnAdd = new qpushbutton(win1) {
setText("Add")
setClickEvent($ApplicationObject+".AddWeight()")
}
btnDelete = new qpushbutton(win1) {
setText("Delete")
setClickEvent($ApplicationObject+".Deleteweight()")
}
addwidget(label1)
addwidget(text1)
addwidget(btnAdd)
addwidget(btnDelete)
}
layoutData = new qhboxlayout()
{
Table1 = new qTableWidget(win1) {
setrowcount(0)
setcolumncount(3)
setselectionbehavior(QAbstractItemView_SelectRows)
setHorizontalHeaderItem(0, new QTableWidgetItem("Date"))
setHorizontalHeaderItem(1, new QTableWidgetItem("Time"))
setHorizontalHeaderItem(2, new QTableWidgetItem("Weight"))
setitemChangedEvent($ApplicationObject+".ItemChanged()")
setAlternatingRowColors(true)
horizontalHeader().setStyleSheet("color: blue")
verticalHeader().setStyleSheet("color: red")
}
addWidget(Table1)
}
layoutClose = new qhboxlayout()
{
btnclose = new qpushbutton(win1) {
setText("Close")
setClickEvent("MyApp.Quit()")
}
addwidget(btnClose)
}
layoutMain = new qvboxlayout()
{
addlayout(layoutButtons)
addLayout(LayoutData)
addLayout(layoutClose)
}
setlayout(layoutMain)
self.OpenDatabase()
self.ShowRecords()
show()
}

57.53. Weight History Application 662

Вам также может понравиться