How to update the GUI from threads in the Ring programming language

Hello

When we create GUI application using the GUILib(RingQt) we still can use other libraries and we can use threads from the GameLib(RingAllegro)

In this example we see how we can update the GUI widgets/controls from child threads

We must update the GUI from the main thread. The next example works around this problem, in the main thread we have a timer that check an array for updates. The child thread uses this array to send updates to the main thread


Load "guilib.ring"

Load “gamelib.ring”

app = new qApp

{

win=new qMainWindow()

{

setwindowtitle(:test)

setGeometry(30,30,200,200)

status1 = new qstatusbar(win)

{

showmessage(“Ready!”,0)

}

prg=new qprogressbar(win)

{

setGeometry(0,40,200,15)

setvalue(0)

}

txt = new qlineedit(win)

{

setGeometry(0,60,100,20)

}

btn = new qpushbutton(win)

{

setGeometry(0,85,100,30)

}

C_FLAG_PROGRESS = 1 # Constant

aUpdateGUI = [[false,0]] # Flag , Value

pMutex = al_create_mutex()

oUpdateGUI = new qTimer(win)

{

setinterval(1000)

settimeoutevent(“pUpdateGUI()”)

start()

}

setstatusbar(status1)

show()

al_create_thread(‘ex()’)

}

exec()

}

func pUpdateGUI

al_lock_mutex(pMutex)

lFlag = aUpdateGUI[C_FLAG_PROGRESS][1]

al_unlock_mutex(pMutex)

if lFlag

al_lock_mutex(pMutex)

prg.setValue(aUpdateGUI[C_FLAG_PROGRESS][2])

aUpdateGUI[C_FLAG_PROGRESS][1] = false

al_unlock_mutex(pMutex)

ok

func ex

for x = 0 to 100 step 1

if x=100 x=0 ok

al_lock_mutex(pMutex)

aUpdateGUI[C_FLAG_PROGRESS][1] = true

aUpdateGUI[C_FLAG_PROGRESS][2] = x

al_unlock_mutex(pMutex)

next

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s