Anda di halaman 1dari 83

HBQT - The tutorial about Harbour and QT programming - by Giovan...

1 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

Go home
Go back to other tutorials

HBQT-Tutorial
Giovanni Di Maria - calimero22@yahoo.it
since: Feb 12, 2011

Tutorial
Harbour QT Classes
This Tutorial:
Rev. [1] - February 12, 2011
Rev. [7657] - Apr 21, 2015

Record of Revision
Revision

Date

Feb 12, 2011

1000

Mar 22, 2011

2000

Mar 30, 2011

3000

Apr 03, 2011

4000

Apr 21, 2011

5000

Jun 07, 2012

6000

Jul 09, 2012

7000

Nov 29, 2012

Last Updates
Updated "How to install QtContribs and compile a PRG" - (Rev. [7642])
Added "How to Distribuite an Application" - (Rev. [7651])
Added "Games - McDonald's Game" - (Rev. [7654])

Introduction
This tutorial is a brief, continuously updated, of the use of the classes QT with Harbour language. It is specifically written for beginners that initially encountered some difficulties in using these classes, however, extremely powerful and
efficient. The approach of the tutorial is different from other guides that are online. It simply focuses a single class or single object, so you do not get lost in the maze of the vast files of examples provided with the product. This will easily
learn to manage and plan their individual class, as needed, and, finally, to put "together" the whole.
Giovanni Di Maria

Notes for developers


In order to make good use of the Qt classes, you should see the include file "hbqtgui.ch"
You should consult the Digia's offcial documentation
If possible, you must NOT use static variables
It is strongly recommended to use hbformat tool to format the .prg sources
To create a source, you can copy and paste the code into your text editor and save it with .prg extension
When you connect an object with a Signal, if must be passed as string without spaces.
Example: oButton1:Connect( "clicked()", { || QApplication():quit() } )
When you connect an object with a Event, if must be passed as constant.
Example: oWnd:connect( QEvent_KeyPress , { |k| keypressed( k ) } )

Legal Notices
This tutorial is a free document and will remain free. You can view, use, print and redistribute it and/or modify it, without any limitations.

How to install QtContribs and compile a PRG


QtContribs_Setup.exe is a self archive containing also Harbour. You need only install it to your disk. Follow these steps to install QtContribs and compile your PRG:
Download last executable version of QtContribs from:
http://sourceforge.net/projects/qtcontribs/files/?source=navbar
Install QTContribs in c:\hbqtcontrib391 (or other folder);
From console type: PATH C:\hbqtcontrib391\bin
hbmk2 -w3 prova.prg addons\hbqt\qtgui\hbqtgui.hbc

How to Distribuite an Application


To distribuite an application follow the following steps:
Copy the folder "platforms" to the folder of your application from "C:\hbqtcontrib391\bin"
Copy your "EXE" to the folder of your application
Copy "Qt5PrintSupport.dll" to the folder of your application
Copy "Qt5Core.dll" to the folder of your application
Copy "libstdc++-6.dll" to the folder of your application
Copy "icuin53.dll" to the folder of your application
Copy "icuuc53.dll" to the folder of your application
Copy "icudt53.dll" to the folder of your application
Copy "Qt5Gui.dll" to the folder of your application
Copy "Qt5Widgets.dll" to the folder of your application
Copy "libgcc_s_dw2-1.dll" to the folder of your application

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

2 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

Copy "libwinpthread-1.dll" to the folder of your application

QMainWindow - Window
The following example shows how to create a simple main window. The window is resizable.

PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oWnd:show()
QApplication():exec()
RETURN

QMainWindow - Window not resizable


The following example shows how to create a simple main window. The window is NOT resizable.

PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:setFixedSize( 200, 200 )
oWnd:show()
QApplication():exec()
RETURN

QMainWindow - Window with fixed width


The following example shows how to create a simple main window. The width is locked and the height is resizable.

PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:setFixedWidth( 500 )
oWnd:show()
QApplication():exec()
RETURN

QMainWindow - Window with Fixed height


The following example shows how to create a simple main window. The height is locked and the width is resizable.

PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:setFixedHeight( 300 )
oWnd:show()
QApplication():exec()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

3 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

RETURN

QMainWindow - Blinking Title Bar


The following example shows how to create a blinking title bar.

PROCEDURE Main()
LOCAL oWnd
LOCAL oClock
oWnd := QMainWindow()
oWnd:resize( 300, 200 )
oWnd:setWindowTitle( "Finestra di Giovanni" )
oClock := QTimer()
oClock:Connect( "timeout()", { || toggle( oWnd ) } )
oClock:start( 500 )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN
PROCEDURE toggle( o )
IF o:WindowTitle == "Finestra di Giovanni"
o:setWindowTitle( "" )
ELSE
o:setWindowTitle( "Finestra di Giovanni" )
ENDIF
RETURN

QMainWindow - Colored Window (QSS)


The following example shows how to create a colored main window, using QSS.

PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oWnd:setStyleSheet( " background-color: #CCCCFF; " )
oWnd:show()
QApplication():exec()
RETURN

QMainWindow - Colored Window (QPalette)


The following example shows how to create a colored main window, using Qpalette.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oPalette
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oPalette := QPalette()
oPalette:SetColor( QPalette_Window, QColor( 255,200,200 ) )
oWnd:setPalette( oPalette )
oWnd:show()
QApplication():exec()
RETURN

QMainWindow - Window with background


The following example shows how to create a window with a background from an image.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

4 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd, oButton
oWnd := QMainWindow()
oWnd:SetFixedSize( 400, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oWnd:setStyleSheet( "background-image: url(01.png) " );
oButton := QPushButton( oWnd )
oButton:setText( "Press" )
oButton:move( 150, 50 )
oButton:setStyleSheet( " background: #F4F4F0; " )
oWnd:show()
QApplication():exec()
RETURN

QMainWindow - Window as Splash Screen


The following example shows how to create a Splash Screen Window. There is NO border. The windows is closed by pressing on it.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel
oWnd := QWidget()
oWnd:setWindowTitle( "Special Effect" )
oWnd:resize( 300, 300 )
oWnd:setAttribute( Qt_WA_TranslucentBackground )
oWnd:setWindowFlags( Qt_FramelessWindowHint )
oLabel := QLabel( oWnd )
oLabel:move( 0, 0 )
oLabel:resize( 300, 300 )
oLabel:setText( "Click me to close!" )
oLabel:setStyleSheet( "border: 1px solid #0000FF; background-color: yellow; border-radius: 150px;" )
oLabel:setAlignment( Qt_AlignHCenter + Qt_AlignVCenter )
oLabel:Connect( QEvent_MouseButtonPress, { || QApplication():quit() } )
oWnd:show()
QApplication():exec()
RETURN

QMainWindow - Colored Window (QSS) with a gradient


The following example shows how to create a colored main window, with a gradient, using QSS.

PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oWnd:setStyleSheet( "background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,stop:0 #8888FF, stop:1 #000033);" )
oWnd:show()
QApplication():exec()
RETURN

QMainWindow - Retrieve version of Harbour, GCC and QT


The following example shows how to retrieve informations from compilers used.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

5 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

#include "hbver.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel1, oLabel2, oLabel3
LOCAL cVersion1, cVersion2, cVersion3
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
cVersion1 := hb_Version ( HB_VERSION_HARBOUR )
cVersion2 := hb_Version ( HB_VERSION_COMPILER )
cVersion3 := qVersion()
oLabel1 := QLabel( oWnd )
oLabel1:resize( 360, 30 )
oLabel1:move( 20, 50 )
oLabel1:setStyleSheet( "background-color : yellow;" )
oLabel1:setText( "Harbour version: " + "<font color=red>" + cVersion1 + "</font>" )
oLabel2 := QLabel( oWnd )
oLabel2:resize( 360, 30 )
oLabel2:move( 20, 100 )
oLabel2:setStyleSheet( "background-color : yellow;" )
oLabel2:setText( "GCC version: " + "<font color=red>" + cVersion2 + "</font>" )
oLabel3 := QLabel( oWnd )
oLabel3:resize( 360, 30 )
oLabel3:move( 20, 150 )
oLabel3:setStyleSheet( "background-color : yellow;" )
oLabel3:setText( "QT version: " + "<font color=red>" + cVersion3 + "</font>" )

oWnd:show()
QApplication():exec()
RETURN

QWidget - Widgets in a Main Window


The following example shows how to create some widgets in a main window. The widgets contain buttons. If you have to move the buttons, simply move the widget only. The Main Window is the parent of the widgets. The widgets are the
parent of the buttons.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oGroup1,oGroup2,oGroup3
oButton1,oButton2,oButton3
oButton4,oButton5,oButton6
oButton7,oButton8,oButton9

oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 650, 400 )
oGroup1 := QWidget(oWnd)
oGroup1:resize( 150, 300 )
oGroup1:move( 50, 50 )
oGroup1:setStyleSheet( " background: #CCCCFF; " )
oGroup2 := QWidget(oWnd)
oGroup2:resize( 150, 300 )
oGroup2:move( 250, 50 )
oGroup2:setStyleSheet( " background: #CCFFCC; " )
oGroup3 := QWidget(oWnd)
oGroup3:resize( 150, 300 )
oGroup3:move( 450, 50 )
oGroup3:setStyleSheet( " background: #FFCCCC; " )
oButton1 := QPushButton( oGroup1 )
oButton1:setText( "Push" )
oButton1:move( 25, 50 )
oButton1:resize( 100, 30 )
oButton1:setStyleSheet( " background: #FFAAAA; " )
oButton2 := QPushButton( oGroup1 )
oButton2:setText( "Push" )
oButton2:move( 25, 100 )
oButton2:resize( 100, 30 )
oButton2:setStyleSheet( " background: #AAFFAA; " )
oButton3 := QPushButton( oGroup1 )
oButton3:setText( "Push" )
oButton3:move( 25, 150 )
oButton3:resize( 100, 30 )
oButton3:setStyleSheet( " background: #AAAAFF; " )
oButton4 := QPushButton( oGroup2 )
oButton4:setText( "Push" )
oButton4:move( 25, 50 )
oButton4:resize( 100, 30 )
oButton4:setStyleSheet( " background: #FFAAAA; " )
oButton5 := QPushButton( oGroup2 )
oButton5:setText( "Push" )
oButton5:move( 25, 100 )
oButton5:resize( 100, 30 )
oButton5:setStyleSheet( " background: #AAFFAA; " )
oButton6 := QPushButton( oGroup2 )
oButton6:setText( "Push" )
oButton6:move( 25, 150 )
oButton6:resize( 100, 30 )
oButton6:setStyleSheet( " background: #AAAAFF; " )
oButton7 := QPushButton( oGroup3 )
oButton7:setText( "Push" )
oButton7:move( 25, 50 )
oButton7:resize( 100, 30 )
oButton7:setStyleSheet( " background: #FFAAAA; " )
oButton8 := QPushButton( oGroup3 )
oButton8:setText( "Push" )
oButton8:move( 25, 100 )
oButton8:resize( 100, 30 )
oButton8:setStyleSheet( " background: #AAFFAA; " )
oButton9 := QPushButton( oGroup3 )
oButton9:setText( "Push" )
oButton9:move( 25, 150 )
oButton9:resize( 100, 30 )
oButton9:setStyleSheet( " background: #AAAAFF; " )
oWnd:show()
QApplication():exec()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

6 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

RETURN

QApplication - Beep
The following example shows how to create a simple main window, as application. At exit, the application produces a beep. This method sounds the bell, using the default volume and sound. The function is not available in Qt for
Embedded Linux.

PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:SetFixedSize( 200, 200 )
oWnd:setWindowTitle( "Giovanni" )
oWnd:show()
QApplication():exec()
QApplication():beep()
RETURN

QApplication - Quitting from application


The following example shows how to quit from an application, using a method.

PROCEDURE Main()
LOCAL oButton1, oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 300, 200 )
oButton1 := QPushButton( oWnd )
oButton1:setText( "Quit" )
oButton1:move( 50, 50 )
oButton1:Connect( "clicked()", { || QApplication():quit() } )
oWnd:show()
QApplication():exec()
RETURN

QIcon - Widget with Icon


The following example shows how to add an icon to a widget.

PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni with icon" )
oWnd:resize( 500, 200 )
oWnd:setwindowicon( QIcon( "harbour-icon.png" ) )
oWnd:show()
QApplication():exec()
RETURN

QDateEdit - Editing a date


The following example shows how to enter and edit a date.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

7 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oDate
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oDate := QDateEdit( oWnd )
oDate:move( 60, 30 )
oWnd:show()
QApplication():exec()
RETURN

QDateEdit - Editing a date and displaying the week day


The following example shows how to enter and edit a date. The program shows also the week day of the date.

PROCEDURE Main()
LOCAL oWnd, oDate, oText
SET DATE italian
oWnd := QMainWindow()
oWnd:SetFixedSize( 400, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oDate := QDateEdit( oWnd )
oDate:resize( 200, 30 )
oDate:move( 50, 50 )
oDate:Connect( "dateChanged(QDate)" , { |x|oText:setText( CDOW(CToD(oDate:text(x ) ) ) ) } )
oText := QLabel( oWnd )
oText:setText( "Change the date" )
oText:resize( 200, 30 )
oText:move( 80, 100 )
oWnd:show()
QApplication():exec()
RETURN

QDateEdit - How to show current date


By default QDateEdit shows the date 01/01/2000. You can show the actual date using setdate() function.

PROCEDURE Main()
LOCAL oWnd, oDate
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 200, 100 )
oDate := QDateEdit( oWnd )
oDate:move( 60, 20 )
oDate:SetDate ( QDate( Year( Date() ), Month( Date() ), Day( Date() ) )

oWnd:show()
QApplication():exec()
RETURN

QTimeEdit - Editing the time


The following example shows how to enter the time.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

8 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oSetting
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 250, 150 )
oSetting := QTimeEdit( oWnd )
oSetting:move( 100, 50 )
oWnd:show()
QApplication():exec()
RETURN

QCalendarWidget - Simple Calendar


The following example shows how to create a simple window containing a calendar.

PROCEDURE Main()
LOCAL oWnd
LOCAL oCalendar
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oCalendar := QCalendarWidget( oWnd )
oCalendar:resize( 250, 200 )
oCalendar:move( 50, 50 )
oCalendar:setFirstDayOfWeek( 1 )
oCalendar:setGridVisible( .T. )
oWnd:show()
QApplication():exec()
RETURN

QCalendarWidget - Interactive Calendar


The following example shows how to create a simple window containing an interactive calendar. The buttons allow to navigate through the months and to show or hide the grid.

PROCEDURE Main()
LOCAL oWnd
LOCAL oCal
LOCAL oBPrev, oBNext, oBGrid
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oCal := QCalendarWidget( oWnd )
oCal:resize( 300, 200 )
oCal:move( 50, 50 )
oCal:setFirstDayOfWeek( 1 )
oCal:setGridVisible( .T. )
oBPrev := QPushButton( oWnd )
oBPrev:setText( "Prev Month" )
oBPrev:move( 100, 5 )
oBPrev:resize( 90, 25 )
oBPrev:Connect( "clicked()", { || oCal:showPreviousMonth() } )
oBNext := QPushButton( oWnd )
oBNext:setText( "Next Month" )
oBNext:move( 200, 5 )
oBNext:resize( 90, 25 )
oBNext:Connect( "clicked()", { || oCal:showNextMonth() } )
oBGrid := QPushButton( oWnd )
oBGrid:setText( "Show/Hide Grid" )
oBGrid:move( 100, 260 )
oBGrid:resize( 200, 25 )
oBGrid:Connect( "clicked()", { || oCal:setGridVisible( ! oCal:isGridVisible() ) } )
oWnd:show()
QApplication():exec()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

9 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

RETURN

QCalendarWidget - Setting a date


This example shows how to set any date in the calendar.

PROCEDURE Main()
LOCAL oWnd
LOCAL oCalendar
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oCalendar := QCalendarWidget( oWnd )
oCalendar:resize( 250, 200 )
oCalendar:move( 50, 50 )
oCalendar:setFirstDayOfWeek( 1 )
oCalendar:setGridVisible( .T. )
oCalendar:setSelectedDate( QDate( 1967, 1, 7 ) )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Text Label


The following example shows how to create a simple main window with a label, used as text string.

PROCEDURE Main()
LOCAL oWnd
LOCAL oText
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oText := QLabel( oWnd )
oText:setText( "Hello World" )
oText:move( 100, 100 )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Image
The following example shows how to view an image, using the QLabel class.

PROCEDURE Main()
LOCAL oWnd
LOCAL oImg
oWnd := QMainWindow()
oWnd:SetFixedSize( 400, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oImg := QLabel( oWnd )
oImg:move( 50, 50 )
oImg:resize( 300, 200 )
oImg:SetPixmap( QPixmap( "sample.png" ) )
oImg:setStyleSheet( "border: 2px solid #0000ff;" )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Colored Text Label (QSS)


The following example shows how to create a simple main window with a label used as text string. The label is colored using QSS.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

10 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oText
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oText := QLabel( oWnd )
oText:setStyleSheet( "background-color : yellow; color : red;" )
oText:setText( "Hello World" )
oText:move( 100, 100 )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Colored Text Label (QPalette)


The following example shows how to create a simple main window with a label used as text string. The label is colored using QPalette.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oPalette
LOCAL oText
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oPalette := QPalette()
oPalette:SetColor( QPalette_WindowText, QColor( 255,0,0 ) )
oText := QLabel( oWnd )
oText:setPalette( oPalette )
oText:setText( "Hello World" )
oText:move( 100, 100 )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Colored Text Label (HTML)


The following example shows how to create a simple main window with a label used as text string. The label is colored and formatted using HTML.

PROCEDURE Main()
LOCAL oWnd
LOCAL oText
LOCAL cString
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 500, 300 )
cString
cString
cString
cString
cString
cString
cString
cString
cString

:=
:=
:=
:=
:=
:=
:=
:=
:=

"Hello World <br><br>"


cString + "This is a complex text, colored and formatted "
cString + "by HTML. <br><br>"
cString + "<FONT color=#FF0000>RED</FONT><br>"
cString + "<FONT color=#008800>GREEN</FONT><br>"
cString + "<FONT color=#0000FF>BLUE</FONT><br><br>"
cString + "<b>Bold Text</b> <br>"
cString + "<i>Italic Text</i> <br>"
cString + "<u>Underlined Text</u> <br"

oText := QLabel( oWnd )


oText:setText( cString )
oText:move( 0, 0 )
oText:resize( 500, 300 )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Circular Label


The following example shows how to create a circular label. The label is colored and formatted using QSS.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

11 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oLabel := QLabel( oWnd )
oLabel:move( 50, 50 )
oLabel:resize( 100, 100 )
oLabel:setText( "79" )
oLabel:setStyleSheet( "border: 1px solid #0000FF; background-color: yellow; border-radius: 50px;" )
oLabel:setAlignment( Qt_AlignHCenter + Qt_AlignVCenter )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Horizontal line


The following example shows how to simulate a horizontal line. A good solution is to create a label with a thickness of 1 or 2 pixels.

PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel, oLabel2
oWnd := QMainWindow()
oWnd:SetFixedSize( 400, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oLabel := QLabel( oWnd )
oLabel:move( 25, 100 )
oLabel:resize( 350, 1 )
oLabel:setStyleSheet( "background-color: black" )
oLabel2 := QLabel( oWnd )
oLabel2:move( 25, 200 )
oLabel2:resize( 350, 2 )
oLabel2:setStyleSheet( "background-color: black" )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Vertical line


The following example shows how to simulate a vertical line. A good so- lution is to create a label with a thickness of 1 or 2 pixels.

PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel, oLabel2
oWnd := QMainWindow()
oWnd:SetFixedSize( 400, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oLabel := QLabel( oWnd )
oLabel:move( 150, 25 )
oLabel:resize( 1, 250 )
oLabel:setStyleSheet( "background-color: black" )
oLabel2 := QLabel( oWnd )
oLabel2:move( 250, 25 )
oLabel2:resize( 2, 250 )
oLabel2:setStyleSheet( "background-color: black" )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Grid of lines


The following example shows how to simulate grid of lines. A good solution is to create two arrays of labels, with a thickness of 1 or 2 pixels. The first array is used for horizontal lines, the second array is used for vertical lines.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

12 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oHorizontalLine[12], oVerticalLine[6]
oNumber[11,5]
nR, nC, nN

oWnd := QMainWindow()
oWnd:SetFixedSize( 500, 600 )
oWnd:setWindowTitle( "Finestra Giovanni" )
nN := 0
for nR := 1 TO 11
for nC := 1 TO 5
nN ++
oNumber[nR,nC] := QLabel( oWnd )
oNumber[nR,nC]:setStyleSheet( "border: 1px solid #0000FF; background-color: #FFFF88; border-radius: 20px; color:red;" )
oNumber[nR,nC]:setText( "<b>" + AllTrim( Str(nN ) ) + "</b>" )
oNumber[nR,nC]:move( nC * 60 + 50, nR * 50 - 25 )
oNumber[nR,nC]:resize( 40, 40 )
oNumber[nR,nC]:setAlignment( Qt_AlignHCenter + Qt_AlignVCenter )
next nC
next nR
for nR := 1 TO 12
oHorizontalLine[nR] := QLabel( oWnd )
oHorizontalLine[nR]:resize( 300, 1 )
oHorizontalLine[nR]:move( 100, nR * 50 - 30 )
oHorizontalLine[nR]:setStyleSheet( "background-color:#000000;" )
next nR

for nC := 1 TO 6
oVerticalLine[nC] := QLabel( oWnd )
oVerticalLine[nC]:resize( 1, 550 )
oVerticalLine[nC]:move( nC * 60 + 40, 20 )
oVerticalLine[nC]:setStyleSheet( "background-color:#000000;" )
next nC
oWnd:show()
QApplication():exec()
RETURN

QLabel - Modify the Frames


The following example shows how to change the frame of the labels.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oText1, oText2, oText3
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 250 )
oText1 := QLabel( oWnd )
oText1:setText( "Hello World" )
oText1:move( 100, 50 )
oText1:setFrameStyle( QFrame_Box )
oText2 := QLabel( oWnd )
oText2:setText( "Hello World" )
oText2:move( 100, 100 )
oText2:setFrameStyle( QFrame_Raised + QFrame_Panel )
oText3 := QLabel( oWnd )
oText3:setText( "Hello World" )
oText3:move( 100, 150 )
oText3:setFrameStyle( QFrame_Sunken + QFrame_Panel )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Alignment
The following example shows how to align a text in a QLabel.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

13 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oText1, oText2, oText3
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 250 )
oText1 := QLabel( oWnd )
oText1:setText( "Hello World" )
oText1:move( 100, 50 )
oText1:setFrameStyle( QFrame_Box )
oText1:setAlignment( Qt_AlignLeft + Qt_AlignTop )
oText2 := QLabel( oWnd )
oText2:setText( "Hello World" )
oText2:move( 100, 100 )
oText2:setFrameStyle( QFrame_Box )
oText2:setAlignment( Qt_AlignHCenter + Qt_AlignVCenter )
oText3 := QLabel( oWnd )
oText3:setText( "Hello World" )
oText3:move( 100, 150 )
oText3:setFrameStyle( QFrame_Box )
oText3:setAlignment( Qt_AlignRight + Qt_AlignBottom )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Setting a number


The following example shows how to set a number in a QLabel, without the use of strings.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oText1
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 150 )
oText1 := QLabel( oWnd )
oText1:setNum( 2011 )
oText1:move( 100, 50 )
oText1:setFrameStyle( QFrame_Box )
oText1:setAlignment( Qt_AlignCenter )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Rectangle
The following example shows how to create a green rectangle with a QLabel.

PROCEDURE Main()
LOCAL oWnd
LOCAL oRectangle
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oRectangle := QLabel( oWnd )
oRectangle:resize( 300, 200 )
oRectangle:move( 50, 50 )
oRectangle:setStyleSheet( "background-color: green;" )
oWnd:show()
QApplication():exec()
RETURN

QLabel - Showing an image from Internet


The following example shows how to view an image from Internet. You can download the image from Internet and store it into your hard disk. Then you can show it as a local image. You have to link the program against hbtip library.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

14 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd, oImg
LOCAL oHttp, cString
oHttp := TIPClientHTTP():new( "http://www.televideo.rai.it/televideo/pub/tt4web/Nazionale/16_9_page-101.png" )
oHttp:open()
cString := oHttp:readAll()
oHttp:close()
hb_MemoWrit( "televideo.png", cString )
oWnd := QMainWindow()
oWnd:SetFixedSize( 700, 500 )
oWnd:setStyleSheet( " background-color: #CCCCFF; " )
oWnd:setWindowTitle( "Giovanni" )
oImg := QLabel( oWnd )
oImg:move( 28, 50 )
oImg:resize( 644, 400 )
oImg:SetPixmap( QPixmap( "televideo.png" ) )
oWnd:show()
QApplication():exec()
RETURN

QMessageBox - Message Box (simple)


The following example shows how to create a simple window with an active button. If the button is pressed, a message box appears.

PROCEDURE Main()
LOCAL oWnd
LOCAL oButton1
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oButton1 := QPushButton( oWnd )
oButton1:setText( "Press for message" )
oButton1:resize( 300, 50 )
oButton1:move( 50, 50 )
oButton1:Connect( "clicked()", { || message() } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE message()
LOCAL oBox
oBox := QMessageBox()
oBox:setInformativeText( "attention!!! " )
oBox:setWindowTitle( "Informazione" )
oBox:exec()
RETURN

QMessageBox - Message Box (Yes and No buttons)


The following example shows how to create a simple window with the Yes and No button. If the Yes button is pressed, the window title changes.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

15 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd,oButton1
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 180 )
oButton1 := QPushButton( oWnd )
oButton1:setText( "Press to change title" )
oButton1:resize( 300, 50 )
oButton1:move( 50, 50 )
oButton1:Connect( "clicked()", { || MsgYesNo(oWnd) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE MsgYesNo(oW)
LOCAL oMB, nButtonPressed
oMB := QMessageBox()
oMB:setInformativeText( "Ok to change the Window Title ?" )
oMB:setWindowTitle( "Information" )
oMB:setWindowFlags( Qt_Dialog )
oMB:setStandardButtons( QMessageBox_Yes + QMessageBox_No )
oMB:setDefaultButton( QMessageBox_Yes )
nButtonPressed := oMB:exec()
IF nButtonPressed == QMessageBox_Yes
oW:setWindowTitle( "Title changed" )
ENDIF
RETURN

QPushButton - Simple button


The following example shows how to create a simple button. If pressed, the window will be resized.

PROCEDURE Main()
LOCAL oButton1, oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 400, 300 )
oButton1 := QPushButton( oWnd )
oButton1:setText( "Resize Window" )
oButton1:move( 50, 50 )
oButton1:Connect( "clicked()", { || edit( oWnd ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE edit( oWnd )
oWnd:resize( 500, 400 )
RETURN

QPushButton - Button with icon


The following example shows how to create a simple button with a icon. If pressed, the title bar changes.

PROCEDURE Main()
LOCAL oButton2, oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 400, 300 )
oButton2 := QPushButton( oWnd )
oButton2:setText( "Press to change title bar" )
oButton2:move( 50, 100 )
oButton2:setIcon( QIcon( "star.png" ) )
oButton2:resize( 300, 50 )
oButton2:Connect( "clicked()", { || edit( oWnd ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE edit( oWnd )
oWnd:setWindowTitle( "Ok, changed" )
RETURN

QPushButton - Buttons
The following example shows how to create a simple window with two active buttons. If the first button is pressed, the window closes. If the second button is pressed, the title of the window changes his value.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

16 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oButton1, oButton2, oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Prova dei pulsanti" )
oWnd:resize( 640, 480 )
oButton1 := QPushButton( oWnd )
oButton1:setText( "Quit" )
oButton1:move( 50, 50 )
oButton1:Connect( "clicked()", { || QApplication():quit() } )
oButton2 := QPushButton( oWnd )
oButton2:setText( "Premere per modificare la barra del titolo" )
oButton2:move( 50, 100 )
oButton2:setIcon( QIcon( "star.png" ) )
oButton2:resize( 300, 50 )
oButton2:Connect( "clicked()", { || edit( oWnd ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE edit( oWnd )
oWnd:setWindowTitle( "Evviva, ci sono riuscito !!!!!!!!!" )
RETURN

QPushButton - Array of Buttons


The following example shows how to create many buttons, stored in an array. The buttons have no actions.

PROCEDURE Main()
LOCAL oWnd
LOCAL oButton[13]
LOCAL k
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 450 )
oWnd:setWindowTitle( "Finestra Giovanni" )

for k := 1 TO 13
oButton[k] := QPushButton( oWnd )
oButton[k]:resize( 150, 25 )
oButton[k]:move( 75, 30 * k )
oButton[k]:setText( "Button " + AllTrim( Str(k ) ) )
next k
oWnd:show()
QApplication():exec()
RETURN

QPushButton - Array of Buttons with variable dimensions


The following example shows how to create many buttons, stored in an array. Their dimension is set by an incremental variable.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

17 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oButton[13]
LOCAL k
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 450 )
oWnd:setWindowTitle( "Finestra Giovanni" )

for k := 1 TO 13
oButton[k] := QPushButton( oWnd )
oButton[k]:resize( 150 + ( k * 5 ), 25 )
oButton[k]:move( 40, 30 * k )
oButton[k]:setText( "Button " + AllTrim( Str(k ) ) )
next k
oWnd:show()
QApplication():exec()
RETURN

QPushButton - Button Tips


The following example shows how to create a button with the tip.

PROCEDURE Main()
LOCAL oWnd
LOCAL oButton1
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 300, 200 )
oButton1 := QPushButton( oWnd )
oButton1:setText( "Press" )
oButton1:move( 50, 50 )
oButton1:setToolTip( "This is an help that explains the function of the button" )
oWnd:show()
QApplication():exec()
RETURN

QPushButton - Button that plays a Wav file


The following example shows how to create a button to miaow a cat.

PROCEDURE Main()
LOCAL oButton1, oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 300, 200 )
oButton1 := QPushButton( oWnd )
oButton1:setText( "Cat" )
oButton1:move( 50, 50 )
oButton1:Connect( "clicked()", { || cat( ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE cat()
QSound( "cat.wav" ):play()
RETURN

QPushButton - Array of Buttons that change the title bar (1)


The following example shows how to create many buttons, stored in an array. Every button changes the title bar, showing its progressive number. This example does not use the macro.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

18 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oButton[13]
LOCAL k
LOCAL oWnd
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 450 )
oWnd:setWindowTitle( "Finestra Giovanni" )
for k := 1 TO 13
oButton[k] := QPushButton( oWnd )
oButton[k]:resize( 150, 25 )
oButton[k]:move( 75, 30 * k )
oButton[k]:setText( "Button " + hb_ntos( k ) )
oButton[k]:Connect( "clicked()", TitleBlock( oWnd, k ) )
next k
oWnd:show()
QApplication():exec()
RETURN
STATIC FUNCTION TitleBlock( oWnd, k )
RETURN { || oWnd:setWindowTitle( Str( k ) ) }

QPushButton - Array of Buttons that change the title bar (2)


The following example shows how to create many buttons, stored in an array. Every button changes the title bar, showing its progressive number. This example uses the macro. It uses also a PRIVATE variable. The use of MEMVAR,
PUBLIC or PRIVATE variables is a very bad practice and no (new) component should ever _require_ such practice.

PROCEDURE Main()
LOCAL oButton[13]
LOCAL k
LOCAL cAction
MEMVAR oWnd
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 450 )
oWnd:setWindowTitle( "Finestra Giovanni" )
for k := 1 TO 13
oButton[k] := QPushButton( oWnd )
oButton[k]:resize( 150, 25 )
oButton[k]:move( 75, 30 * k )
oButton[k]:setText( "Button " + hb_ntos( k ) )
cAction := "{|| change_title( oWnd , " + hb_ntos( k ) + " )}"
oButton[k]:Connect( "clicked()", &( cAction ) )
next k
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE change_title( oW, cNumber )
oW:setWindowTitle( Str( cNumber ) )
RETURN

QPushButton - Array of Buttons that change the title bar (3)


The following example shows how to create many buttons, stored in an array. Every button changes the title bar, showing its progressive number. This example does not use the macro. The buttons are created in a FOR loop, but the
Connection is created manually, one for each button.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

19 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oButton[13]
LOCAL k
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 450 )
oWnd:setWindowTitle( "Finestra Giovanni" )
for k := 1 TO 13
oButton[k] := QPushButton( oWnd )
oButton[k]:resize( 150, 25 )
oButton[k]:move( 75, 30 * k )
oButton[k]:setText( "Button " + hb_ntos( k ) )
next k
oButton[1]:Connect( "clicked()", { || oWnd:setWindowTitle( "1" )
oButton[2]:Connect( "clicked()", { || oWnd:setWindowTitle( "2" )
oButton[3]:Connect( "clicked()", { || oWnd:setWindowTitle( "3" )
oButton[4]:Connect( "clicked()", { || oWnd:setWindowTitle( "4" )
oButton[5]:Connect( "clicked()", { || oWnd:setWindowTitle( "5" )
oButton[6]:Connect( "clicked()", { || oWnd:setWindowTitle( "6" )
oButton[7]:Connect( "clicked()", { || oWnd:setWindowTitle( "7" )
oButton[8]:Connect( "clicked()", { || oWnd:setWindowTitle( "8" )
oButton[9]:Connect( "clicked()", { || oWnd:setWindowTitle( "9" )
oButton[10]:Connect( "clicked()", { || oWnd:setWindowTitle( "10"
oButton[11]:Connect( "clicked()", { || oWnd:setWindowTitle( "11"
oButton[12]:Connect( "clicked()", { || oWnd:setWindowTitle( "12"
oButton[13]:Connect( "clicked()", { || oWnd:setWindowTitle( "13"

}
}
}
}
}
}
}
}
}
)
)
)
)

)
)
)
)
)
)
)
)
)
}
}
}
}

)
)
)
)

oWnd:show()
QApplication():exec()
RETURN

QStatusBar - Status Bar


The following example shows how to create and modify the status bar, at the bottom of the window.

PROCEDURE Main()
LOCAL oWnd
LOCAL oSBar
oWnd := QMainWindow()
oWnd:show()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 300, 200 )
oSBar := QStatusBar( oWnd )
oWnd:setStatusBar( oSBar )
oSBar:showMessage( "Harbour-QT Statusbar Ready!" )
QApplication():exec()
RETURN

QStatusBar - Status Bar and time clock


The following example shows how to show a clock on the status bar, at the bottom of the window.

PROCEDURE Main()
LOCAL oWnd
LOCAL oSBar
LOCAL oClock
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 300, 200 )
oClock := QTimer()
oClock:Connect( "timeout()", { || oSBar:showMessage( Time() ) } )
oClock:start( 1000 )
oSBar := QStatusBar( oWnd )
oWnd:setStatusBar( oSBar )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN

QStatusBar - Status Bar Ping Pong


The following example shows how create a status bar with a ping pong effect. The status bar goes to left and right.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

20 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oClock
oSBar
nSpaces, nInc

oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 300, 200 )
nSpaces := 1
nInc := 1
oClock := QTimer()
oClock:Connect( "timeout()", { || pingpong( oSBar, @nSpaces, @nInc ) } )
oClock:start( 25 )
oSBar := QStatusBar( oWnd )
oSBar:move( 100, 1 )
oWnd:setStatusBar( oSBar )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN
PROCEDURE pingpong( oSb, nSp, nI )
LOCAL cString
cString := Space( nSp ) + "Hello"
oSb:showMessage( cString )
nSp += nI
IF nSp == 40
nI =- 1
ENDIF
IF nSp == 1
nI := 1
ENDIF
RETURN

QStatusBar - Colored Status Bar


The following example shows how to create and modify a colored status bar, at the bottom of the window.

PROCEDURE Main()
LOCAL oWnd
LOCAL oSBar
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 300, 200 )
oSBar := QStatusBar( oWnd )
oWnd:setStatusBar( oSBar )
oSBar:showMessage( "This is a colored Statusbar" )
oSBar:setStyleSheet( "background-color : yellow; color : red;" )
oWnd:show()
QApplication():exec()
RETURN

QStatusBar - Status Bar with timeout


The following example shows how to display a status bar for "n" milliseconds and then it desappears. The value of timeout is expressed in milliseconds.

PROCEDURE Main()
LOCAL oWnd
LOCAL oSBar
oWnd := QMainWindow()
oWnd:show()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 300, 200 )
oSBar := QStatusBar( oWnd )
oWnd:setStatusBar( oSBar )
oSBar:showMessage( "Message displayed for 5 seconds..." , 5000 )
QApplication():exec()
RETURN

QStatusBar - Multiple Status Bar


The following example shows how to create a status bar composed by several widgets.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

21 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd, oStatusBar
LOCAL oText1, oText2, oText3
oWnd := QMainWindow()
oWnd:show()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 600, 200 )
oText1 := QLabel()
oText1:setText( "Status Bar 1" )
oText1:setStyleSheet( "background-color : #BBBBFF;" )
oText2 := QLabel()
oText2:setText( "Date: " + DToC( Date() ) )
oText3 := QLabel()
oText3:setText( "Time: " + Time() )
oText3:setStyleSheet( "color : red;" )
oStatusBar := QStatusBar( oWnd
oStatusBar:addWidget( oText1, 0
oStatusBar:addWidget( oText2, 0
oStatusBar:addWidget( oText3, 0

)
)
)
)

oWnd:setStatusBar( oStatusBar )
QApplication():exec()
RETURN

QStatusBar - Status Bar and Progress Bar


The following example shows how to add a progress bar to the status bar.

PROCEDURE Main()
LOCAL oWnd, oStatusBar, oClock, oText, oBar
oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 500, 200 )
oText := QLabel()
oText:setText( Time() )
oBar := QProgressBar()
oBar:setRange( 1, 100 )
oBar:setValue( 1 )
oClock := QTimer()
oClock:Connect( "timeout()", {|| updateStatusBar( oText, oBar ) } )
oClock:start( 1000 )
oStatusBar := QStatusBar( oWnd )
oStatusBar:addWidget( oText, 0 )
oStatusBar:addWidget( oBar, 1 )
oWnd:setStatusBar( oStatusBar )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN

PROCEDURE updateStatusBar( oT, oB )


oT:setText( Time() )
oB:setValue( oB:value() + 1 )
RETURN

QStatusBar - Comboboxes in the Status Bar


The following example shows how to add comboboxes to the status bar.

PROCEDURE Main()
LOCAL oWnd, oStatusBar
LOCAL oCombo1, oCombo2
oWnd := QMainWindow()
oWnd:show()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 600, 200 )
oCombo1 := QComboBox()
oCombo1:addItem( "Small" )
oCombo1:addItem( "Medium" )
oCombo1:addItem( "Large" )
oCombo2 := QComboBox()
oCombo2:addItem( "COM1"
oCombo2:addItem( "COM2"
oCombo2:addItem( "COM3"
oCombo2:addItem( "COM4"

)
)
)
)

oStatusBar := QStatusBar( oWnd )


oStatusBar:addWidget( oCombo1, 0 )
oStatusBar:addWidget( oCombo2, 0 )
oWnd:setStatusBar( oStatusBar )
QApplication():exec()
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

22 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

QCursor - Cursor Managing


The following example shows how to modify the cursor of the mouse, over widgets.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oCursor
oCursor := QCursor()
oCursor:setShape( Qt_WaitCursor )
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oWnd:SetCursor( oCursor )
oWnd:show()
QApplication():exec()
RETURN

QTabWidget - TAB Control


The following example shows how to create three Tab Widget.

PROCEDURE Main()
LOCAL oWnd
LOCAL oBar
LOCAL oComputer, oMonitor, oPrinter
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 500, 300 )
oComputer
oMonitor
oPrinter

:= QWidget()
:= QWidget()
:= QWidget()

oBar := QTabWidget( oWnd )


oBar:resize( 400, 200 )
oBar:move( 50, 50 )
oBar:addTab( oComputer, "Computers" )
oBar:addTab( oMonitor, "Monitors" )
oBar:addTab( oPrinter, "Printers" )
oWnd:show()
QApplication():exec()
RETURN

QTabWidget - TAB Controls in a TAB Control


The following example shows how to create many Tab Widgets in three Tab Widgets.

PROCEDURE Main()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

23 de 83

LOCAL
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oWnd
oBar, oBar2, oBar3, oBar4
oComputer, oMonitor, oPrinter
oAmd, oIntel
oCrt, oLcd
oLaser, oInk, oDotmatrix, oThermal

oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 500, 300 )
oComputer
oMonitor
oPrinter

:= QWidget()
:= QWidget()
:= QWidget()

oBar := QTabWidget( oWnd )


oBar:resize( 400, 200 )
oBar:move( 50, 50 )
oBar:addTab( oComputer, "Computers" )
oBar:addTab( oMonitor, "Monitors" )
oBar:addTab( oPrinter, "Printers" )
oAmd
:= QWidget()
oIntel := QWidget()
oBar2
:= QTabWidget( oComputer )
oBar2:resize( 200, 100 )
oBar2:move( 50, 50 )
oBar2:addTab( oAmd, "Cpu Amd" )
oBar2:addTab( oIntel, "Cpu Intel" )
oCrt
:= QWidget()
oLcd
:= QWidget()
oBar3
:= QTabWidget( oMonitor )
oBar3:resize( 200, 100 )
oBar3:move( 50, 50 )
oBar3:addTab( oCrt, "Monitor CRT" )
oBar3:addTab( oLcd, "Monitor LCD" )
oLaser
:= QWidget()
oInk
:= QWidget()
oDotmatrix := QWidget()
oThermal
:= QWidget()
oBar4
:= QTabWidget( oPrinter )
oBar4:resize( 380, 100 )
oBar4:move( 10, 50 )
oBar4:addTab( oLaser, "Laser Printer" )
oBar4:addTab( oInk, "Inkjet Printer" )
oBar4:addTab( oDotmatrix, "Dot-matrix Printer" )
oBar4:addTab( oThermal, "Thermal Printer" )
oWnd:show()
QApplication():exec()
RETURN

QTimer - Timer
The following example shows a clock every 1 second, thank to QTimer class. The time of refresh can be adjusted by the start(x) property.

PROCEDURE Main()
LOCAL oWnd
LOCAL oClock
LOCAL oText
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 250, 150 )
oText := QLabel( oWnd )
oText:setText( "clocking..." )
oText:move( 100, 100 )
oClock := QTimer()
oClock:Connect( "timeout()", { || print_clock( oText ) } )
oClock:start( 1000 )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN
PROCEDURE print_clock( oT )
oT:setText( Time() )
RETURN

QTimer - Timer with controls


The following example shows a clock every a second, thank to QTimer class. The time of refresh can be adjusted by the start(x) property. If the Start button is pressed, the time is updated every a second. If the Stop button is pressed, the
time stops.

PROCEDURE Main()
LOCAL oWnd
LOCAL oButtonStart, oButtonStop
LOCAL oClock, oText
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oText := QLabel( oWnd )
oText:setText( "clocking..." )
oText:move( 50, 50 )
oText:resize( 200, 100 )
oClock := QTimer()
oClock:Connect( "timeout()", { || oText:setText( Time()

) } )

oButtonStart := QPushButton( oWnd )


oButtonStart:move( 150, 50 )
oButtonStart:setText( "Start" )
oButtonStart:connect( "pressed()", { || oClock:start( 1000 ) } )
oButtonStop := QPushButton( oWnd )
oButtonStop:move( 150, 100 )
oButtonStop:setText( "Stop" )
oButtonStop:connect( "pressed()", { || oClock:stop() } )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

24 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

QTimer - Timer in the Window Title


The following example shows a clock on the Title bar of a window.

PROCEDURE Main()
LOCAL oWnd
LOCAL oClock
oWnd := QMainWindow()
oWnd:resize( 250, 150 )
oClock := QTimer()
oClock:Connect( "timeout()", { || oWnd:setWindowTitle( Time() ) } )
oClock:start( 1000 )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN

QTimer - Scrolling text


The following example shows how to create a scrolling text, using qTimer class. The scrolling text is a text in a label. Every 100 milliseconds, this string is updated, by removing the first character and putting it at the end of the string. This
gives the illusion of the scrolling.

PROCEDURE Main()
LOCAL oWnd, oText, oClock
oWnd := QMainWindow()
oWnd:setWindowTitle( "Scrolling text" )
oWnd:resize( 400, 200 )
oText := QLabel( oWnd )
oText:setText( "This simple program shows how to scroll a text in a Label.
oText:resize( 300, 40 )
oText:move( 50, 100 )
oText:setStyleSheet( "background-color: #CCCCCC; border: 1px solid #333333;" )

" )

oClock := QTimer()
oClock:Connect( "timeout()", { || scorrimento( oText ) } )
oClock:start( 100 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE scorrimento( o )
LOCAL cSt
cSt := o:Text()
cSt := SubStr( cSt, 2 ) + Left( cSt, 1 )
o:setText( cSt )
RETURN

QTimer - Countdown
The following example shows how to create a program for a countdown. The time starts from 60 seconds. Pressing the "start" button, the countdown starts. You can press the "start" button anytime.

PROCEDURE Main()
LOCAL oWnd
LOCAL oButtonStart

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

25 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

LOCAL oDisplay, oClock


oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oDisplay := QLCDNumber( oWnd )
oDisplay:resize( 100, 50 )
oDisplay:move( 150, 20 )
oDisplay:display( 60 )
oClock := QTimer()
oClock:Connect( "timeout()", { || print_clock( oDisplay,oClock ) } )
oButtonStart := QPushButton( oWnd )
oButtonStart:move( 150, 100 )
oButtonStart:resize( 100, 50 )
oButtonStart:setText( "Start" )
oButtonStart:connect( "pressed()", { || start( oDisplay,oClock ) } )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN
PROCEDURE start( oD, oC )
oD:display( 60 )
oC:start( 1000 )
RETURN
PROCEDURE print_clock( oD, oC )
LOCAL s
s := oD:value()
s := s - 1
oD:display( s )
IF s == 0
oC:stop()
end IF
RETURN

QSS - QSS Style Sheet


The following example shows how we can use CSS to change any property of objects.

PROCEDURE Main()
LOCAL oWnd, oButton1
oWnd := QMainWindow()
oWnd:setWindowTitle( "Prova dei pulsanti" )
oWnd:resize( 300, 200 )
oButton1 := QPushButton( oWnd )
oButton1:setText( "Quit" )
oButton1:move( 50, 50 )
oButton1:Connect( "clicked()", { || QApplication():quit() } )
oButton1:setStyleSheet( "background-color: yellow; border: 2px solid #FF0000;" )
oWnd:show()
QApplication():exec()
RETURN

QSS - Gradient (linear: top to bottom)


The following example shows how we can use CSS to change any property of objects. In particular it shows the creation of a linear gradient.

PROCEDURE Main()
LOCAL oWnd, oLabel
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oLabel := QLabel( oWnd )
oLabel:setText( "Gradient" )
oLabel:move( 50, 50 )
oLabel:resize( 100, 100 )
oLabel:setStyleSheet( "background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,stop:0 #FF0000, stop:1 #FFFF00);" )
oWnd:show()
QApplication():exec()
RETURN

QSS - Gradient (linear: left to right)


The following example shows how we can use CSS to change any property of objects. In particular it shows the creation of a linear gradient.

PROCEDURE Main()
LOCAL oWnd, oLabel
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Finestra Giovanni" )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

26 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oLabel := QLabel( oWnd )


oLabel:setText( "Gradient" )
oLabel:move( 50, 50 )
oLabel:resize( 100, 100 )
oLabel:setStyleSheet( "background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0,stop:0 #0000FF, stop:1 #FFFFFF);" )
oWnd:show()
QApplication():exec()
RETURN

QSS - Gradient (linear: left-top to right-bottom)


The following example shows how we can use CSS to change any property of objects. In particular it shows the creation of a linear gradient.

PROCEDURE Main()
LOCAL oWnd, oLabel
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oLabel := QLabel( oWnd )
oLabel:setText( "Gradient" )
oLabel:move( 50, 50 )
oLabel:resize( 100, 100 )
oLabel:setStyleSheet( "background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1,stop:0 #00FF00, stop:1 #004400);" )
oWnd:show()
QApplication():exec()
RETURN

QSS - Radial Gradient


The following example shows how we can use CSS to change any property of objects. In particular it shows the creation of a radial gradient.

PROCEDURE Main()
LOCAL oWnd, oLabel
oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oLabel := QPushButton( oWnd )
oLabel:setText( "Gradient" )
oLabel:move( 50, 50 )
oLabel:resize( 100, 100 )
oLabel:setStyleSheet( "color: #333; border: 2px solid #555; border-radius: 11px; padding: 5px; background: qradialgradient(cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4, radius: 1.35, stop: 0 #FFFFFF, stop: 1 #AAAAFF); min-width: 80px;" )
oWnd:show()
QApplication():exec()
RETURN

QSS - Colored QLCDNumber


The following example shows a colored QLCDNumber by QSS.

PROCEDURE Main()
LOCAL oWnd
LOCAL oLcd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oLcd := QLCDNumber( oWnd )
oLcd:move( 50, 50 )
oLcd:resize( 300, 100 )
oLcd:setNumDigits ( 8 )
oLcd:setStyleSheet( "color: #FF0000;" )
oLcd:display ( "43'" )
oWnd:show()
QApplication():exec()
RETURN

QMenu - Menu
The following example shows the usage of menu. Every item can be connected to any function or UDF.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

27 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd, oWnd2, oWnd3
LOCAL oMenuBar, oMenu1, oItemIns, oItemMod
// ----------Window----------oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Giovanni" )
oWnd2 := QMainWindow()
oWnd2:SetFixedSize( 640, 480 )
oWnd2:setWindowTitle( "Insert" )
oWnd3 := QMainWindow()
oWnd3:SetFixedSize( 640, 480 )
oWnd3:setWindowTitle( "Edit" )
// ----------Menu----------oMenuBar := QMenuBar( oWnd )
oMenuBar:resize( 300, 30 )
oMenu1 := QMenu()
oMenu1:setTitle( "File" )
oItemIns := QAction( oMenu1 )
oItemIns:setText( "Insert" )
oItemIns:connect( "triggered(bool)", { || oWnd2:Show() } )
oItemMod := QAction( oMenu1 )
oItemMod:setText( "Edit" )
oItemMod:connect( "triggered(bool)", { || oWnd3:Show() } )
oMenu1:addAction( oItemIns )
oMenu1:addAction( oItemMod )
oMenuBar:addMenu( oMenu1 )
oWnd:Show()
QApplication():exec()
RETURN

QMenu - Menu with separators


The following example shows the usage of menu. An item can separated from the other by a line separator. Every item can be connected to any function or UDF.

PROCEDURE Main()
LOCAL oWnd, oWnd2, oWnd3
LOCAL oMenuBar, oMenu1, oItemIns, oItemMod
// ----------Window----------oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Giovanni" )
oWnd2 := QMainWindow()
oWnd2:SetFixedSize( 640, 480 )
oWnd2:setWindowTitle( "Insert" )
oWnd3 := QMainWindow()
oWnd3:SetFixedSize( 640, 480 )
oWnd3:setWindowTitle( "Edit" )
// ----------Menu----------oMenuBar := QMenuBar( oWnd )
oMenuBar:resize( 300, 30 )
oMenu1 := QMenu()
oMenu1:setTitle( "File" )
oItemIns := QAction( oMenu1 )
oItemIns:setText( "Insert" )
oItemIns:connect( "triggered(bool)", { || oWnd2:Show() } )
oItemMod := QAction( oMenu1 )
oItemMod:setText( "Edit" )
oItemMod:connect( "triggered(bool)", { || oWnd3:Show() } )
oMenu1:addAction( oItemIns )
oMenu1:addSeparator()
oMenu1:addAction( oItemMod )
oMenuBar:addMenu( oMenu1 )
oWnd:Show()
QApplication():exec()
RETURN

QMenu - Menu and sub-menu


The following example shows the usage of menu and sub-menu. The sub-menu is also a menu.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oMenuBar, oMenu1, oMenu2
oItemMod
oItemInsImage, oItemInsTable, oItemInsPage

// ----------Window----------oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Giovanni" )
// ----------Menu----------oMenuBar := QMenuBar( oWnd )
oMenuBar:resize( 300, 30 )
oMenu1 := QMenu()
oMenu1:setTitle( "File" )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

28 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oMenu2 := QMenu()
oMenu2:setTitle( "Insert" )
oItemInsImage := QAction( oMenu2 )
oItemInsImage:setText( "Insert Image" )
oItemInsImage:connect( "triggered(bool)", { || image() } )
oMenu2:addAction( oItemInsImage )
oItemInsTable := QAction( oMenu2 )
oItemInsTable:setText( "Insert Table" )
oItemInsTable:connect( "triggered(bool)", { || table() } )
oMenu2:addAction( oItemInsTable )
oItemInsPage := QAction( oMenu2 )
oItemInsPage:setText( "Insert Table" )
oItemInsPage:connect( "triggered(bool)", { || page() } )
oMenu2:addAction( oItemInsPage )
oMenu1:addMenu( oMenu2 )
oItemMod := QAction( oMenu1 )
oItemMod:setText( "Edit" )
oItemMod:connect( "triggered(bool)", { || edit() } )
oMenu1:addAction( oItemMod )
oMenuBar:addMenu( oMenu1 )
oWnd:Show()
QApplication():exec()
RETURN
PROCEDURE edit()
// Type your code here
RETURN
PROCEDURE image()
// Type your code here
RETURN
PROCEDURE table()
// Type your code here
RETURN
PROCEDURE page()
// Type your code here
RETURN

QMenu - Colored Menu


The following example shows the usage of a colored menu. Every item can be connected to any function or UDF.

#define cSTYLE "background-color : yellow; color : red; selection-color: white; selection-background-color: blue;"
PROCEDURE Main()
LOCAL oWnd
LOCAL oMenuBar, oMenu1, oItemIns, oItemMod
// ----------Window----------oWnd := QMainWindow()
oWnd:SetFixedSize( 300, 200 )
oWnd:setWindowTitle( "Giovanni" )
// ----------Menu----------oMenuBar := QMenuBar( oWnd )
oMenuBar:resize( 300, 30 )

oMenu1 := QMenu()
oMenu1:setTitle( "File" )
oMenu1:setStyleSheet( cSTYLE )
oItemIns := QAction( oMenu1 )
oItemIns:setText( "Insert" )
oItemIns:connect( "triggered(bool)", { || insert() } )
oItemMod := QAction( oMenu1 )
oItemMod:setText( "Edit" )
oItemMod:connect( "triggered(bool)", { || edit() } )
oMenu1:addAction( oItemIns )
oMenu1:addAction( oItemMod )
oMenuBar:addMenu( oMenu1 )
oWnd:Show()
QApplication():exec()
RETURN
PROCEDURE insert()
// Type your code here
RETURN
PROCEDURE edit()
// Type your code here
RETURN

QMenu - A complex Menu


The following example shows how to create a complex menu.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oMenuBar
oMenu1, oMenu2, oMenu3
oSubMenu11, oSubMenu12, oSubMenu21, oSubMenu31
oSubMenu113
oItem111, oItem112
oItem1131, oItem1132
oItem211, oItem212

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

29 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

// ---------- Window ----------oWnd := QMainWindow()


oWnd:reSize( 640, 480 )
oWnd:setWindowTitle( "Giovanni" )
// ---------- Menu ----------oMenuBar := QMenuBar( oWnd )
oMenuBar:resize( 640, 30 )
// ---------- Menu 1 ---------oMenu1 := QMenu()
oMenu1:setTitle( "Menu_1" )
// ---------- SubMenu 11 ---------oSubMenu11 := QMenu()
oSubMenu11:setTitle( "SubMenu_11" )
// --------- Items --------oItem111 := QAction( oSubMenu11 )
oItem111:setText( "Item_111" )
oItem111:connect( "triggered(bool)", { || Item_111() } )
oSubMenu11:addAction( oItem111 )
oItem112 := QAction( oSubMenu11 )
oItem112:setText( "Item_112" )
oItem112:connect( "triggered(bool)", { || Item_112() } )
oSubMenu11:addAction( oItem112 )
oSubMenu11:addSeparator() //====================
// ---------- SubMenu 113 ---------oSubMenu113 := QMenu()
oSubMenu113:setTitle( "SubMenu_113" )
// --------- Items --------oItem1131 := QAction( oSubMenu113 )
oItem1131:setText( "Item_1131" )
oItem1131:connect( "triggered(bool)", { ||Item_1131() } )
oSubMenu113:addAction( oItem1131 )
oItem1132 := QAction( oSubMenu113 )
oItem1132:setText( "Item_1132" )
oItem1132:connect( "triggered(bool)", { ||Item_1132() } )
oSubMenu113:addAction( oItem1132 )
oSubMenu11:addMenu( oSubMenu113 )
oMenu1:addMenu( oSubMenu11 )
oMenu1:addSeparator() //====================
// ---------- SubMenu 12 ---------oSubMenu12 := QAction( oMenu1 )
oSubMenu12:setText( "SubMenu_12" )
oSubMenu12:connect( "triggered(bool)", { || SubMenu_12() } )
oMenu1:addAction( oSubMenu12 )
oMenuBar:addMenu( oMenu1 )
// ---------- Menu 2 ---------oMenu2 := QMenu()
oMenu2:setTitle( "Menu_2" )
// ---------- SubMenu 21 ---------oSubMenu21 := QMenu()
oSubMenu21:setTitle( "SubMenu_21" )
// --------- Items --------oItem211 := QAction( oSubMenu21 )
oItem211:setText( "Item_211" )
oItem211:connect( "triggered(bool)", { || Item_211() } )
oSubMenu21:addAction( oItem211 )
oItem212 := QAction( oSubMenu21 )
oItem212:setText( "Item_212" )
oItem212:connect( "triggered(bool)", { || Item_212() } )
oSubMenu21:addAction( oItem212 )
oMenu2:addMenu( oSubMenu21 )
oMenuBar:addMenu( oMenu2 )
// ---------- Menu 3 ---------oMenu3 := QMenu()
oMenu3:setTitle( "Menu_3" )
// ---------- SubMenu 31 ---------oSubMenu31 := QAction( oMenu3 )
oSubMenu31:setText( "SubMenu_31" )
oSubMenu31:connect( "triggered(bool)", { || SubMenu_31() } )
oMenu3:addAction( oSubMenu31 )
oMenuBar:addMenu( oMenu3 )
oWnd:Show()
QApplication():exec()
RETURN
PROCEDURE Item_111()
// Type your code here
RETURN
PROCEDURE Item_112()
// Type your code here
RETURN
PROCEDURE Item_1131()
// Type your code here
RETURN
PROCEDURE Item_1132()
// Type your code here
RETURN
PROCEDURE SubMenu_12()
// Type your code here
RETURN
PROCEDURE Item_211()
// Type your code here
RETURN
PROCEDURE Item_212()
// Type your code here
RETURN
PROCEDURE SubMenu_31()
// Type your code here
RETURN

UI - UI file created with QT Creator

At the moment the QUiLoader() class is not present.


The following example shows how to draw a window or a complete set of widget, from a UI file created with QT Creator program.

PROCEDURE Main()
LOCAL oWnd, oUi, oFile
oFile := QFile( "sample.ui" )
oFile:open( 1 )
oUi := QUiLoader()
oWnd := oUi:load( oFile )
oFile:close()
oWnd:show()
QApplication():exec()
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

30 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

UI - A fully functional example with UI file


The following example uses a file .UI created with QT Creator program. Pressing the button, the title changes.

PROCEDURE Main()
LOCAL oWnd
oWnd := hbqtui_form()
oWnd:setWindowTitle( "Old Title" )
oWnd:q_pushButton:Connect( "clicked()",

{ ||oWnd:setWindowTitle( "Changed" ) } )

oWnd:show()
QApplication():exec()
RETURN

This is the form.ui file, created with QT Creator


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>325</width>
<height>129</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>50</x>
<y>50</y>
<width>211</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Change Title Bar</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

To work correctly, the programmer must remember the following rules:


1. You must create the file "form.ui" (or other name) with QT Creator
2. You must include the "form.ui" file in the compilation (directly or in the file .HBP)
3. To refer to children of the widget, you must "rename" the objects with "q_" at the beginning of the line. For example: q_pushButton
4. The "form.ui" file is not necessary for the execution of the program. It's compiled in the .exe file
5. If the name of the .UI file is "form.ui", then you must call the hbqtui_form() function. If the name of the .UI file is "example.ui", then you must call the hbqtui_example() function.

QTableWidget - Simple table


The following example shows how to create a small table, with 2 rows and 2 columns.

PROCEDURE Main()
LOCAL oWnd
LOCAL oTable
LOCAL oCell, oLabel
oWnd := QMainWindow()
oWnd:resize( 400, 300 )
oWnd:setWindowTitle( "Giovanni" )
//------------Table-------------oTable := QTableWidget( oWnd )
oTable:move( 50, 50 )
oTable:resize( 300, 200 )
oTable:setRowCount( 2 )
oTable:setColumnCount( 2 )
oTable:setColumnWidth( 0, 70 )
oTable:setColumnWidth( 1, 90 )
//------------Fill Table-------------oCell := QTableWidgetItem()
oCell:setText( "1986" )
oTable:setItem( 0, 0, oCell )
oCell := QTableWidgetItem()
oCell:setText( "123" )
oTable:setItem( 0, 1, oCell )
oCell := QTableWidgetItem()
oCell:setText( "1998" )
oTable:setItem( 1, 0, oCell )
oCell := QTableWidgetItem()
oCell:setText( "177" )
oTable:setItem( 1, 1, oCell )
oLabel := QStringList()
oLabel:append( "Year" )
oLabel:append( "Number of Dogs" )
oTable:setHorizontalHeaderLabels( oLabel )
oWnd:show()
QApplication():exec()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

31 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

RETURN

QTableWidget - Arithmetic tables


The following example shows how to create an Arithmetic table, with 100 rows and 4 columns. The first column contains the number, the second column contains the square of the number, the third column contains the square root of the
number and the fourth column contains the cube of the number. The numbers are generated by a loop.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oCell, oLabel
k
oTable

oWnd := QMainWindow()
oWnd:resize( 600, 600 )
oWnd:setWindowTitle( "Giovanni" )
//------------Table-------------oTable := QTableWidget( oWnd )
oTable:move( 10, 10 )
oTable:resize( 580, 580 )
oTable:setRowCount( 100 )
oTable:setColumnCount( 4 )
oTable:setColumnWidth( 0, 70 )
oTable:setColumnWidth( 1, 90 )
//------------Fill Table-------------for k := 1 TO 100
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ) ) )
oTable:setItem( k - 1, 0, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ^ 2 ) ) )
oTable:setItem( k - 1, 1, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(Sqrt(k ) ) ) )
oTable:setItem( k - 1, 2, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ^ 3 ) ) )
oTable:setItem( k - 1, 3, oCell )
next k
oLabel := QStringList()
oLabel:append( "N." )
oLabel:append( "Square" )
oLabel:append( "Square root" )
oLabel:append( "Cube" )
oTable:setHorizontalHeaderLabels( oLabel )
oWnd:show()
QApplication():exec()
RETURN

QTableWidget - Colored Cells


The following example shows how to create a small table, with 2 rows and 2 columns. The cells are colored.

PROCEDURE Main()
LOCAL oWnd
LOCAL oTable
LOCAL oCell, oLabel
oWnd := QMainWindow()
oWnd:resize( 400, 300 )
oWnd:setWindowTitle( "Giovanni" )
//------------Table-------------oTable := QTableWidget( oWnd )
oTable:move( 50, 50 )
oTable:resize( 300, 200 )
oTable:setRowCount( 2 )
oTable:setColumnCount( 2 )
oTable:setColumnWidth( 0, 70 )
oTable:setColumnWidth( 1, 90 )
//------------Fill Table-------------oCell := QTableWidgetItem()
oCell:setText( "1986" )
oCell:setForeground( QBrush( QColor(255,0,0 ) ) )
oCell:setBackground( QBrush( QColor(255,255,100 ) ) )
oTable:setItem( 0, 0, oCell )
oCell := QTableWidgetItem()
oCell:setText( "123" )
oCell:setForeground( QBrush( QColor(0,150,0 ) ) )
oTable:setItem( 0, 1, oCell )
oCell := QTableWidgetItem()
oCell:setText( "1998" )
oCell:setForeground( QBrush( QColor(255,255,0 ) ) )
oCell:setBackground( QBrush( QColor(0,0,150 ) ) )
oTable:setItem( 1, 0, oCell )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

32 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oCell := QTableWidgetItem()
oCell:setText( "177" )
oCell:setForeground( QBrush( QColor(255,0,0 ) ) )
oTable:setItem( 1, 1, oCell )
oLabel := QStringList()
oLabel:append( "Year" )
oLabel:append( "Number of Dogs" )
oTable:setHorizontalHeaderLabels( oLabel )
oWnd:show()
QApplication():exec()
RETURN

QTableWidget - Arithmetic tables (colored columns)


The following example shows how to create an Arithmetic table, with 100 rows and 4 columns. The first column contains the number, the second column contains the square of the number, the third column contains the square root of the
number and the fourth column contains the cube of the number. The numbers are generated by a loop. The columns are colored.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oCell, oLabel
k
oTable

oWnd := QMainWindow()
oWnd:resize( 600, 600 )
oWnd:setWindowTitle( "Giovanni" )
//------------Table-------------oTable := QTableWidget( oWnd )
oTable:move( 10, 10 )
oTable:resize( 580, 580 )
oTable:setRowCount( 100 )
oTable:setColumnCount( 4 )
oTable:setColumnWidth( 0, 70 )
oTable:setColumnWidth( 1, 90 )
//------------Fill Table-------------for k := 1 TO 100
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ) ) )
oCell:setForeground( QBrush( QColor(255,0,0 ) ) )
oCell:setBackground( QBrush( QColor(255,255,100 ) ) )
oTable:setItem( k - 1, 0, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ^ 2 ) ) )
oCell:setForeground( QBrush( QColor(255,255,255 ) ) )
oCell:setBackground( QBrush( QColor(0,0,250 ) ) )
oTable:setItem( k - 1, 1, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(Sqrt(k ) ) ) )
oCell:setForeground( QBrush( QColor(255,255,255 ) ) )
oCell:setBackground( QBrush( QColor(200,50,200 ) ) )
oTable:setItem( k - 1, 2, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ^ 3 ) ) )
oCell:setForeground( QBrush( QColor(0,100,0 ) ) )
oCell:setBackground( QBrush( QColor(200,255,200 ) ) )
oTable:setItem( k - 1, 3, oCell )
next k
oLabel := QStringList()
oLabel:append( "N." )
oLabel:append( "Square" )
oLabel:append( "Square root" )
oLabel:append( "Cube" )
oTable:setHorizontalHeaderLabels( oLabel )
oWnd:show()
QApplication():exec()
RETURN

QTableWidget - Arithmetic tables (conditional coloring of rows)


The following example shows how to create an Arithmetic table, with 100 rows and 4 columns. The first column contains the number, the second column contains the square of the number, the third column contains the square root of the
number and the fourth column contains the cube of the number. The numbers are generated by a loop. The rows are colored following a condition (even and odd).

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

33 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oCell, oLabel
k
oBrushForeground, oBrushBackground
oTable

oWnd := QMainWindow()
oWnd:resize( 600, 600 )
oWnd:setWindowTitle( "Giovanni" )
//------------Table-------------oTable := QTableWidget( oWnd )
oTable:move( 10, 10 )
oTable:resize( 580, 580 )
oTable:setRowCount( 100 )
oTable:setColumnCount( 4 )
oTable:setColumnWidth( 0, 70 )
oTable:setColumnWidth( 1, 90 )
//------------Fill Table-------------for k := 1 TO 100
IF k % 2 == 0
oBrushForeground
oBrushBackground
ELSE
oBrushForeground
oBrushBackground
ENDIF

:= QBrush( QColor( 0,0,200 ) )


:= QBrush( QColor( 220,255,220 ) )
:= QBrush( QColor( 0,0,200 ) )
:= QBrush( QColor( 220,220,255 ) )

oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ) ) )
oCell:setForeground( oBrushForeground )
oCell:setBackground( oBrushBackground )
oTable:setItem( k - 1, 0, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ^ 2 ) ) )
oCell:setForeground( oBrushForeground )
oCell:setBackground( oBrushBackground )
oTable:setItem( k - 1, 1, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(Sqrt(k ) ) ) )
oCell:setForeground( oBrushForeground )
oCell:setBackground( oBrushBackground )
oTable:setItem( k - 1, 2, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ^ 3 ) ) )
oCell:setForeground( oBrushForeground )
oCell:setBackground( oBrushBackground )
oTable:setItem( k - 1, 3, oCell )
next k
oLabel := QStringList()
oLabel:append( "N." )
oLabel:append( "Square" )
oLabel:append( "Square root" )
oLabel:append( "Cube" )
oTable:setHorizontalHeaderLabels( oLabel )
oWnd:show()
QApplication():exec()
RETURN

QTableWidget - Sorting colums


The following example shows how to sort the colums in a table, by pressing the respective buttons.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oTable
oCell, oLabel
k
oButton1, oButton2

oWnd := QMainWindow()
oWnd:resize( 400, 600 )
oWnd:setWindowTitle( "Giovanni" )
//------------Table--------------

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

34 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oTable := QTableWidget( oWnd )


oTable:move( 50, 50 )
oTable:resize( 300, 500 )
oTable:setRowCount( 14 )
oTable:setColumnCount( 2 )
oTable:setColumnWidth( 0, 100 )
oTable:setColumnWidth( 1, 100 )
//------------Headers-------------oLabel := QStringList()
oLabel:append( "Random 1" )
oLabel:append( "Random 2" )
oTable:setHorizontalHeaderLabels( oLabel )
//------------Fill Table-------------for k := 0 TO 13
oCell := QTableWidgetItem()
oCell:setText( StrZero( hb_RandomInt (1,1000 ),4 ) )
oTable:setItem( k, 0, oCell )
oCell := QTableWidgetItem()
oCell:setText( StrZero( hb_RandomInt (1,100 ),3 ) )
oTable:setItem( k, 1, oCell )
next k
//------------Buttons-------------oButton1 := QPushButton( oWnd )
oButton1:setText( "Sort" )
oButton1:move( 70, 10 )
oButton1:Connect( "clicked()", { || oTable:sortItems ( 0 ) } )
oButton2 := QPushButton( oWnd )
oButton2:setText( "Sort" )
oButton2:move( 180, 10 )
oButton2:Connect( "clicked()", { || oTable:sortItems ( 1 )

} )

oWnd:show()
QApplication():exec()
RETURN

QTableWidget - Alternate colors of rows


The following example shows how to color alternating rows, using setAlternatingRowColors function.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oCell, oLabel
k
oTable

oWnd := QMainWindow()
oWnd:resize( 600, 600 )
oWnd:setWindowTitle( "Giovanni" )
//------------Table-------------oTable := QTableWidget( oWnd )
oTable:move( 10, 10 )
oTable:resize( 580, 580 )
oTable:setRowCount( 100 )
oTable:setColumnCount( 2 )
oTable:setColumnWidth( 0, 70 )
oTable:setColumnWidth( 1, 90 )
oTable:setAlternatingRowColors( .T. )
//------------Fill Table-------------for k := 1 TO 100
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ) ) )
oTable:setItem( k - 1, 0, oCell )
oCell := QTableWidgetItem()
oCell:setText( AllTrim( Str(k ^ 2 ) ) )
oTable:setItem( k - 1, 1, oCell )
next k
oLabel := QStringList()
oLabel:append( "N." )
oLabel:append( "Square" )
oTable:setHorizontalHeaderLabels( oLabel )
oWnd:show()
QApplication():exec()
RETURN

QInputDialog - Input Dialog window


The following example uses a input dialog window to insert values into the program.

PROCEDURE Main()
LOCAL oWnd
LOCAL cString
LOCAL oDialog
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

35 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oWnd:resize( 400, 300 )


oDialog := QInputDialog()
cString := oDialog:getText( oWnd, "Title", "What's your name?" )
oWnd:setWindowTitle( cString )
oWnd:show()
QApplication():exec()
RETURN

QColorDialog - Color Dialog


The following example shows how to use a color dialog to change the color of a text label.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd, oButton
LOCAL oColorDialog, oText
oWnd := QMainWindow()
oWnd:resize( 300, 200 )
oWnd:setWindowTitle( "Giovanni" )
oText := QLabel( oWnd )
oText:setText( "<h1>Hello World</h1>" )
oText:move( 20, 20 )
oText:resize( 250, 50 )
oButton := QPushButton( oWnd )
oButton:move( 180, 30 )
oButton:setText( "Change Color" )
oButton:Connect( "clicked()", { || oColorDialog:open() } )
oColorDialog := QColorDialog( oWnd )
oColorDialog:Connect( "currentColorChanged(QColor)", { ||change( oColorDialog,oText ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE change( oCd, cT )
LOCAL oPalette
oPalette := QPalette()
oPalette:SetColor( QPalette_WindowText, oCd:currentColor() )
cT:setPalette( oPalette )
RETURN

QProgressBar - Progress Bar


The following example shows use of a progress bar. It' useful during a long counting or elaboration of big archives.

PROCEDURE Main()
LOCAL oBar
LOCAL k
oBar := QProgressBar()
oBar:resize( 400, 50 )
oBar:move( 50, 50 )
oBar:setRange( 1, 500000 )
oBar:setWindowTitle( "Wait..." )
oBar:Show()
oBar:repaint()
for k := 1 TO 500000
oBar:setValue( k )
next k
oBar:quit()
QApplication():exec()
RETURN

QProgressBar - Progress Bar in a widget


The following example shows use of a progress bar in a widget.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

36 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oBar
LOCAL k
oWnd := QMainWindow()
oWnd:resize( 400, 100 )
oWnd:show()
oBar := QProgressBar( oWnd )
oBar:resize( 300, 20 )
oBar:move( 50, 25 )
oBar:setRange( 1, 500000 )
oBar:Show()
for k := 1 TO 500000
oBar:setValue( k )
next k
QApplication():exec()
RETURN

HTML - Tag HTML


The following example shows how to use tag HTML to change the color, size and aspect of text of other objects.

PROCEDURE Main()
LOCAL oWnd
LOCAL oText
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oText := QLabel( oWnd )
oText:setText( "<font color=#FF0000 size=7>Gio</font>" )
oText:move( 10, 10 )
oText:resize( 280, 100 )
oWnd:show()
QApplication():exec()
RETURN

HTML - Tag HTML


The following example shows how to use tag HTML, like ordered list, text formatting and break row.

PROCEDURE Main()
LOCAL oWnd, oLabel, cString
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
cString
cString
cString
cString
cString
cString
cString
cString
cString

:=
:=
:=
:=
:=
:=
:=
:=
:=

"<font color=#FF0000 size=6>HTML and QLabel</font>"


cString + "<br><br>"
cString + "<b>This is an ordered list</b><br>"
cString + "<ol>"
cString + "<li>Dog</li>"
cString + "<li>Cat</li>"
cString + "<li>Bird</li>"
cString + "</ol>"
cString + "<hr>"

oLabel := QLabel( oWnd )


oLabel:resize( 200, 150 )
oLabel:move( 20, 20 )
oLabel:setText( cString )
oWnd:show()
QApplication():exec()
RETURN

HTML - Tag HTML and Images


The following example shows how to insert an image with the IMG tag HTML.

PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel
LOCAL cString
oWnd := QMainWindow()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

37 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oWnd:setWindowTitle( "Finestra di Giovanni" )


oWnd:resize( 300, 200 )
cString := "<img src='logo_google.bmp'>"
cString := cString + "<br><br>"
cString := cString + "<img src='logo_google.bmp' width=70>"
oLabel := QLabel( oWnd )
oLabel:resize( 200, 150 )
oLabel:move( 20, 20 )
oLabel:setText( cString )
oWnd:show()
QApplication():exec()
RETURN

HTML - Tag HTML and subscript


The following example shows how to use a subscript with the text.

PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel
LOCAL oString
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 600, 100 )
oString := "<font size=5 color=#0000AA>"
oString := oString + "The chemical formula of water is H<sub>2</sub>O"
oString := oString + "</font>"
oLabel := QLabel( oWnd )
oLabel:resize( 600, 100 )
oLabel:move( 0, 0 )
oLabel:setText( oString )
oWnd:show()
QApplication():exec()
RETURN

QSlider - Slider with value


The following example shows a slider that changes the value of a Lcd display. Values are between 0 and 99.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oSlider
LOCAL oLcd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 240, 200 )
oLcd := QLCDNumber( oWnd )
oLcd:resize( 200, 120 )
oLcd:move( 20, 20 )
oSlider := QSlider( oWnd )
oSlider:resize( 211, 21 )
oSlider:move( 10, 160 )
oSlider:setMinimum( 0 )
oSlider:setMaximum( 99 )
oSlider:setSingleStep( 1 )
oSlider:setValue( 0 )
oSlider:setOrientation( Qt_Horizontal )
oSlider:Connect( "valueChanged(int)", { |x| oLcd:display( x ) } )
oWnd:show()
QApplication():exec()
RETURN

QSlider - Sliders RGB


The following example uses three sliders to change the RGB color of a text.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

window
font
oSliderRed, oSliderGreen, oSliderBlu
oText

window := QMainWindow()
window:resize( 320, 400 )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

38 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

window:setWindowTitle( "Giovanni" )
font := QFont()
font:setPointSize( 30 )
font:setBold( .T. )
oText := QLabel( window )
oText:setText( "Colors" )
oText:move( 100, 10 )
oText:resize( 200, 100 )
oText:setfont( font )
oSliderRed := QSlider( window )
oSliderRed:resize( 30, 200 )
oSliderRed:move( 100, 120 )
oSliderRed:setMinimum( 0 )
oSliderRed:setMaximum( 255 )
oSliderRed:setSingleStep( 1 )
oSliderRed:setPageStep( 10 )
oSliderRed:setValue( 0 )
oSliderRed:Connect( "valueChanged(int)", { || change_colors( oText,oSliderRed,oSliderGreen,oSliderBlu ) } )
oSliderGreen := QSlider( window )
oSliderGreen:resize( 30, 200 )
oSliderGreen:move( 150, 120 )
oSliderGreen:setMinimum( 0 )
oSliderGreen:setMaximum( 255 )
oSliderGreen:setSingleStep( 1 )
oSliderGreen:setPageStep( 10 )
oSliderGreen:setValue( 0 )
oSliderGreen:Connect( "valueChanged(int)", { || change_colors( oText,oSliderRed,oSliderGreen,oSliderBlu ) } )
oSliderBlu := QSlider( window )
oSliderBlu:resize( 30, 200 )
oSliderBlu:move( 200, 120 )
oSliderBlu:setMinimum( 0 )
oSliderBlu:setMaximum( 255 )
oSliderBlu:setSingleStep( 1 )
oSliderBlu:setPageStep( 10 )
oSliderBlu:setValue( 0 )
oSliderBlu:Connect( "valueChanged(int)", { || change_colors( oText,oSliderRed,oSliderGreen,oSliderBlu ) } )
window:show()
QApplication():exec()
RETURN
PROCEDURE change_colors( oT, oRed, oGre, oBlu )
LOCAL oPalette
oPalette := QPalette()
oPalette:SetColor( QPalette_WindowText, QColor( oRed:value , oGre:value , oBlu:value ) )
oT:setPalette( oPalette )
RETURN

QSlider - Sliders synchronization


The following example shows how to synchronize two sliders, so that moving a slider, it moves the other.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oSlider1, oSlider2
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 240, 200 )
oSlider1 := QSlider( oWnd )
oSlider1:resize( 211, 21 )
oSlider1:move( 10, 50 )
oSlider1:setMinimum( 0 )
oSlider1:setMaximum( 99 )
oSlider1:setSingleStep( 1 )
oSlider1:setValue( 0 )
oSlider1:setOrientation( Qt_Horizontal )
oSlider1:Connect( "valueChanged(int)", { |x|oSlider2:setValue( x ) } )
oSlider2 := QSlider( oWnd )
oSlider2:resize( 211, 21 )
oSlider2:move( 10, 100 )
oSlider2:setMinimum( 0 )
oSlider2:setMaximum( 99 )
oSlider2:setSingleStep( 1 )
oSlider2:setValue( 0 )
oSlider2:setOrientation( Qt_Horizontal )
oSlider2:Connect( "valueChanged(int)", { |x|oSlider1:setValue( x ) } )
oWnd:show()
QApplication():exec()
RETURN

QDial - Simple Wheel


The following example uses a QDial control, to modify any value. It is a beautiful control and it can be used in many occasions.

PROCEDURE Main()
LOCAL oWnd,oWheel
oWnd := QMainWindow()
oWnd:resize( 400, 300 )
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWheel := QDial( oWnd )
oWheel:move( 100, 50 )
oWheel:resize( 200, 200 )
oWheel:setMinimum( 0 )
oWheel:setMaximum( 10 )
oWheel:setSingleStep( 1 )
oWheel:setWrapping( .F. )
oWnd:show()
QApplication():exec()
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

39 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

QDial - Wheel with a LCD Display


The following example uses a QDial control, to modify the value in a LCD display.

PROCEDURE Main()
LOCAL oWnd, oWheel, oDisplay
oWnd := QMainWindow()
oWnd:resize( 400, 300 )
oWnd:setWindowTitle( "Finestra di Giovanni" )
oDisplay := QLCDNumber( oWnd )
oDisplay:resize( 100, 50 )
oDisplay:move( 150, 20 )
oDisplay:display( 0 )
oWheel := QDial( oWnd )
oWheel:move( 100, 80 )
oWheel:resize( 200, 200 )
oWheel:setMinimum( 0 )
oWheel:setMaximum( 10 )
oWheel:setSingleStep( 1 )
oWheel:setWrapping( .F. )
oWheel:Connect( "valueChanged(int)", { |x| oDisplay:display( x ) } )
oWnd:show()
QApplication():exec()
RETURN

QSpinBox - Spin Control


The following example uses a spin control, to modify the size of a text. Values can be typed or chosen with the arrows.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oFont
oText
oChanger

oWnd := QMainWindow()
oWnd:resize( 320, 200 )
oWnd:setWindowTitle( "Giovanni" )
oFont := QFont()
oFont:setPointSize( 30 )
oText := QLabel( oWnd )
oText:setText( "Text" )
oText:move( 10, 10 )
oText:resize( 280, 100 )
oText:setFont( oFont )
oChanger := QSpinBox( oWnd )
oChanger:move( 50, 150 )
oChanger:resize( 50, 25 )
oChanger:Connect( "valueChanged(int)", { |x|change_dimension(x,oFont,oText) } )
oChanger:setMinimum( 1 )
oChanger:setMaximum( 72 )
oChanger:setSingleStep( 1 )
oChanger:setValue( 30 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE change_dimension(nD,oF,oT)
oF:setPointSize( nD )
oT:setFont( oF )
RETURN

QComboBox - ComboBox
The following example creates a combobox, with many items. User can select any item.

PROCEDURE Main()
LOCAL oWnd
LOCAL oCombo
oWnd := QMainWindow()
oWnd:resize( 320, 200 )
oWnd:setWindowTitle( "Giovanni" )
oCombo := QComboBox( oWnd )
oCombo:move( 100, 50 )
oCombo:resize( 100, 25 )
oCombo:addItem( "Francia" )
oCombo:addItem( "Italia" )
oCombo:addItem( "U.S.A." )
oCombo:addItem( "Germania" )
oCombo:addItem( "Belgio" )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

40 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oCombo:addItem( "Spagna" )
oCombo:addItem( "Portogallo" )
oCombo:addItem( "Islanda" )
oWnd:show()
QApplication():exec()
RETURN

QComboBox - ComboBox with Update


The following example creates a combobox, with many items. User can select any item. When selected, it's shown in a label.

PROCEDURE Main()
LOCAL oWnd, oCombo, oLabel
oWnd := QMainWindow()
oWnd:resize( 320, 200 )
oWnd:setWindowTitle( "Giovanni" )
oCombo := QComboBox( oWnd )
oCombo:move( 20, 20 )
oCombo:resize( 100, 25 )
oCombo:addItem( "Francia" )
oCombo:addItem( "Italia" )
oCombo:addItem( "U.S.A." )
oCombo:addItem( "Germania" )
oCombo:addItem( "Belgio" )
oCombo:addItem( "Spagna" )
oCombo:addItem( "Portogallo" )
oCombo:addItem( "Islanda" )
oCombo:Connect( "currentIndexChanged(int)", { ||oLabel:setText( oCombo:currentText() ) } )
oLabel := QLabel( oWnd )
oLabel:move( 200, 150 )
oWnd:show()
QApplication():exec()
RETURN

QComboBox - Populating a ComboBox from a DBF


The following example creates a combobox, with many items. The items are appended from a DBF database.

PROCEDURE Main()
LOCAL oWnd
LOCAL oCombo
oWnd := QMainWindow()
oWnd:resize( 320, 200 )
oWnd:setWindowTitle( "Giovanni" )
oCombo := QComboBox( oWnd )
oCombo:move( 50, 50 )
oCombo:resize( 200, 25 )
USE cities
DO WHILE .NOT. Eof()
oCombo:addItem( cities -> city )
SKIP
ENDDO
USE
oWnd:show()
QApplication():exec()
RETURN

QFontComboBox - ComboBox with Fonts


The following example creates a combobox, with a set of system fonts. The text is changed with the selected font.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

41 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oText
LOCAL oCombo
oWnd := QMainWindow()
oWnd:resize( 320, 200 )
oWnd:setWindowTitle( "Giovanni" )
oText := QLabel( oWnd )
oText:setText( "Ciao a tutti" )
oText:resize( 200, 80 )
oText:move( 50, 20 )
oCombo := QFontComboBox( oWnd )
oCombo:move( 50, 100 )
oCombo:resize( 200, 25 )
oCombo:Connect( "currentFontChanged(QFont)", { ||oText:setFont( oCombo:currentFont() ) } )
oWnd:show()
QApplication():exec()
RETURN

QLCDNumber - LCD Display


The following example creates a nice LCD display, to view numbers. It's properties can be changed.

PROCEDURE Main()
LOCAL oWnd, oButtonMinus, oButtonPlus, oLcd
oWnd := QMainWindow()
oWnd:resize( 300, 200 )
oWnd:setWindowTitle( "Giovanni" )
oLcd := QLCDNumber( oWnd )
oLcd:move( 50, 50 )
oLcd:resize( 200, 50 )
oButtonMinus := QPushButton( oWnd )
oButtonMinus:resize( 30, 30 )
oButtonMinus:move( 70, 130 )
oButtonMinus:setText( "-" )
oButtonMinus:Connect( "clicked()", { || oLcd:display( oLcd:value() - 1 ) } )
oButtonPlus := QPushButton( oWnd )
oButtonPlus:resize( 30, 30 )
oButtonPlus:move( 200, 130 )
oButtonPlus:setText( "+" )
oButtonPlus:Connect( "clicked()", { || oLcd:display( oLcd:value() + 1 ) } )
oWnd:show()
QApplication():exec()
RETURN

QLCDNumber - LCD with number in decimal, binary, hexdecimal and octal base
The following example creates four LCD displays, that display a number in four bases: decimal, binary, hexdecimal and octal. The number is changed by a Spin Box.

PROCEDURE Main()
LOCAL oWnd, oSpinBox
LOCAL oLabel1, oLabel2, oLabel3, oLabel4
LOCAL oLcd1, oLcd2, oLcd3, oLcd4
oWnd := QMainWindow()
oWnd:resize( 300, 300 )
oWnd:setWindowTitle( "Giovanni" )
oLcd1 := QLCDNumber( oWnd )
oLcd1:move( 60, 10 )
oLcd1:resize( 200, 50 )
oLcd1:SetMode( 1 )
oLcd2 := QLCDNumber( oWnd )
oLcd2:move( 60, 70 )
oLcd2:resize( 200, 50 )
oLcd2:SetMode( 3 )
oLcd3 := QLCDNumber( oWnd )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

42 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oLcd3:move( 60, 130 )


oLcd3:resize( 200, 50 )
oLcd3:SetMode( 0 )
oLcd4 := QLCDNumber( oWnd )
oLcd4:move( 60, 190 )
oLcd4:resize( 200, 50 )
oLcd4:SetMode( 2 )
oLabel1 := QLabel( oWnd )
oLabel1:setText( "Dec" )
oLabel1:move( 25, 20 )
oLabel2 := QLabel( oWnd )
oLabel2:setText( "Bin" )
oLabel2:move( 25, 80 )
oLabel3 := QLabel( oWnd )
oLabel3:setText( "Hex" )
oLabel3:move( 25, 140 )
oLabel4 := QLabel( oWnd )
oLabel4:setText( "Oct" )
oLabel4:move( 25, 200 )
oSpinBox := QSpinBox( oWnd )
oSpinBox:move( 125, 260 )
oSpinBox:resize( 50, 25 )
oSpinBox:Connect( "valueChanged(int)", { |x|disp( x,oLcd1, oLcd2, oLcd3, oLcd4 ) } )
oSpinBox:setMinimum( 0 )
oSpinBox:setMaximum( 31 )
oSpinBox:setSingleStep( 1 )
oSpinBox:setValue( 0 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE DISP( x, oD1, oD2, oD3, oD4 )
oD1:display(
oD2:display(
oD3:display(
oD4:display(

x
x
x
x

)
)
)
)

RETURN

QLCDNumber - Colored LCD Display


The following example creates three nice colored LCD displays.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd, oLcd1, oLcd2, oLcd3, oPalette
oWnd := QMainWindow()
oWnd:resize( 300, 200 )
oWnd:setWindowTitle( "Giovanni" )
oPalette := QPalette()
oPalette:SetColor( QPalette_WindowText, QColor( 255,0,0 ) )
oLcd1 := QLCDNumber( oWnd )
oLcd1:move( 50, 10 )
oLcd1:resize( 200, 50 )
oLcd1:display( 1967 )
oLcd1:setPalette( oPalette )
oPalette:SetColor( QPalette_WindowText, QColor( 0,150,0 ) )
oLcd2 := QLCDNumber( oWnd )
oLcd2:move( 50, 70 )
oLcd2:resize( 200, 50 )
oLcd2:display( 1999 )
oLcd2:setPalette( oPalette )
oPalette:SetColor( QPalette_WindowText, QColor( 50,50,255 ) )
oLcd3 := QLCDNumber( oWnd )
oLcd3:move( 50, 130 )
oLcd3:resize( 200, 50 )
oLcd3:display( 2007 )
oLcd3:setPalette( oPalette )
oWnd:show()
QApplication():exec()
RETURN

QLCDNumber - How to display numbers and text


Digits and other symbols can be shown: 0/O, 1, 2, 3, 4, 5/S, 6, 7, 8, 9/g, minus, decimal point, A, B, C, D, E, F, h, H, L, o, P, r, u, U, Y, colon, degree sign (which is specified as single quote in the string) and space.

PROCEDURE Main()
LOCAL oWnd, oLcd
oWnd := QMainWindow()
oWnd:resize( 700, 200 )
oWnd:setWindowTitle( "Giovanni" )
oLcd := QLCDNumber( oWnd )
oLcd:move( 50, 50 )
oLcd:resize( 600, 50 )
oLcd:setDigitCount ( 30 )
oLcd:display ( "0123456789-. ABCDEFhHLoPruUY:'" )
oWnd:show()
QApplication():exec()
RETURN

QLCDNumber - Digital clock with LCD


The following example shows how to create a real time digital clock.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

43 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oClock
LOCAL oLcd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oLcd := QLCDNumber( oWnd )
oLcd:move( 50, 50 )
oLcd:resize( 300, 100 )
oLcd:setDigitCount( 8 )
oLcd:setStyleSheet( "color: #FF0000;" )
oClock := QTimer()
oClock:Connect( "timeout()", { || print_clock( oLcd ) } )
oClock:start( 1000 )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN
PROCEDURE print_clock( oL )
LOCAL stringa
LOCAL hou, min, sec
hou := SubStr( Time(), 1, 2 )
min := SubStr( Time(), 4, 2 )
sec := SubStr( Time(), 7, 2 )
stringa := hou + ":" + min + ":" + sec
oL:display ( stringa )
RETURN

QRadioButton - Radio Buttons


The following example shows how to use a radio button, with three options. Every option is connected to a function.

PROCEDURE Main()
LOCAL oWnd
LOCAL oButton1, oButton2, oButton3
oWnd := QMainWindow()
oWnd:resize( 400, 300 )
oWnd:setWindowTitle( "Giovanni" )
oButton1 := QRadioButton( oWnd )
oButton1:move( 100, 50 )
oButton1:setText( "Si" )
oButton1:Connect( "clicked()", { || message( "SI" ) } )
oButton2 := QRadioButton( oWnd )
oButton2:move( 100, 80 )
oButton2:setText( "No" )
oButton2:Connect( "clicked()", { || message( "NO" ) } )
oButton3 := QRadioButton( oWnd )
oButton3:move( 100, 110 )
oButton3:setText( "Non so" )
oButton3:Connect( "clicked()", { || message( "NON SO" ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE message( cMsg )
LOCAL oBox
oBox := QMessageBox()
oBox:setInformativeText( cMsg )
oBox:setWindowTitle( "Informazione" )
oBox:exec()
RETURN

QRadioButton - Radio Buttons and images


The following example shows how to use a radio button, with three options. Every option is connected to a function, showing an image.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

44 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd, oImg
LOCAL oButton1, oButton2, oButton3
oWnd := QMainWindow()
oWnd:resize( 400, 200 )
oWnd:setWindowTitle( "Giovanni" )
oButton1 := QRadioButton( oWnd )
oButton1:move( 100, 50 )
oButton1:setText( "Washington" )
oButton1:Connect( "clicked()", { || message( oWnd, oImg, "washington.bmp" ) } )
oButton2 := QRadioButton( oWnd )
oButton2:move( 100, 80 )
oButton2:setText( "Napoleon" )
oButton2:Connect( "clicked()", { || message( oWnd, oImg, "napoleon.bmp" ) } )
oButton3 := QRadioButton( oWnd )
oButton3:move( 100, 110 )
oButton3:setText( "Garibaldi" )
oButton3:Connect( "clicked()", { || message( oWnd, oImg, "garibaldi.bmp" ) } )
oImg := QLabel( oWnd )
oImg:move( 250, 50 )
oImg:resize( 80, 80 )
oImg:setStyleSheet( "border: 2px solid #0000ff;" )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE message( oW, oI, cMsg )
oW:setWindowTitle( cMsg )
oI:SetPixmap( QPixmap( cMsg ) )
RETURN

QVBoxLayout - Vertical Layout


The following example uses the vertical layouts, to place controls without to specify any position. The placing and position is automatic.

PROCEDURE Main()
LOCAL oW, oLay
LOCAL oT0, oT1, oT2, oT3, oT4, oT5, oT6, oB0
oW := QWidget()
oW:setWindowTitle( "Finestra di Giovanni" )
oW:resize( 300, 200 )
oT0 := QLabel()
oT0:setText( "Hello World - Ciao mondo" )
oT1 := QLabel()
oT1:setText( "Line 2 - Linea 2" )
oT2 := QLabel()
oT2:setText( "Hello again - Nuovamente Ciao" )
oT3 := QLabel()
oT3:setText( "Line 3 - Linea 3" )
oT4 := QLabel()
oT4:setText( "Ciao ancora una volta!" )
oT5 := QLabel()
oT5:setText( "Line 5 - Linea 5" )
oT6 := QLabel()
oT6:setText( "Line 6 - Linea 6" )
oB0 := QPushButton()
oB0:setText( "Clicca per terminare / Click to quit" )
oB0:Connect( "clicked()", { || QApplication():quit() } )
oLay := QVBoxLayout( oW )
oLay:addWidget( oT0 )
oLay:addWidget( oT1 )
oLay:addWidget( oT2 )
oLay:addWidget( oT3 )
oLay:addWidget( oT4 )
oLay:addWidget( oT5 )
oLay:addWidget( oT6 )
oLay:addWidget( oB0 )
oW:show()
QApplication():exec()
RETURN

QHBoxLayout - Horizontal Layout


The following example uses the horizontal layouts, to place controls without to specify any position. The placing and position is automatic.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

45 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oW, oLay
LOCAL oP0, oP1, oP2, oP3, oP4, oP5, oP6
oW := QWidget()
oW:setWindowTitle( "Finestra di Giovanni" )
oW:resize( 400, 100 )
oP0 := QPushButton()
oP0:setText( "OK 1" )
oP1 := QPushButton()
oP1:setText( "OK 2" )
oP2 := QPushButton()
oP2:setText( "OK 3" )
oP3 := QPushButton()
oP3:setText( "OK 4" )
oP4 := QPushButton()
oP4:setText( "OK 5" )
oP5 := QPushButton()
oP5:setText( "OK 6" )
oP6 := QPushButton()
oP6:setText( "OK 7" )
oLay := QHBoxLayout( oW )
oLay:addWidget( oP0 )
oLay:addWidget( oP1 )
oLay:addWidget( oP2 )
oLay:addWidget( oP3 )
oLay:addWidget( oP4 )
oLay:addWidget( oP5 )
oLay:addWidget( oP6 )
oW:show()
QApplication():exec()
RETURN

QLineEdit - Line Edit


The following example uses a line edit, to insert and view any data.

PROCEDURE Main()
LOCAL oWnd
LOCAL oName, oLabel
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 150 )
oLabel := QLabel( oWnd )
oLabel:setText( "Name" )
oLabel:move( 50, 50 )
oLabel:resize( 100, 20 )
oName := QLineEdit( oWnd )
oName:move( 160, 50 )
oName:resize( 150, 20 )
oWnd:show()
QApplication():exec()
RETURN

QLineEdit - Array of Line Edit


The following example uses many line edits, stored in an array, to insert and view data.

PROCEDURE Main()
LOCAL oWnd, oName[16], k
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 250 )
for k := 1 TO 8
oName[k] := QLineEdit( oWnd )
oName[k]:move( 20, 22 * k )
oName[k]:resize( 150, 20 )
next k
for k := 9 TO 16
oName[k] := QLineEdit( oWnd )
oName[k]:move( 180, 22 * ( k - 8 ) )
oName[k]:resize( 150, 20 )
next k
oWnd:show()
QApplication():exec()
RETURN

QLineEdit - Merging two Line Edits


The following example uses two line edits, to insert and view data. By pressing the button, the values of the two Line Edits are merged and added to a third Line Edit.

PROCEDURE Main()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

46 de 83

LOCAL
LOCAL
LOCAL
LOCAL

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oWnd
oLabel1, oLabel2
oButton
oName, oSurname, oSum

oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 150 )
oLabel1 := QLabel( oWnd )
oLabel1:setText( "First Name" )
oLabel1:move( 20, 20 )
oLabel1:resize( 100, 20 )
oName := QLineEdit( oWnd )
oName:move( 20, 40 )
oName:resize( 100, 20 )
oLabel2 := QLabel( oWnd )
oLabel2:setText( "Last Name" )
oLabel2:move( 150, 20 )
oLabel2:resize( 100, 20 )
oSurname := QLineEdit( oWnd )
oSurname:move( 150, 40 )
oSurname:resize( 100, 20 )
oButton := QPushButton( oWnd )
oButton:move( 20, 85 )
oButton:setText( "Merge the fields" )
oButton:connect( "clicked()", { || merge( oName,oSurname,oSum ) } )
oSum := QLineEdit( oWnd )
oSum:move( 150, 90 )
oSum:resize( 180, 20 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE merge( oNam, oSur, oSum )
oSum:setText( oNam:text() +

Space( 1 ) + oSur:text() )

RETURN

QLineEdit - Resetting Line Edits


The following example shows how to reset and clear line edits.

PROCEDURE Main()
LOCAL oWnd
LOCAL oButton
LOCAL oF1, oF2, oF3, oF4, oF5, oF6, oF7, oF8
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 270, 300 )
oF1 := QLineEdit( oWnd )
oF1:move( 10, 10 )
oF1:resize( 200, 20 )
oF2 := QLineEdit( oWnd )
oF2:move( 10, 40 )
oF2:resize( 200, 20 )
oF3 := QLineEdit( oWnd )
oF3:move( 10, 70 )
oF3:resize( 250, 20 )
oF4 := QLineEdit( oWnd )
oF4:move( 10, 100 )
oF4:resize( 60, 20 )
oF5 := QLineEdit( oWnd )
oF5:move( 10, 130 )
oF5:resize( 100, 20 )
oF6 := QLineEdit( oWnd )
oF6:move( 10, 160 )
oF6:resize( 50, 20 )
oF7 := QLineEdit( oWnd )
oF7:move( 10, 190 )
oF7:resize( 200, 20 )
oF8 := QLineEdit( oWnd )
oF8:move( 10, 220 )
oF8:resize( 250, 20 )
oButton := QPushButton( oWnd )
oButton:move( 100, 250 )
oButton:resize( 85, 40 )
oButton:setText( "Clear Fields" )
oButton:connect( "clicked()", { || clear_all(oF1, oF2, oF3, oF4, oF5, oF6, oF7, oF8) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE clear_all(oF1, oF2, oF3, oF4, oF5, oF6, oF7, oF8)
oF1:clear()
oF2:setText( "" )
oF3:clear()
oF4:clear()
oF5:clear()
oF6:clear()
oF7:clear()
oF8:clear()

// Clear the field


// Clear the field

RETURN

QLineEdit - Password
The following example shows how to insert a password into a line edit. The asterisks will be shown instead of the characters actually entered.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oPasswd, oLabel
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 150 )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

47 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oLabel := QLabel( oWnd )


oLabel:setText( "Type a password" )
oLabel:move( 50, 50 )
oLabel:resize( 100, 20 )
oPasswd := QLineEdit( oWnd )
oPasswd:move( 160, 50 )
oPasswd:resize( 150, 20 )
oPasswd:setEchoMode( QLineEdit_Password )
oWnd:show()
QApplication():exec()
RETURN

QLineEdit - Order of inputs


The following example shows how to change the order of tabulation, simply changing the position of QLineEdit objects, in the source file.

PROCEDURE Main()
LOCAL oWnd
LOCAL oLineEdit1, oLineEdit2, oLineEdit3, oLineEdit4
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oLineEdit1 := QLineEdit( oWnd )
oLineEdit1:move( 50, 50 )
oLineEdit1:resize( 300, 20 )
oLineEdit2 := QLineEdit( oWnd )
oLineEdit2:move( 50, 100 )
oLineEdit2:resize( 300, 20 )
oLineEdit3 := QLineEdit( oWnd )
oLineEdit3:move( 50, 150 )
oLineEdit3:resize( 300, 20 )
oLineEdit4 := QLineEdit( oWnd )
oLineEdit4:move( 50, 200 )
oLineEdit4:resize( 300, 20 )
oWnd:show()
QApplication():exec()
RETURN

PROCEDURE Main()
LOCAL oWnd
LOCAL oLineEdit1, oLineEdit2, oLineEdit3, oLineEdit4
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oLineEdit3 := QLineEdit( oWnd )
oLineEdit3:move( 50, 150 )
oLineEdit3:resize( 300, 20 )
oLineEdit2 := QLineEdit( oWnd )
oLineEdit2:move( 50, 100 )
oLineEdit2:resize( 300, 20 )
oLineEdit4 := QLineEdit( oWnd )
oLineEdit4:move( 50, 200 )
oLineEdit4:resize( 300, 20 )
oLineEdit1 := QLineEdit( oWnd )
oLineEdit1:move( 50, 50 )
oLineEdit1:resize( 300, 20 )
oWnd:show()
QApplication():exec()
RETURN

QLineEdit - Set Input Mask


The following example shows how to set a mask in a QLineEdit, to filter the characters inserted. The input mask is similar to PICTURE in @ SAY ... GET command.

PROCEDURE Main()
LOCAL oWnd
LOCAL oNumber, oLabel
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 150 )
oLabel := QLabel( oWnd )
oLabel:setText( "Only Six Numbers" )
oLabel:move( 50, 50 )
oLabel:resize( 100, 20 )
oNumber := QLineEdit( oWnd )
oNumber:move( 160, 50 )
oNumber:resize( 150, 20 )
oNumber:setInputMask( "999999" )
oWnd:show()
QApplication():exec()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

48 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

RETURN

Character
Meaning
--------------------------------------------------------------------------------------------A
ASCII alphabetic character required. A-Z, a-z.
a
ASCII alphabetic character permitted but not required.
N
ASCII alphanumeric character required. A-Z, a-z, 0-9.
n
ASCII alphanumeric character permitted but not required.
X
Any character required.
x
Any character permitted but not required.
9
ASCII digit required. 0-9.
0
ASCII digit permitted but not required.
D
ASCII digit required. 1-9.
d
ASCII digit permitted but not required (1-9).
#
ASCII digit or plus/minus sign permitted but not required.
H
Hexadecimal character required. A-F, a-f, 0-9.
h
Hexadecimal character permitted but not required.
B
Binary character required. 0-1.
b
Binary character permitted but not required.
>
All following alphabetic characters are uppercased.
<
All following alphabetic characters are lowercased.
!
Switch off case conversion.
\
Use \ to escape the special characters listed above to use them as separators.
--------------------------------------------------------------------------------------------The mask consists of a string of mask characters and separators, optionally followed
by a semicolon and the character used for blanks. The blank characters are always
removed from the text after editing.
Examples:
Mask
Notes
-------------------------------------------------------------------000.000.000.000;_
IP address; blanks are _
HH:HH:HH:HH:HH:HH;_
MAC address
0000-00-00
ISO Date; blanks are space
>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#
License number; blanks are - and all characters are converted to uppercase

QLineEdit - How to Hide and Show a line edit


The following example shows how to to hide and show a line edit, using a button. Also the label of the button changes, if it's pressed.

PROCEDURE Main()
LOCAL oWnd
LOCAL oNumber1, oButton
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oNumber1 := QLineEdit( oWnd )
oNumber1:move( 150, 50 )
oNumber1:resize( 100, 20 )
oButton := QPushButton( oWnd )
oButton:setText( "Hide Line Edit" )
oButton:move( 125, 150 )
oButton:resize( 150, 25 )
oButton:Connect( "clicked()", { || HideShow( oNumber1, oButton ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE HideShow ( oN, oB )
oN:setVisible ( .NOT. oN:isVisible() )
IF .NOT. oN:isVisible()
oB:setText( "Show Line Edit" )
ELSE
oB:setText( "Hide Line Edit" )
END IF
RETURN

QLineEdit - Navigating with UP/DOWN keys


By default you can navigate across QLineEdits by TAB key. This example shows how to change this behavior, using UP and DOWN cursor keys.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oLineEdit1, oLineEdit2, oLineEdit3
oWnd := QMAINWINDOW()
oWnd:resize( 400, 300 )
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:Connect( QEvent_KeyPress, {| oEvent | fKey( oEvent, oWnd )

} )

oLineEdit1 := QLINEEDIT( oWnd )


oLineEdit1:move( 50, 50 )
oLineEdit1:resize( 300, 20 )
oLineEdit2 := QLINEEDIT( oWnd )
oLineEdit2:move( 50, 100 )
oLineEdit2:resize( 300, 20 )
oLineEdit3 := QLINEEDIT( oWnd )
oLineEdit3:move( 50, 150 )
oLineEdit3:resize( 300, 20 )
oWnd:show()
QAPPLICATION():exec()
RETURN
PROCEDURE fKey( oEvent, oWnd )
DO CASE
CASE oEvent:key() == Qt_Key_Down
QAPPLICATION():sendEvent( oWnd, QKEYEVENT( QEvent_KeyPress, Qt_Key_Tab, Qt_NoModifier ) )
CASE oEvent:key() == Qt_Key_Up
QAPPLICATION():sendEvent( oWnd, QKEYEVENT( QEvent_KeyPress, Qt_Key_Backtab, Qt_NoModifier ) )
ENDCASE
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

49 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

QTextEdit - Rich Text Editor


The following example shows a Rich Text editor and sets his text to bold, italic and size 20, color blue.

PROCEDURE Main()
LOCAL oWnd, oEditor, oFont
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oFont := QFont()
oFont:setBold( .T. )
oFont:setItalic( .T. )
oFont:setPointSize( 20 )
oEditor := QTextEdit( oWnd )
oEditor:resize( 300, 200 )
oEditor:move( 50, 50 )
oEditor:setTextColor( QColor( 0,0,200 ) )
oEditor:setCurrentFont( oFont )
oWnd:show()
QApplication():exec()
RETURN

QTextEdit - Ascii Art


The following example shows how to draw with Ascii Art in a QTextEdit. The font used must be "Courier". The drawing is stored in a string.

#include "hbqtgui.ch"
#define CR Chr( 13 )
PROCEDURE Main()
LOCAL oWnd, oEditor, oFont
LOCAL cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt

:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=

""
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

.,.
.,;CCCCCCCC>-;;.
;CCC>>''';;;<>;;,.`-;.
,CC>' .;''CCCCCCCCCCC>;,.
,cC> ,<CC>.''````''''''<CCC>.
.C'.;CCC>> ,cc$$$$$$$$$h.`'<>.;..
; ;CCC> ,c$$$$$$$$$$$$$$$c -.`'CCC,
',<CC>'.z$$$$$$$$$$$$$$$$$$h.`-;,cCC>
,cCC> c$$$$$$$$$$$$$$$$$$$??$c <CCCCC>
.,<>;CCC;>.
`CCC'.$$$$$$$$$$$$$$$$'',;:::$h <CC''<
.;<CCC''>>''.,;;,.
;CCC>.$$$$$$$$$$$$$$F =',;=-?$$h <CC;
.;CCCCCC>' .;CCCCCCCC;
<CCC'.$$$$$$????$$$$c' '.
,$$$h CCC,
.,<C>>''', .;CCCCCCCCCCCCC,
<CCC'<$$F',r=-;-,?$$$.hcccc$$$$$$.`CC>
`<C,.;CCC> <CCCCCCCCCCCCCCC,
CCC <$$cJ'P'' Jf<$$??$$$$$$$$$$$ C>
; C<CC'''C,cCCCCCCCCCCCC><CCC,
`>>> ?$$L_ - ,c$$i?$$<?$$$$$$$$$$ C,
,C' .,. <'''CCCCC><CCCC<C> CCCC
<C> '$$$$$$$$$$$$$$$h?$$$$$$$$$ CC
<',$$$$cc$ ``'CCC> CCC>,cC <CCC
CC>>.`$$$$$$$$$$>J$$?>?$$$$$$$$ CC
z?$$$$$$$h<C,. <C <CC <CC;.`''
`C> >.`?$$$$$$$$h<,;,c$$?$$$$$$ C'
;L _`'?$$$$$$$$hr` C') CC(`<C,.
`<>.`; ?$$$$$$$$$???'.,c$$$$$','.,..
$$,
$$$$$$$$$$$ `,c,`<C> CC
`;C>. '$$$$$$;ccc??73$$$$' > J$$$$F<$$$$$$$$$' ``'?$$$c `')>'CC,.'
`<CC> `?$$$$$$$$6$$$$$P'. <$$$$$F`$$$??$$$$$`?' '$$$$cr > C>>>
<>./>;'`-,`'?$$$$$$$$P',J$. $$$$$$F $??c;?>J$$hc,.,c$$$$' <>.';
``' ,r<$$$hc,,.`''.,c$$$$$ ''??$h ?hc`'=c$$$$$$$$$$$$',c' '
zJ$$C;$$$$$$$$$$$$$$$$$$$:c, --,.' $L;,-cd$$$$$?????' -cc,
.,c$$$$$$$$$$$$$$$$$$$$$$$$$$:$$$cc `C,`?$$$$$$$??;it$' <$cc`?$c
.z$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$:$$$$$$c,`\..''''$$$P''_,ccc$$$L,$$c
z$$$$$$$$$$$$$$'.,.`' .`?$$$$$$$$$$$$$$$$$$c CC',$
,cd$$$$$$$$$$$$$$
$$$$$$$$$$$$$P' `'??$c,h.'?$$$$$$$$$$$$$$$$$.'' '.zJ$$$$$$$$$$$$$$$$$F
.`'$$$$$$$$$$ `?$$hc$$$$$h ?$$$$$$$$$$$$$$P',cd$$$$$$$$$$$$$$$$$$$$$$F
CC,.'?$$$$$$$ =ccc,J$$$$$$Lcc,.,,;,.```''',J$$$$$$$$$$$$$$$$$$$$$$$$$$.
CCCC>;.'?$$$$$- ''?$$$$$$$$$$$$$$$$$$$$$c$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
`'<CCCC>.'?$$$c`'??$$$$$?$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$P'$$$$$$$$$$$$?
CC;,.`<<<>,.,,,<;,.`''''`?$$$$$$$$$$$$$$$???$$$$$$$$P' zJ$$$$$$$$$??iJ
CCCCCCC>;.`<<CCCCCCCCC;<>;,.`'??$$$$$$C$$$$C$?$???'',c$$$$$$$$$$$$$$$$r
`<<CCCCCCCC>;. ``'<<<<<<>>''', --..`''`''??'' ,;;i$$$$$$$$$$$$$$$$$$$$F
C>;,.,,cC,.,,,;<>;;;,,, .;CCC,;> ,;;,. .,.,,. CCCC$$$$$$$$$$$$$$$$$$$>>c,
CCCC,`'<CCCCCCCCCCCCC' ,<CCCC'.;CCCC>''.,.`'< <CC$$$$$$$$$$$$$$$$$??><J$$c
CCCCCC>;.`'<<CCCCCC> ,CCC>'',-''<>''.;CCCCCC; <CC$$$$$$$$$$$$$$$$$C$$$$$$$
' `''.,,,;;,.`C''' <C>' ,-- .;;> CCCCCCCCCCC <CCC$$$$$$$$$$$$$$$$$$$$$$$$
<CCCCCCCCCCCCC,.`'''.,,-'.;CCC,'> CCCCCCCCCCC,`CCC$$$$$$$$$$$$$$$$$$$$$$$$
CCCCCCCCCCCCCCCC,.`''.,;<CCCCC>.> CCCCCCCCCCCC <CC$$$$$$$$$$$$$$$$$$$$$$$$
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC>, <CCCCCCCCCCCC> CC$$$$$$$$$$$$$$$$$$$$$$$$
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC > <CCCCCCCCCCCCC,`CC$$$?????'''''''''??$$$$

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

50 de 83

cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt

:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=
:=

cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt
cSt

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

CCCCCC>''CCCCCCCCCCCCCCCCCCCC> ; <CCCCCCCCCCCCCC,`'.,cccd$$$$$$$$$$$cc,'?$
CCCCCCC CCCCCCCCCCCCCCCCCCCCC ;' CCCCCCCCCCCC>' ,c$$$$$$$$$$$$$$$$$$$$$h,$
CCCCCCC `CCCCCCCCCCCCCCCCCCCC,< <CCCCCCCCCC>',c$$$$$$$$$$$$$$$$$$$$$$$$$$$
CCCCCCC <<<CCCCCCCCCCCCCCCCC> <CCCCCCCCCC> $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
CCCCCC> <<<<CCCCCCCCCCCCCCC> <>.<CCCCCCCCC J$$$$$$$$$$$$$$$$$$$$$$'' '$$$
CCCCCC
<<<<<<<CCCCCCCCCC>>> <C'<CCCCCC>>> $$$$$$$$$$$$$$$$$$$$$'. '$c '$
CCCCCC
`<<<<<<<<<<<<<>>>>>>' ' <<<<<>>>>> $$$$$$$$$$$$$$$$$$$$$.`?b`?$c
CCCCCC
-;-;-;-;-;-;-;-;;;;;,,,,,........ <$$$$$$$$$$$$$$??$$$$$$h.'h.'?$
CCCCCC ,,;;;; ,;;, ,, ,,. `''''''''''''. J$$$$$$$$$$$$C?'J$$$$$$$$.`?h.'
CCCCC> ,cCCCC',cCC ;CC CCC> <>;;,, ;;, CCC $$$$$$$$$$$$$$$ ''?$F ..`'- ?$c
CCCCC CCCCC> CCCC CCC CCCC>'CCCCC, <C,'C> $$$$$$$$$$$$$$$ $ccc,`'$$hc, '$
CCCC> <CCCCC'<CCCC,)CC <CCCC,`'CCCC,'CC;<C $$$$$$$$$$$$$$F,$$$$$$c, ''??'
'''' `<<CCCC <CCCC>'CC.`CCCC> <CCCCC,<CCCC $$$$$$$$$$$$$',$$$$$$$$$$c
CCC>' .;..`''<<CCCC,cCC CCCCC><CCCCCCCCCC>,$$$$$$$$$$$$',$$$$$$$$$$$$c
C> ,<CCCC>''.,;,,.```---<CCCCCCCCCCCCCCCC <$$$$$$$$$$$' $$$$$$$$$$$$$$c
CCCCCC>'.;<CCCCCCCCCC;;,, `'''<CCCC>>>''',J$$$$$$$$$' :, ?$$$$$$$$$$$$$
CCCCC ,<CCCCCCCCCCCCCCC' z$$$cc,`' ,$c=$$$$$$$$$$?$' ::`: '$$$$$$$$$$$$c,
CCCCCCCCCCCCCCCCCCCCCC ,J$$$$$$$$$c,' .',??$$$$$$c `: .::. '$$$$$$$$$$$$
CCCCCCCCCCCCCCCCCCCCC z$$$$$$$$$$$$$CC;,`,, $$$$$$ ..::'..::. '$$$$$$$$$C
CCCCCCCCCCCCCCCCCCCCC $$$$$$$$$$$$$$$$$CCcc,, '?$F .....:::... `?$$$$$$$>c
CCCCCCCCCCCCCCCCCCCCC ?$$??$$$$$$$$$$$$$$$C'?$hc ``:::': ``'``. '?$$$$$$$
CCCCCCCCCCCCCCCCCCCCC> '??<$$$$$$$$$$$$$$$$$c,`?<> :..` ...`.::: J$$$$$$$$
CCCCCCCCCCCC>>>>''''..,;, `'?$$$$$$$$$$$$F'?$$h.`?.`:: .:::'`,J$$$$$$P''
`''''
.-;>
`'!' <!'`.,;!;, '??$$$$.`'?$h.`?$$h. .`:'.. . ,$$$$$P''.: `
!!,;, `<'
- ``,;<!!'' ,;;;;,''?$$c,`?$$. '?$$.` :'' : P;J$$P .::' `
!!!!!>;,`' .>',, `,;<!!! ;,-`.',!'''- '?$h.`?$h. '?$h. .'.: `'''. :..::::
!!!!!!' ./'`.>',;!!!..' `!''`'.,;;;!!''- '== '?$c '?$r .:::':'' `''` ..
!!!!' .< ,;!' <!!!!!!' ``,;;!!!'``.,;;;!!,,,.,..`.,..,..,..
,;;;!!!!
!!' ;!' ;'`,;!''
;-',;!!!!!!;;!!!!!!!!!!!''''''''''''`!!!!!!!;;!!!!!!!!
' .<' ;!' <!!! ,'' ' ,;!!!!!!!!!!!!!!!!!'`.,;;;;;;;;!!!;;;,.```!!!!!!!!!!!

oWnd := QMainWindow()
oWnd:setWindowTitle(
oWnd:resize( 510, 940 )

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR
CR

"Finestra di Giovanni" )

oFont := QFont( "Courier New", 6 )


oEditor := QTextEdit( oWnd )
oEditor:resize( 490, 920 )
oEditor:move( 10, 10 )
oEditor:setTextColor( QColor( 0,255,255 ) )
oEditor:setStyleSheet( "background-color: #000000" )
oEditor:setFont( oFont )
oEditor:setPlainText( cSt )
oWnd:show()
QApplication():exec()
RETURN

QEvent Close
The following example shows how to intercept the Close Event. In this example, the close window button is ignored and you can exit from the window with the user button ("Quit") only .

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd, oButton1
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oWnd:connect( QEvent_Close , { |x| x:ignore()

} )

oButton1 := QPushButton( oWnd )


oButton1:setText( "Quit" )
oButton1:move( 50, 50 )
oButton1:Connect( "clicked()", { || QApplication():quit() } )
oWnd:show()
QApplication():exec()
RETURN

QEvent KeyPress
The following example shows how to capture a key pressed. The even can capture also Shift, Alt and Ctr keys.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel, oLabel2
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 240, 200 )
oWnd:connect( QEvent_KeyPress , { |k| keypressed( k , oLabel2 ) } )
oLabel := QLabel( oWnd )
oLabel:setText( "Please press a key..." )
oLabel:move( 10, 10 )
oLabel:resize( 200, 50 )
oLabel2 := QLabel( oWnd )
oLabel2:move( 10, 100 )
oLabel2:resize( 220, 50 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE keypressed( x , oL )
LOCAL string
string := "You have pressed the key: " + "<br><br>"
string := string + "VALUE= " + Str( x:key() ) + "<br>"
string := string + "KEY= " + Chr( x:key() )
oL:setText( string )
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

51 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

QEvent KeyPress
The following example shows how to move a window by pressing the arrow keys.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 240, 200 )
oWnd:connect( QEvent_KeyPress , { |k| keypressed( k , oWnd ) } )
oLabel := QLabel( oWnd )
oLabel:setText( "Press Arrow Keys to move the window..." )
oLabel:resize( 220, 20 )
oLabel:move( 10, 10 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE keypressed( kk , oW )
DO CASE
CASE kk:key() == Qt_Key_Up
oW:move( oW:x() , oW:y() - 4
CASE kk:key() == Qt_Key_Down
oW:move( oW:x() , oW:y() + 4
CASE kk:key() == Qt_Key_Left
oW:move( oW:x() - 4 , oW:y()
CASE kk:key() == Qt_Key_Right
oW:move( oW:x() + 4 , oW:y()
ENDCASE

)
)
)
)

RETURN

QEvent Move
The following example shows how to intercept the event of moving the window.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 180 )
oWnd:show()
oWnd:connect( QEvent_Move , { || Message() } )

// After oWnd:show()

QApplication():exec()
RETURN
PROCEDURE Message()
LOCAL oMB
oMB := QMessageBox()
oMB:setInformativeText( "You have moved the window" )
oMB:setWindowTitle( "Harbour-QT" )
oMB:exec()
oMB := NIL
RETURN

QEvent Resize
The following example shows how to intercept the event of resizing the window. The objects in the window are resized and repositioned proportionally.

#include "hbqtgui.ch"
PROCEDURE Main()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

52 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

LOCAL oWnd, oButton


oWnd := QMainWindow()
oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 200, 180 )
oWnd:show()
oWnd:connect( QEvent_Resize , { || pChange( oWnd,oButton )

} )

oButton := QPushButton( oWnd )


oButton:setText( "Quit" )
oButton:move( oWnd:width()/5, oWnd:height()/5 )
oButton:resize( oWnd:width()/6, oWnd:height()/6 )
oButton:show()
QApplication():exec()
RETURN
PROCEDURE pChange( oWnd, oButton )
oButton:resize( oWnd:width()/6, oWnd:height()/6 )
oButton:move( oWnd:width()/5, oWnd:height()/5 )
RETURN

QPainter - Drawing a face


The following example shows how to draw a face with primitives, like circles and lines. This source uses the class QPainter.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oPixmap
oPainter
oLabel

oWnd := QWidget()
oWnd:resize( 300, 200 )
oWnd:setWindowTitle( "Finestra di Giovanni" )
oPixmap := QPixmap( 100, 100 )
oPixmap:fill( QColor( 255,255,150 ) )
oPainter := QPainter( oPixmap )
oPainter:drawEllipse( QPointF( 50,50
oPainter:drawEllipse( QPointF( 40,30
oPainter:drawEllipse( QPointF( 40,30
oPainter:drawEllipse( QPointF( 60,30
oPainter:drawEllipse( QPointF( 60,30
oPainter:drawEllipse( QPointF( 50,50
oPainter:drawLine( 30, 70, 70, 75 )
oPainter:end()

),
),
),
),
),
),

40, 40 )
5, 5 )
1, 1 )
5, 5 )
1, 1 )
5, 15 )

//
//
//
//
//
//
//

Face
Eye
Eye center
Eye
Eye center
Nose
Mouth

oLabel := QLabel( oWnd )


oLabel:setPixmap( oPixmap )
oLabel:move( 100, 20 )
oWnd:show()
QApplication():exec()
RETURN

QPainter - Moving a face


The following example shows how to draw and move a face with buttons. This source uses the class QPainter.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oLabel
oPixmap
oPainter
oButtonLeft, oButtonRight

oWnd := QWidget()
oWnd:resize( 300, 200 )
oWnd:setWindowTitle( "Finestra di Giovanni" )
oPixmap := QPixmap( 100, 100 )
oPixmap:fill( QColor( 255,255,150 ) )
oPainter := QPainter( oPixmap )
oPainter:drawEllipse( QPointF( 50,50
oPainter:drawEllipse( QPointF( 40,30
oPainter:drawEllipse( QPointF( 40,30
oPainter:drawEllipse( QPointF( 60,30
oPainter:drawEllipse( QPointF( 60,30
oPainter:drawEllipse( QPointF( 50,50
oPainter:drawLine( 30, 70, 70, 75 )
oPainter:end()

),
),
),
),
),
),

40, 40 )
5, 5 )
1, 1 )
5, 5 )
1, 1 )
5, 15 )

//
//
//
//
//
//
//

Face
Eye
Eye center
Eye
Eye center
Nose
Mouth

oLabel := QLabel( oWnd )


oLabel:setPixmap( oPixmap )
oLabel:move( 100, 20 )
oButtonLeft := QPushButton( oWnd )
oButtonLeft:setText( "Left" )
oButtonLeft:move( 30, 160 )
oButtonLeft:Connect( "clicked()", { || MoveLeft( oLabel ) } )
oButtonRight := QPushButton( oWnd )
oButtonRight:setText( "Right" )
oButtonRight:move( 200, 160 )
oButtonRight:Connect( "clicked()", { || MoveRight( oLabel ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE MoveLeft( oL )
oL:move( oL:x() - 5, 20 )
RETURN
PROCEDURE MoveRight( oL )
oL:move( oL:x() + 5, 20 )
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

53 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

QPainter - Saving a picture


The following example shows how to save a picture to BMP and PNG files. The picture is not visible on the screen.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oPixmap
oPainter
k, oLabel

oWnd := QWidget()
oWnd:resize( 250, 100 )
oWnd:setWindowTitle( "Finestra di Giovanni" )
oPixmap := QPixmap( 300, 200 )
oPixmap:fill( QColor( 200,255,255 ) )
oPainter := QPainter( oPixmap )
oPainter:drawLine( 30, 70, 70, 75 )
oPainter:drawLine( 10, 10, 70, 33 )
for k := 50 TO 250 STEP 10
oPainter:drawLine( k, 50, k, 150 )
next k
oPixmap:save( "sample.bmp" )
oPixmap:save( "sample.png" )
oLabel := QLabel( oWnd )
oLabel:setText( "BMP, PNG, Saved" )
oLabel:move( 20, 20 )
oLabel:resize( 200, 100 )
oWnd:show()
QApplication():exec()
RETURN

QTreeView - The Tree of directories and folders


The following example shows how to create a tree of the directories of your storage media. When you click on a file or folder, its name is shown in a text label.

PROCEDURE Main()
LOCAL oWnd, oModello, oAlbero, oText
oWnd := QMainWindow()
oWnd:setWindowTitle( "Tree Directories" )
oWnd:setfixedsize( 800, 460 )
oModello := QFileSystemModel()
oModello:setRootPath( "" )
oAlbero := QTreeView( oWnd )
oAlbero:resize( 780, 400 )
oAlbero:move( 10, 10 )
oAlbero:setIndentation( 20 )
oAlbero:setModel( oModello )
oAlbero:setSortingEnabled( .T. )
oAlbero:sortByColumn( 0, 0 )
oAlbero:setColumnWidth( 0, 300 )
oAlbero:setColumnWidth( 1, 100 )
oAlbero:setColumnWidth( 2, 100 )
oAlbero:setColumnWidth( 3, 100 )
oAlbero:connect( "clicked(QModelIndex)", { |i| oText:setText( oModello:filePath( i ) ) } )
oText := QLabel( oWnd )
oText:setStyleSheet( "background-color: yellow; color: red;" )
oText:setText( "" )
oText:move( 10, 420 )
oText:resize( 780, 30 )
oWnd:show()
QApplication():exec()
RETURN

QResource - How to use Resources


The following example shows how to use the resource files. You can use images and compile them in the executable file, so you can distribuite the exe file, without the images. You need, for this example, the following files:

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

54 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

elektrosoft.prg
elektrosoft.hbp
elektrosoft.qrc
logo.png

This is the file elektrosoft.hbp:


-w3
hbqt.hbc
elektrosoft.prg
elektrosoft.qrc

This is the file elektrosoft.qrc:


<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/">
<file>logo.png</file>
</qresource>
</RCC>

Please rename correctly the files and the name of variables.

PROCEDURE Main()
LOCAL oWnd
LOCAL oLogo
LOCAL oRes
oRes := QResource()
oRes:registerResource_1( HBQTRES_ELEKTROSOFT() )
oWnd := QWidget()
oWnd:SetFixedSize( 600, 300 )
oWnd:setStyleSheet( "background:#545A7C;" )
oLogo := QLabel( oWnd )
oLogo:move( 100, 60 )
oLogo:resize( 400, 181 )
oLogo:SetPixmap( QPixmap( ":logo.png" ) )
oWnd:show()
QApplication():exec()
oRes:unregisterResource_1( HBQTRES_ELEKTROSOFT() )
RETURN

QScrollArea - Very simple scrolling area


The following example shows how to create a simple scrolling area. The area can contain any object. In this example, the label contains an image and the scroll area contains the label. The image is composed of 1200x860 pixels (in this
example). You can scroll the image using scrolling bars.

PROCEDURE Main()
LOCAL oWnd
LOCAL oImage
LOCAL oArea
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 640, 480 )
oImage := QLabel( )
oImage:SetPixmap( QPixmap( "background.png" ) )
oArea := QScrollArea( oWnd )
oArea:move( 20, 20 )
oArea:resize( 600, 440 )
oArea:setWidget( oImage )
oWnd:show()
QApplication():exec()
RETURN

QDialog - A modal window (1)


This example shows how to create a modal window, using the "setmodal()" and "show()" functions.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

55 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd, oButton
LOCAL oModal, oPassword
oWnd := QMainWindow()
oWnd:SetFixedSize( 400, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oButton := QPushButton( oWnd )
oButton:setText( "Enter password" )
oButton:move( 100, 50 )
oButton:resize( 150, 30 )
oButton:Connect( "clicked()", {|| oModal:show() } )
// ----------------------------oModal := QDialog()
oModal:SetFixedSize( 300, 200 )
oModal:setWindowTitle( "Modal Window" )
oModal:setModal ( .T. )
oPassword := QLineEdit( oModal )
oPassword:move( 20, 50 )
oWnd:show()
QApplication():exec()
RETURN

QDialog - A modal window (2)


This example shows how to create a modal window, using only the "exec()" slot.

PROCEDURE Main()
LOCAL oWnd, oButton
LOCAL oModal, oPassword
oWnd := QMainWindow()
oWnd:SetFixedSize( 400, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oButton := QPushButton( oWnd )
oButton:setText( "Enter password" )
oButton:move( 100, 50 )
oButton:resize( 150, 30 )
oButton:Connect( "clicked()", {|| oModal:exec() } )
// ----------------------------oModal := QDialog()
oModal:SetFixedSize( 300, 200 )
oModal:setWindowTitle( "Modal Window" )
oPassword := QLineEdit( oModal )
oPassword:move( 20, 50 )
oWnd:show()
QApplication():exec()
RETURN

QToolBar - A simple textual Tool Bar


This example shows how to create a simple Tool Bar, without icons. You can drag the toolbar.

PROCEDURE Main()
LOCAL oWnd, oQToolBar
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 200 )
oQToolBar := QToolBar ( oWnd )
oQToolBar:resize( 380, 30 )
oQToolBar:move( 0, 0 )
oQToolBar:addAction ( "New" )
oQToolBar:addAction ( "Open" )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

56 de 83

oQToolBar:addAction
oQToolBar:addAction
oQToolBar:addAction
oQToolBar:addAction
oQToolBar:addAction

(
(
(
(
(

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

"Save" )
"Cut" )
"Copy" )
"Paste" )
"Print" )

oWnd:show()
QApplication():exec()
RETURN

QToolBar - A simple textual Tool Bar with separator


This example shows how to create a simple Toolbar, without icons. The toolbar has a separator.

PROCEDURE Main()
LOCAL oWnd, oQToolBar
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 200 )
oQToolBar := QToolBar ( oWnd )
oQToolBar:resize( 200, 30 )
oQToolBar:move( 0, 0 )
oQToolBar:addAction ( "Large" )
oQToolBar:addSeparator ()
oQToolBar:addAction ( "Small" )
oWnd:show()
QApplication():exec()
RETURN

QToolBar - A toolbar with icons, actions and tooltips


This example has a toolbar with two icons. Pressing them, the main windows changes its size. The actions have a tooltip.

PROCEDURE Main()
LOCAL oWnd, oQToolBar, oAction1, oAction2
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 200 )
oQToolBar := QToolBar ( oWnd )
oQToolBar:resize( 100, 30 )
oQToolBar:move( 0, 0 )
oAction1 := QAction( oWnd )
oAction1:setIcon( QIcon( "small.png" ) )
oAction1:setToolTip( "Small" )
oAction1:connect( "triggered(bool)", {|| oWnd:resize( 100, 60 ) } )
oAction2 := QAction( oWnd )
oAction2:setIcon( QIcon( "large.png" ) )
oAction2:setToolTip( "Large" )
oAction2:connect( "triggered(bool)", {|| oWnd:resize( 400, 200 ) } )
oQToolBar:addAction( oAction1 )
oQToolBar:addAction( oAction2 )
oWnd:show()
QApplication():exec()
RETURN

QToolBar - Draggable toolbar


This example shows how to create a draggable toolbar.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

57 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd, oQToolBar
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 200 )
oQToolBar := oWnd:addToolbar( "Toolbar" )
oQToolBar:resize( 380, 30 )
oQToolBar:move( 0, 0 )
oQToolBar:addAction ( "New" )
oQToolBar:addAction ( "Open" )
oQToolBar:addAction ( "Save" )
oQToolBar:addAction ( "Cut" )
oQToolBar:addAction ( "Copy" )
oQToolBar:addAction ( "Paste" )
oQToolBar:addAction ( "Print" )
oQToolBar:setFloatable( .T. )
oQToolBar:setMovable( .T. )
oWnd:show()
QApplication():exec()
RETURN

QFileDialog - Very simple File Dialog


This example shows how to create a very simple File Dialog.

PROCEDURE Main()
LOCAL oFD
oFD := QFileDialog()
oFD:show()
QApplication():exec()
RETURN

QFileDialog - A viewer for text files


This example shows how to create a viewer for text files. User can choose the folder and the file. It will be shown on the screen.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

58 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd, oButton, oEditor
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 700, 500 )
oEditor := QTextEdit( oWnd )
oEditor:resize( 600, 400 )
oEditor:move( 50, 50 )
oButton := QPushButton( oWnd )
oButton:setText( "Select a file" )
oButton:move( 10, 10 )
oButton:Connect( "clicked()", {|| viewFile( oEditor ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE viewFile( oEditor )
LOCAL oFileDialog, cListOfFiles, cFileName, cBuffer
oFileDialog := QFileDialog()
oFileDialog:exec()
cListOfFiles := oFileDialog:selectedFiles()
cFileName := cListOfFiles:At( 0 )
cBuffer := MemoRead( cFileName )
oEditor:setText( cBuffer )
RETURN

QFileDialog - A viewer for BMP and PNG


This example shows how to create a viewer of BMP and PNG images. User can choose the folder and the file. It will be shown on the screen.

PROCEDURE Main()
LOCAL oWnd, oButton, oImg
oWnd := QMainWindow()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

59 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oWnd:setWindowTitle( "Giovanni" )
oWnd:resize( 200, 250 )
oImg := QLabel( oWnd )
oImg:move( 50, 50 )
oImg:resize( 100, 150 )
oImg:setStyleSheet( "border: 2px solid #0000ff;" )
oButton := QPushButton( oWnd )
oButton:setText( "Select a file" )
oButton:move( 10, 10 )
oButton:Connect( "clicked()", {|| viewImage( oImg ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE viewImage( oImg )
LOCAL oFileDialog, cListOfFiles, cFileName
oFileDialog := QFileDialog()
oFileDialog:exec()
cListOfFiles := oFileDialog:selectedFiles()
cFileName := cListOfFiles:At( 0 )
oImg:SetPixmap( QPixmap( cFileName ) )
RETURN

QCheckBox - Simple checkboxes


This example shows how to create checkboxes.

PROCEDURE Main()
LOCAL oWnd
LOCAL oCb1, oCb2
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oCb1 := QCheckBox ( oWnd )
oCb1:move( 20, 20 )
oCb1:setText( "First" )
oCb2 := QCheckBox ( oWnd )
oCb2:move( 20, 40 )
oCb2:setText( "Second" )
oWnd:show()
QApplication():exec()
RETURN

QCheckBox - Exclusive checkboxes


This example shows how to create exclusive checkboxes.

PROCEDURE Main()
LOCAL oWnd
LOCAL oCb1, oCb2
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oCb1 := QCheckBox ( oWnd )
oCb1:move( 20, 20 )
oCb1:setText( "First" )
oCb1:setAutoExclusive ( .T. )
oCb2 := QCheckBox ( oWnd )
oCb2:move( 20, 40 )
oCb2:setText( "Second" )
oCb2:setAutoExclusive ( .T. )
oWnd:show()
QApplication():exec()
RETURN

QCheckBox - How to set a checkbox


This example shows how to set a checkbox.

PROCEDURE Main()
LOCAL oWnd
LOCAL oCb1
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 300, 200 )
oCb1 := QCheckBox ( oWnd )
oCb1:move( 20, 20 )
oCb1:setText( "First" )
oCb1:setChecked ( .T. )
oWnd:show()
QApplication():exec()
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

60 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

QSAY, QGET, QREAD and friends - Input form


You can use new classes QSAY, QGET and QREAD to build an entry form in a similar-Clipper language. This forms accepts string data.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL cOne, cTwo, cThree, cFour
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
@ 02,
@ 03,
@ 04,
@ 05,
QREAD

05
05
05
05

Qsay
Qsay
Qsay
Qsay

"First: "
"Second:"
"Third: "
"Fourth:"

Qget
Qget
Qget
Qget

cOne
cTwo
cThree
cFour

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Input form


You can use new classes QSAY, QGET and QREAD to build an entry form in a similar-Clipper language. This forms accepts numerical and string data. The dimension of the form depends from the position of the controls.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL nWidth, nHeight, nColor
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
nWidth := 0
nHeight := 0
nColor := "Red

"

@ 02, 05 Qsay "Width: " Qget nWidth


@ 03, 05 Qsay "Heigth:" Qget nHeight
@ 06, 30 Qsay "Color: " Qget nColor
QREAD
QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Input form


You can use new classes QSAY, QGET and QREAD to build an entry form in a similar-Clipper language. This forms accepts numerical data.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL nWidth, nHeight
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
nWidth := 0
nHeight := 0
@ 02, 05 Qsay "Width: " Qget nWidth
@ 03, 05 Qsay "Heigth:" Qget nHeight
QREAD
QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Input form with PICTURE


You can use new classes QSAY, QGET and QREAD to build an entry form in a similar-Clipper language. This forms accepts numerical data. The GETs clauses use PICTURE mask.

#include "hbqtstd.ch"
FUNCTION Main()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

61 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

LOCAL nWidth, nHeight


LOCAL SAYLIST := {}
LOCAL GETLIST := {}
nWidth := 0
nHeight := 0
@ 02, 05 Qsay "Width: " Qget nWidth PICTURE "9999"
@ 03, 05 Qsay "Heigth:" Qget nHeight PICTURE "999"
QREAD
QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - How to change the title bar
You can change the title bar using the TITLE of QREAD.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL nWidth, nHeight
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
nWidth := 0
nHeight := 0
@ 02, 05 Qsay "Width: " Qget nWidth
@ 03, 05 Qsay "Heigth:" Qget nHeight
QREAD TITLE "Giovanni's Window"
QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - How to change the title bar
You can change the title bar using the PROPERTIES of QREAD.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL nWidth, nHeight, nColor
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
nWidth := 0
nHeight := 0
nColor := "Red
@ 02,
@ 03,
@ 04,
QREAD

"

05 Qsay "Width: " Qget nWidth


05 Qsay "Heigth:" Qget nHeight
05 Qsay "Color: " Qget nColor
PROPERTIES {| oWnd| oWnd:setWindowTitle( "Just a Test" ) }

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Colored GETs


You can change the color of the GET like Clipper language.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL cOne, cTwo, cThree, cFour
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
@ 02,
@ 03,
@ 04,
@ 05,
QREAD

05
05
05
05

Qsay
Qsay
Qsay
Qsay

"First: "
"Second:"
"Third: "
"Fourth:"

Qget
Qget
Qget
Qget

cOne COLOR "g+/b"


cTwo COLOR "b+/r"
cThree COLOR "w+/n"
cFour COLOR "n/w"

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Push Button


This code shows how to create a push button.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

62 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL
LOCAL
LOCAL
LOCAL

cOne, cTwo, cThree, cFour


lSave
SAYLIST := {}
GETLIST := {}

cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
lSave := .F.
@ 02,
@ 03,
@ 04,
@ 05,
@ 07,
QREAD

05 Qsay
05 Qsay
05 Qsay
05 Qsay
10, 08,

"First: " Qget cOne


"Second:" Qget cTwo
"Third: " Qget cThree
"Fourth:" Qget cFour
20 QGET lSave PUSHBUTTON "Save"

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Checkbox


This code shows how to create a Checkbox.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL lUpdated, lActive
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
lUpdated := .F.
lActive := .T.
@ 02, 05 QSAY "Updated?"
@ 02, 20 QGET lUpdated CHECKBOX
@ 04, 05 QSAY "Active?"
@ 04, 20 QGET lActive CHECKBOX
QREAD
QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Display a text


This code shows how to display a text, using SAY clause.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
@ 02, 05 QSAY "Text created by GIOVANNI DI MARIA"
QREAD
QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Valid


This code shows how to validate the input fields.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL nWidth, nHeight, nColor
LOCAL SAYLIST := {}
LOCAL GETLIST := {}

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

63 de 83

nWidth := 5
nHeight := 0
nColor := "Red

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

"

@ 02, 05 Qsay "Width: " Qget nWidth VALID nWidth >= 5 .AND. nWidth <= 10
@ 03, 05 Qsay "Heigth:" Qget nHeight
@ 06, 30 Qsay "Color: " Qget nColor
QREAD
QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - When


This code shows how to input a field for a specific condition.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL cOne, cTwo, cThree, cFour
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
@ 02,
@ 03,
@ 04,
@ 05,
QREAD

05
05
05
05

Qsay
Qsay
Qsay
Qsay

"First: "
"Second:"
"Third: "
"Fourth:"

Qget
Qget
Qget
Qget

cOne
cTwo WHEN AllTrim( Upper( cOne ) ) == "DI MARIA"
cThree
cFour

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Memoedit


This code shows how to edit a text string in a "memo" field.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL cOne, cTwo, cThree, cFour
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := "This tutorial is a brief of the use of the classes QT with Harbour language."
@ 01,
@ 02,
@ 03,
@ 05,
@ 05,
QREAD

05 Qsay
05 Qsay
05 Qsay
05 Qsay
15, 09,

"First: " Qget cOne


"Second:" Qget cTwo
"Third: " Qget cThree
"Fourth:"
50 Qget cFour MEMOEDIT

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Many colors


This code shows how to use many colors in the GET clause, with the function RGB.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL cOne, cTwo, cThree, cFour
LOCAL SAYLIST := {}
LOCAL GETLIST := {}
cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
@ 02,
@ 03,
@ 04,
@ 05,
QREAD

05
05
05
05

Qsay
Qsay
Qsay
Qsay

"First: "
"Second:"
"Third: "
"Fourth:"

Qget
Qget
Qget
Qget

cOne COLOR "w+/rgb(250,0,0)"


cTwo COLOR "w+/rgb(200,0,0)"
cThree COLOR "w+/rgb(150,0,0)"
cFour COLOR "w+/rgb(100,0,0)"

QApplication():exec()
RETURN NIL

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

64 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

QSAY, QGET, QREAD and friends - Listbox


This code shows how to implement a listbox as field.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL
LOCAL
LOCAL
LOCAL

cOne, cTwo, cThree, cFour


aList
SAYLIST := {}
GETLIST := {}

cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
aList
:= { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" }
@ 01,
@ 02,
@ 03,
@ 05,
@ 05,
QREAD

05 Qsay
05 Qsay
05 Qsay
05 Qsay
15, 11,

"First: " Qget cOne


"Second:" Qget cTwo
"Third: " Qget cThree
"Fourth:"
30 Qget cFour LISTBOX aList

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Combobox


This code shows how to implement a combobox as field.

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL
LOCAL
LOCAL
LOCAL

cOne, cTwo, cThree, cFour


aList
SAYLIST := {}
GETLIST := {}

cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
aList
:= { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" }
@ 01,
@ 02,
@ 03,
@ 05,
@ 05,
QREAD

05 Qsay "First: " Qget cOne


05 Qsay "Second:" Qget cTwo
05 Qsay "Third: " Qget cThree
05 Qsay "Fourth:"
15, 5, 30 Qget cFour COMBOBOX aList

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Setfocus


This code sets the focus to Fourth field if you press the button "Focus".

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL
LOCAL
LOCAL
LOCAL

cOne, cTwo, cThree, cFour


lSave
SAYLIST := {}
GETLIST := {}

cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
lSave := .F.
@
@
@
@

02,
03,
04,
05,

05
05
05
05

Qsay
Qsay
Qsay
Qsay

"First: "
"Second:"
"Third: "
"Fourth:"

Qget
Qget
Qget
Qget

cOne
cTwo
cThree
cFour

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

65 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

@ 07, 10, 08, 20 QGET lSave PUSHBUTTON "Focus" ACTION {|| GETLIST[ 4 ]:setFocus() }
QREAD
QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Changing a field


This code assigns the value "Giovanni" to Fourth field if you press the button "Assign".

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL
LOCAL
LOCAL
LOCAL

cOne, cTwo, cThree, cFour


lSave
SAYLIST := {}
GETLIST := {}

cOne := Space( 10 )
cTwo := Space( 10 )
cThree := Space( 10 )
cFour := Space( 10 )
lSave := .F.
@ 02,
@ 03,
@ 04,
@ 05,
@ 07,
QREAD

05 Qsay
05 Qsay
05 Qsay
05 Qsay
10, 08,

"First: " Qget cOne


"Second:" Qget cTwo
"Third: " Qget cThree
"Fourth:" Qget cFour
20 QGET lSave PUSHBUTTON "Assign" ACTION {|| GETLIST[ 4 ]:varPut( "Giovanni" ),GETLIST[ 4 ]:display() }

QApplication():exec()
RETURN NIL

QSAY, QGET, QREAD and friends - Area of rectangle (accessing directly to GETLIST array)
This code calculates the area of a rectangle when you press the button "Calculate".

#include "hbqtstd.ch"
FUNCTION Main()
LOCAL
LOCAL
LOCAL
LOCAL

nBase, nHeight, nArea


lButton
SAYLIST := {}
GETLIST := {}

nBase := 0
nHeight := 0
nArea := 0
@ 02, 05 Qsay "Base:
" Qget nBase
@ 03, 05 Qsay "Height: " Qget nHeight
@ 05, 05 Qsay "Area:
" Qget nArea
@ 07, 07, 08, 23 QGET lButton PUSHBUTTON "Calculate Area" ACTION {|| calculate( GETLIST ) }
QREAD
QApplication():exec()
RETURN NIL

FUNCTION calculate( GETLIST )


LOCAL nBase2, nHeight2, nArea2
nBase2 := GETLIST[ 1 ]:varGet()
nHeight2 := GETLIST[ 2 ]:varGet()
nArea2 := nBase2 * nHeight2
GETLIST[ 3 ]:varPut( nArea2 )
GETLIST[ 3 ]:display()
RETURN NIL

QSAY, QGET, QREAD and friends - Area of rectangle (accessing to GETLIST by "name" of variable)
This code calculates the area of a rectangle when you press the button "Calculate".

#include "hbqtstd.ch"
FUNCTION Main()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

66 de 83

LOCAL
LOCAL
LOCAL
LOCAL

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

nBase, nHeight, nArea


lButton
SAYLIST := {}
GETLIST := {}

nBase := 0
nHeight := 0
nArea := 0
@ 02, 05 Qsay "Base:
" Qget nBase
@ 03, 05 Qsay "Height: " Qget nHeight
@ 05, 05 Qsay "Area:
" Qget nArea
@ 07, 07, 08, 23 QGET lButton PUSHBUTTON "Calculate Area" ACTION {|| calculate( GETLIST ) }
QREAD
QApplication():exec()
RETURN NIL

FUNCTION calculate( GETLIST )


LOCAL nBase2, nHeight2, nArea2
LOCAL nIndexBase, nIndexHeight, nIndexArea
//
Searching the index of the variables
nIndexBase := AScan( GETLIST, {| a| a:name() == "nBase" } )
nIndexHeight := AScan( GETLIST, {| a| a:name() == "nHeight" } )
nIndexArea := AScan( GETLIST, {| a| a:name() == "nArea" } )
// Taking value from fiels of the form
nBase2 := GETLIST[ nIndexBase ]:varGet()
nHeight2 := GETLIST[ nIndexHeight ]:varGet()
// Calculating area
nArea2 := nBase2 * nHeight2
GETLIST[ nIndexArea ]:varPut( nArea2 )
GETLIST[ nIndexArea ]:display()
RETURN NIL

Games - Game of Colors


This is a game where the player must find the exact color randomly chosen by computer. The color is chosen by three slider of the RGB colors (red, green, blue). Player can get an hint from the computer. The maximum score is 765. Have
fun.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oButtonStart, oButtonCheck, oButtonHint
oLcd
oTextMan, oTextPC
oRed, oGreen, oBlu
nRndRed, nRndGreen, nRndBlu

oWnd := QMainWindow()
oWnd:resize( 400, 650 )
oWnd:setWindowTitle( "Giovanni" )
oButtonStart := QPushButton( oWnd )
oButtonStart:setText( "New Game" )
oButtonStart:move( 50, 30 )
oButtonStart:resize( 300, 50 )
oButtonStart:Connect( "clicked()", { || newgame( oLcd, oTextPC, oRed, oGreen, oBlu, @nRndRed, @nRndGreen, @nRndBlu ) } )
oTextPC := QLabel( oWnd )
oTextPC:setText( "Computer" )
oTextPC:move( 50, 80 )
oTextPC:resize( 300, 100 )
oTextPC:setFont( QFont( "Arial",46 ) )
oTextPC:setAlignment( Qt_AlignVCenter + Qt_AlignHCenter )
oTextMan := QLabel( oWnd )
oTextMan:setText( "You" )
oTextMan:move( 50, 160 )
oTextMan:resize( 300, 100 )
oTextMan:setFont( QFont( "Arial",46 ) )
oTextMan:setAlignment( Qt_AlignVCenter + Qt_AlignHCenter )
oRed := QSlider( oWnd )
oRed:resize( 40, 200 )
oRed:move( 120, 260 )
oRed:setMinimum( 0 )
oRed:setMaximum( 255 )
oRed:setSingleStep( 1 )
oRed:setPageStep( 10 )
oRed:setValue( 0 )
oRed:Connect( "valueChanged(int)", { || change_colors( oTextMan, oRed, oGreen, oBlu ) } )
oGreen := QSlider( oWnd )
oGreen:resize( 40, 200 )
oGreen:move( 180, 260 )
oGreen:setMinimum( 0 )
oGreen:setMaximum( 255 )
oGreen:setSingleStep( 1 )
oGreen:setPageStep( 10 )
oGreen:setValue( 0 )
oGreen:Connect( "valueChanged(int)", { || change_colors( oTextMan, oRed, oGreen, oBlu ) } )
oBlu := QSlider( oWnd )
oBlu:resize( 40, 200 )
oBlu:move( 240, 260 )
oBlu:setMinimum( 0 )
oBlu:setMaximum( 255 )
oBlu:setSingleStep( 1 )
oBlu:setPageStep( 10 )
oBlu:setValue( 0 )
oBlu:Connect( "valueChanged(int)", { || change_colors( oTextMan, oRed, oGreen, oBlu ) } )
oButtonCheck := QPushButton( oWnd )
oButtonCheck:setText( "Check your score (Max is 765)" )
oButtonCheck:move( 150, 500 )
oButtonCheck:resize( 200, 50 )
oButtonCheck:Connect( "clicked()", { || score( oLcd, oRed, oGreen, oBlu, nRndRed, nRndGreen, nRndBlu
oButtonHint := QPushButton( oWnd )
oButtonHint:setText( "Hint" )
oButtonHint:move( 50, 500 )
oButtonHint:resize( 90, 50 )
oButtonHint:Connect( "clicked()", { || hint( oRed, oGreen, oBlu, nRndRed, nRndGreen, nRndBlu

) } )

) } )

oLcd := QLCDNumber( oWnd )


oLcd:move( 150, 570 )
oLcd:resize( 200, 50 )
newgame( oLcd, oTextPC, oRed, oGreen, oBlu, @nRndRed, @nRndGreen, @nRndBlu )
oWnd:show()
QApplication():exec()

// First time

RETURN
PROCEDURE change_colors( oT , oR, oG, oB )
LOCAL oPalette

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

67 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oPalette := QPalette()
oPalette:SetColor( QPalette_WindowText, QColor( oR:value() ,oG:value() , oB:value() ) )
oT:setPalette( oPalette )
RETURN
PROCEDURE newgame( oLc, oTp , oR, oG, oB, nRndRed, nRndGreen, nRndBlu )
LOCAL oPalette
oR:setValue(
oG:setValue(
oB:setValue(
oLc:display(

0
0
0
0

)
)
)
)

nRndRed := hb_RandomInt( 0, 255 )


nRndGreen := hb_RandomInt( 0, 255 )
nRndBlu := hb_RandomInt( 0, 255 )
oPalette := QPalette()
oPalette:SetColor( QPalette_WindowText, QColor( nRndRed , nRndGreen , nRndBlu ) )
oTp:setPalette( oPalette )
RETURN
PROCEDURE score( oL , oR, oG, oB, nRndRed, nRndGreen, nRndBlu )
LOCAL nScore
nScore := Abs( nRndRed - oR:value() ) + Abs( nRndGreen - oG:value() ) + Abs( nRndBlu - oB:value() )
nScore := 765 - nScore
oL:display( nScore )
RETURN
PROCEDURE hint( oR, oG, oB, nRndRed, nRndGreen, nRndBlu

oR:setValue( nRndRed )
oG:setValue( nRndGreen )
oB:setValue( nRndBlu )
RETURN

Games - Game of the Dog and the Cat (with boxes)


This is a nice game where the player is a dog (the red square). The dog must take the cat (the blue square). Every time that the dog takes the cat, the score increments its value by 1. The dog is moved by pressing arrow keys. Have fun.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oDog, oCat
LOCAL oScore
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oWnd:connect( QEvent_KeyPress , { |k| keypressed( k, oDog, oCat, oScore ) } )
oCat := QLabel( oWnd )
oCat:resize( 20, 20 )
oCat:move( 0, 0 )
oCat:setStyleSheet( "background-color: #0000FF" )
oDog := QLabel( oWnd )
oDog:resize( 30, 30 )
oDog:move( 150, 250 )
oDog:setStyleSheet( "background-color: #FF0000" )
oScore := QLCDNumber( oWnd )
oScore:resize( 100, 40 )
oScore:move( 250, 10 )
oScore:display( 0 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE keypressed( x, oDog, oCat, oScore )
LOCAL nStep := 4
LOCAL nXdiff, nYdiff
//--------------------Keys---------------DO CASE
CASE x:key() == Qt_Key_Left
oDog:move( oDog:x() - nStep , oDog:y() )
CASE x:key() == Qt_Key_Right
oDog:move( oDog:x() + nStep , oDog:y() )
CASE x:key() == Qt_Key_Up
oDog:move( oDog:x() , oDog:y() - nStep )
CASE x:key() == Qt_Key_Down
oDog:move( oDog:x() , oDog:y() + nStep )
ENDCASE
//----------------Check for collision---------------nXdiff := Abs ( oDog:x() - oCat:x() )
nYdiff := Abs ( oDog:y() - oCat:y() )
IF nXdiff < 10 .AND. nYdiff < 10
oCat:move( HB_RandomInt( 0, 380 ), HB_RandomInt( 60, 280 ) )
oScore:display( oScore:value() + 1 )
ENDIF
RETURN

Games - Game of the Dog and the Cat (with images)


This is a nice game where the player is a dog. The dog must take the cat. Every time that the dog takes the cat, the score increments its value by 1. The dog and the cat are real bitmap images. The dog is moved by pressing arrow keys.
Have fun.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oDog, oCat
LOCAL oScore
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

68 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oWnd:resize( 400, 300 )


oWnd:connect( QEvent_KeyPress , { |k| keypressed( k, oDog, oCat, oScore ) } )
oCat := QLabel( oWnd )
oCat:resize( 20, 20 )
oCat:move( 0, 0 )
oCat:SetPixmap( QPixmap( "cat.bmp" ) )
oDog := QLabel( oWnd )
oDog:resize( 30, 30 )
oDog:move( 150, 250 )
oDog:SetPixmap( QPixmap( "dog.bmp" ) )
oScore := QLCDNumber( oWnd )
oScore:resize( 100, 40 )
oScore:move( 250, 10 )
oScore:display( 0 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE keypressed( x, oDog, oCat, oScore )
LOCAL nStep := 4
LOCAL nXdiff, nYdiff
//--------------------Keys---------------DO CASE
CASE x:key() == Qt_Key_Left
oDog:move( oDog:x() - nStep , oDog:y() )
CASE x:key() == Qt_Key_Right
oDog:move( oDog:x() + nStep , oDog:y() )
CASE x:key() == Qt_Key_Up
oDog:move( oDog:x() , oDog:y() - nStep )
CASE x:key() == Qt_Key_Down
oDog:move( oDog:x() , oDog:y() + nStep )
ENDCASE
//----------------Check for collision---------------nXdiff := Abs ( oDog:x() - oCat:x() )
nYdiff := Abs ( oDog:y() - oCat:y() )
IF nXdiff < 10 .AND. nYdiff < 10
oCat:move( HB_RandomInt( 0, 380 ), HB_RandomInt( 60, 280 ) )
oScore:display( oScore:value() + 1 )
ENDIF
RETURN

Games - Game of the Dog and the Cat (with images and sound)
This is a nice game where the player is a dog. The dog must take the cat. Every time that the dog takes the cat, the score increments its value by 1 and the cat meows. The dog and the cat are real bitmap images. The dog is moved by
pressing arrow keys. Have fun.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL oWnd
LOCAL oDog, oCat
LOCAL oScore
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oWnd:connect( QEvent_KeyPress , { |k| keypressed( k, oDog, oCat, oScore ) } )
oCat := QLabel( oWnd )
oCat:resize( 20, 20 )
oCat:move( 0, 0 )
oCat:SetPixmap( QPixmap( "cat.bmp" ) )
oDog := QLabel( oWnd )
oDog:resize( 30, 30 )
oDog:move( 150, 250 )
oDog:SetPixmap( QPixmap( "dog.bmp" ) )
oScore := QLCDNumber( oWnd )
oScore:resize( 100, 40 )
oScore:move( 250, 10 )
oScore:display( 0 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE keypressed( x, oDog, oCat, oScore )
LOCAL nStep := 4
LOCAL nXdiff, nYdiff
//--------------------Keys---------------DO CASE
CASE x:key() == Qt_Key_Left
oDog:move( oDog:x() - nStep , oDog:y() )
CASE x:key() == Qt_Key_Right
oDog:move( oDog:x() + nStep , oDog:y() )
CASE x:key() == Qt_Key_Up
oDog:move( oDog:x() , oDog:y() - nStep )
CASE x:key() == Qt_Key_Down
oDog:move( oDog:x() , oDog:y() + nStep )
ENDCASE
//----------------Check for collision---------------nXdiff := Abs ( oDog:x() - oCat:x() )
nYdiff := Abs ( oDog:y() - oCat:y() )
IF nXdiff < 10 .AND. nYdiff < 10
oCat:move( HB_RandomInt( 0, 380 ), HB_RandomInt( 60, 280 ) )
oScore:display( oScore:value() + 1 )
QSound( "cat.wav" ):play()
ENDIF
RETURN

Games - Dice Game (1 die)


This is a nice game where the player play with one die.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

69 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL oWnd
LOCAL oDice
LOCAL oButton
oWnd := QMainWindow()
oWnd:SetFixedSize( 200, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oButton := QPushButton( oWnd )
oButton:resize( 100, 50 )
oButton:move( 50, 200 )
oButton:setText( "Play now" )
oButton:Connect( "clicked()", { ||dice( oDice )

} )

oDice := QLabel( oWnd )


oDice:move( 50, 50 )
oDice:resize( 100, 100 )
dice( oDice )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE dice( oD )
LOCAL cFileName[6]
LOCAL nRandom
cFileName[1]
cFileName[2]
cFileName[3]
cFileName[4]
cFileName[5]
cFileName[6]

:=
:=
:=
:=
:=
:=

"dice1.png"
"dice2.png"
"dice3.png"
"dice4.png"
"dice5.png"
"dice6.png"

nRandom := HB_RandomInt( 1, 6 )
oD:SetPixmap( QPixmap( cFileName[nRandom] ) )
RETURN

Games - Dice Game (6 dice)


This is a nice game where the player play with six dice. The dice are stored in a vector.

PROCEDURE Main()
LOCAL oWnd
LOCAL aoDice[6], k
LOCAL oButton
oWnd := QMainWindow()
oWnd:SetFixedSize( 700, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oButton := QPushButton( oWnd )
oButton:resize( 400, 50 )
oButton:move( 150, 200 )
oButton:setText( "Play now" )
oButton:Connect( "clicked()", { ||dice( aoDice )

} )

FOR k := 1 TO 6
aoDice[k] := QLabel( oWnd )
aoDice[k]:move( k * 110 - 85, 50 )
aoDice[k]:resize( 100, 100 )
NEXT k
dice( aoDice )

// First time

oWnd:show()
QApplication():exec()
RETURN
PROCEDURE dice( oD )
LOCAL k
LOCAL cFileName[6]
LOCAL nRandom
cFileName[1]
cFileName[2]
cFileName[3]
cFileName[4]
cFileName[5]
cFileName[6]

:=
:=
:=
:=
:=
:=

"dice1.png"
"dice2.png"
"dice3.png"
"dice4.png"
"dice5.png"
"dice6.png"

FOR k := 1 TO 6
nRandom := hb_RandomInt( 1, 6 )
oD[k]:SetPixmap( QPixmap( cFileName[nRandom] ) )
NEXT k
RETURN

Games - McDonald's Game


This is a nice game where the player must take the sandwich, the coke and the potato chips. Player can choice the hero and he can set the speed of game and its duration.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

70 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

#include "hbqtgui.ch"
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC
STATIC

oWnd
nDirXGiuseppe, nDirYGiuseppe
nDirXPanino, nDirYPanino
nDirXPatatine, nDirYPatatine
nDirXCocaCola, nDirYCocaCola
oSuperEroe
oPanino, oPatatine, oCocaCola
oScore, oScoreTitolo
oClockPerSprites, oClockPerCronometro
oSfondo
oButtonStart, oButtonStop
oCronometro, oCronometroTitolo, nCronometro
oMinutiDelGioco, oMinutiDelGiocoTitolo
oLivelloDelGioco, oLivelloDelGiocoTitolo
oButtonPause
oGameOver
oWndPerClassifica, oButtonClassifica, oTableClassifica, oCella
oDialog, cNomeGiocatore
oComboSuperEroe,oComboSuperEroeTitolo

PROCEDURE Main()
LOCAL aDbArray
SET DATE italian
SET CENTURY ON
aDbArray := {}
AAdd( aDbArray,
AAdd( aDbArray,
AAdd( aDbArray,
AAdd( aDbArray,

{
{
{
{

"Data", "D", 10, 0 } )


"Giocatore", "C", 20, 0 } )
"Minuti", "N", 3, 0 } )
"Punti", "N", 5, 0 } )

IF ! File( "classifica.dbf" )
dbCreate( "classifica.dbf", aDbArray )
ENDIF
oWnd := QMAINWINDOW()
oWnd:setWindowTitle( "McDonald's Game" )
oWnd:resize( 800, 600 )
oWnd:connect( QEvent_KeyPress, {|k| keypressed( k ) } )
oSfondo := QLABEL( oWnd )
oSfondo:move( 0, 0 )
oSfondo:resize( 800, 600 )
oSfondo:SetPixmap( QPIXMAP( "background.png" ) )
oPanino := QLABEL( oWnd )
oPanino:move( hb_RandomInt( 200, 600 ), hb_RandomInt( 100, 400 ) )
oPanino:resize( 100, 53 )
oPanino:SetPixmap( QPIXMAP( "panino.png" ) )
oPatatine := QLABEL( oWnd )
oPatatine:move( hb_RandomInt( 200, 600 ), hb_RandomInt( 100, 400 ) )
oPatatine:resize( 100, 124 )
oPatatine:SetPixmap( QPIXMAP( "patatine.png" ) )
oCocaCola := QLABEL( oWnd )
oCocaCola:move( hb_RandomInt( 200, 600 ), hb_RandomInt( 100, 400 ) )
oCocaCola:resize( 100, 150 )
oCocaCola:SetPixmap( QPIXMAP( "cocacola.png" ) )
oSuperEroe := QLABEL( oWnd )
oSuperEroe:move( 150, 250 )
oSuperEroe:resize( 150, 204 )
oScore := QLCDNUMBER( oWnd )
oScore:move( 20, 20 )
oScore:resize( 100, 40 )
oScore:display( 0 )
oScore:setStyleSheet( "background-color : yellow; color: #FF0000;" )
oScoreTitolo := QLABEL( oWnd )
oScoreTitolo:move( 20, 2 )
oScoreTitolo:resize( 100, 18 )
oScoreTitolo:setText( "Punti" )
oScoreTitolo:setFont( QFONT( "Courier", 10, QFont_Bold )
)
oScoreTitolo:setStyleSheet( "color: #640000;" )
oClockPerSprites := QTIMER()
oClockPerSprites:Connect( "timeout()", {|| fOgniNmillisecondi() } )
oClockPerCronometro := QTIMER()
oClockPerCronometro:Connect( "timeout()", {|| fIncrementaCronometro() } )
oButtonStart := QPUSHBUTTON( oWnd )
oButtonStart:setText( "Start Game" )
oButtonStart:move( 140, 18 )
oButtonStart:resize( 100, 20 )
oButtonStart:Connect( "clicked()", {|| fStartGioco() } )
oButtonStop := QPUSHBUTTON( oWnd )
oButtonStop:setText( "Stop Game" )
oButtonStop:move( 140, 40 )
oButtonStop:resize( 100, 20 )
oButtonStop:setEnabled( .F. )
oButtonStop:Connect( "clicked()", {|| fStopGioco() } )
oCronometro := QLABEL( oWnd )
oCronometro:move( 260, 20 )
oCronometro:resize( 200, 40 )
oCronometro:setText( "00:00:00" )
oCronometro:setAlignment( Qt_AlignCenter )
oCronometro:setStyleSheet( "color: #000055; background-color : #D2FCFD; border: 1px solid #0000ff;" )
oCronometro:setFont( QFONT( "Courier", 20, QFont_Bold, .F. )
)
oCronometroTitolo := QLABEL( oWnd )
oCronometroTitolo:move( 260, 2 )
oCronometroTitolo:resize( 100, 18 )
oCronometroTitolo:setText( "Tempo" )
oCronometroTitolo:setFont( QFONT( "Courier", 10, QFont_Bold )
)
oCronometroTitolo:setStyleSheet( "color: #640000;" )
oMinutiDelGioco := QSPINBOX( oWnd )
oMinutiDelGioco:move( 480, 20 )
oMinutiDelGioco:resize( 50, 40 )
oMinutiDelGioco:setMinimum( 1 )
oMinutiDelGioco:setMaximum( 3 )
oMinutiDelGioco:setSingleStep( 1 )
oMinutiDelGioco:setValue( 1 )
oMinutiDelGioco:setEnabled( .T. )
oMinutiDelGiocoTitolo := QLABEL( oWnd )
oMinutiDelGiocoTitolo:move( 480, 2 )
oMinutiDelGiocoTitolo:resize( 100, 18 )
oMinutiDelGiocoTitolo:setText( "Minuti" )
oMinutiDelGiocoTitolo:setFont( QFONT( "Courier", 10, QFont_Bold )
oMinutiDelGiocoTitolo:setStyleSheet( "color: #640000;" )
oLivelloDelGioco := QSPINBOX( oWnd )
oLivelloDelGioco:move( 550, 20 )
oLivelloDelGioco:resize( 50, 40 )
oLivelloDelGioco:setMinimum( 1 )
oLivelloDelGioco:setMaximum( 10 )
oLivelloDelGioco:setSingleStep( 1 )
oLivelloDelGioco:setValue( 4 )
oLivelloDelGioco:setEnabled( .T. )
oLivelloDelGioco:setEnabled( .T. )
oLivelloDelGiocoTitolo := QLABEL( oWnd )
oLivelloDelGiocoTitolo:move( 550, 2 )
oLivelloDelGiocoTitolo:resize( 100, 18 )
oLivelloDelGiocoTitolo:setText( "Speed" )
oLivelloDelGiocoTitolo:setFont( QFONT( "Courier", 10, QFont_Bold )
oLivelloDelGiocoTitolo:setStyleSheet( "color: #640000;" )

oButtonPause := QPUSHBUTTON( oWnd )


oButtonPause:setText( "Pause" )
oButtonPause:move( 620, 18 )
oButtonPause:resize( 60, 40 )
oButtonPause:Connect( "clicked()", {|| fPauseGioco() } )
oButtonPause:setEnabled( .F. )
oButtonClassifica := QPUSHBUTTON( oWnd )
oButtonClassifica:setText( "Classifica" )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

71 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oButtonClassifica:move( 700, 18 )
oButtonClassifica:resize( 70, 40 )
oButtonClassifica:Connect( "clicked()", {|| fMostraClassifica() } )
oGameOver := QLABEL( oWnd )
oGameOver:move( 0, 100 )
oGameOver:resize( 800, 400 )
oGameOver:setText( "Game Over" )
oGameOver:setAlignment( Qt_AlignCenter )
oGameOver:setStyleSheet( "color: #AE00AE;" )
oGameOver:setFont( QFONT( "Courier", 80, QFont_Bold, .F.
oGameOver:setVisible( .F. )

oComboSuperEroe := QComboBox( oWnd )


oComboSuperEroe:move( 20, 80 )
oComboSuperEroe:resize( 100, 25 )
oComboSuperEroe:Connect( "currentIndexChanged(int)", { || fCambiaSuperEroe() } )
oComboSuperEroe:addItem( "Giuseppe" )
oComboSuperEroe:addItem( "Giulia" )
oComboSuperEroe:addItem( "Daniele" )
oComboSuperEroe:addItem( "Vittoria" )
oComboSuperEroe:addItem( "Francesco" )
oComboSuperEroe:addItem( "Emma" )
oComboSuperEroe:addItem( "Sofia" )
oComboSuperEroe:addItem( "Riccardo" )
oComboSuperEroeTitolo:= QLABEL( oWnd )
oComboSuperEroeTitolo:move( 20, 62 )
oComboSuperEroeTitolo:resize( 100, 18 )
oComboSuperEroeTitolo:setText( "SuperEroe" )
oComboSuperEroeTitolo:setFont( QFONT( "Courier", 10, QFont_Bold )
)
oComboSuperEroeTitolo:setStyleSheet( "color: #640000;" )
oWnd:show()
QAPPLICATION():exec()
RETURN

PROCEDURE keypressed( x )
DO CASE
CASE x:key() == Qt_Key_Left
nDirXGiuseppe := -oLivelloDelGioco:value()
nDirYGiuseppe := 0
CASE x:key() == Qt_Key_Right
nDirXGiuseppe := oLivelloDelGioco:value()
nDirYGiuseppe := 0
CASE x:key() == Qt_Key_Up
nDirXGiuseppe := 0
nDirYGiuseppe := -oLivelloDelGioco:value()
CASE x:key() == Qt_Key_Down
nDirXGiuseppe := 0
nDirYGiuseppe := oLivelloDelGioco:value()
ENDCASE
RETURN

PROCEDURE fOgniNmillisecondi()
fMuoviGiuseppe()
fMuoviOggetto( oPanino, @nDirXPanino, @nDirYPanino )
fMuoviOggetto( oPatatine, @nDirXPatatine, @nDirYPatatine )
fMuoviOggetto( oCocaCola, @nDirXCocaCola, @nDirYCocaCola )
fControllaCollisione ( oSuperEroe, oPanino )
fControllaCollisione ( oSuperEroe, oPatatine )
fControllaCollisione ( oSuperEroe, oCocaCola )
RETURN

PROCEDURE fMuoviGiuseppe()
oSuperEroe:move( oSuperEroe:x() + nDirXGiuseppe, oSuperEroe:y() + nDirYGiuseppe )
IF oSuperEroe:y() < ( 0 -( oSuperEroe:height() / 2 ) ) .OR. oSuperEroe:y() > ( 600 -( oSuperEroe:height() / 2 ) )
nDirYGiuseppe := nDirYGiuseppe * ( -1 )
ENDIF
IF oSuperEroe:x() < ( 0 -( oSuperEroe:width() / 2 ) ) .OR. oSuperEroe:x() > ( 800 -( oSuperEroe:width() / 2 ) )
nDirXGiuseppe := nDirXGiuseppe * ( -1 )
ENDIF
RETURN

PROCEDURE fMuoviOggetto( oO, nDirX, nDirY )


IF oO:x() <
nDirX :=
ENDIF
IF oO:x() >
nDirX :=
ENDIF

0
-nDirX

IF oO:y() <
nDirY :=
ENDIF
IF oO:y() >
nDirY :=
ENDIF

0
-nDirY

( 800 -oO:width() )
-nDirX

( 600 -oO:height() )
-nDirY

oO:move( oO:x() + nDirX, oO:y() + nDirY )


RETURN

PROCEDURE fControllaCollisione ( oO1, oO2 )


LOCAL nCentroX1, nCentroY1
LOCAL nCentroX2, nCentroY2
LOCAL nDiffX, nDiffY
nCentroX1 := ( oO1:x() + ( oO1:x() + oO1:width() ) ) / 2
nCentroY1 := ( oO1:y() + ( oO1:y() + oO1:height() ) ) / 2
nCentroX2 := ( oO2:x() + ( oO2:x() + oO2:width() ) ) / 2
nCentroY2 := ( oO2:y() + ( oO2:y() + oO2:height() ) ) / 2
nDiffX := Abs( nCentroX1 - nCentroX2 )
nDiffY := Abs( nCentroY1 - nCentroY2 )
IF nDiffX < 30 .AND. nDiffY < 30
oScore:display( oScore:value() + 1 )
oO2:move( hb_RandomInt( 200, 600 ), hb_RandomInt( 100, 400 ) )
fDislocaOggetti()
fImpostaDirezioneOggetti()
ENDIF
RETURN

PROCEDURE fControllaCollisionePericolosa ( oO1, oO2 )


LOCAL nCentroX1, nCentroY1
LOCAL nCentroX2, nCentroY2
LOCAL nDiffX, nDiffY
nCentroX1 := ( oO1:x() + ( oO1:x() + oO1:width() ) ) / 2
nCentroY1 := ( oO1:y() + ( oO1:y() + oO1:height() ) ) / 2
nCentroX2 := ( oO2:x() + ( oO2:x() + oO2:width() ) ) / 2
nCentroY2 := ( oO2:y() + ( oO2:y() + oO2:height() ) ) / 2
nDiffX := Abs( nCentroX1 - nCentroX2 )
nDiffY := Abs( nCentroY1 - nCentroY2 )
IF nDiffX < 30 .AND. nDiffY < 30
oScore:display( 0 )
oO2:move( hb_RandomInt( 200, 600 ), hb_RandomInt( 100, 400 ) )
fDislocaOggetti()
fImpostaDirezioneOggetti()
ENDIF
RETURN

FUNCTION fnCasualeNoZero( nMin, nMax )


LOCAL nCaso
nCaso := hb_RandomInt( nMin, nMax )
IF nCaso == 0
nCaso := 1
ENDIF
RETURN nCaso

PROCEDURE fImpostaDirezioneOggetti()
nDirXPanino := fnCasualeNoZero( -2, 2
nDirYPanino := fnCasualeNoZero( -2, 2
nDirXPatatine := fnCasualeNoZero( -2,
nDirYPatatine := fnCasualeNoZero( -2,
nDirXCocaCola := fnCasualeNoZero( -2,
nDirYCocaCola := fnCasualeNoZero( -2,

)
)
2
2
2
2

)
)
)
)

RETURN

PROCEDURE fDislocaOggetti()
oPanino:move( hb_RandomInt( 200, 600 ), hb_RandomInt( 100, 400 ) )
oPatatine:move( hb_RandomInt( 200, 600 ), hb_RandomInt( 100, 400 ) )
oCocaCola:move( hb_RandomInt( 200, 600 ), hb_RandomInt( 100, 400 ) )
RETURN

PROCEDURE fStartGioco()
oSuperEroe:setFocus()
oClockPerSprites:start( 10 )
oClockPerCronometro:start( 1000 )
fDislocaOggetti()
fImpostaDirezioneOggetti()
nCronometro := 0

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

72 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oCronometro:setText( "00:00:00" )
oScore:display( 0 )
nDirXGiuseppe := oLivelloDelGioco:value()
nDirYGiuseppe := 0
oGameOver:setVisible( .F. )
oButtonStart:setEnabled( .F. )
oButtonStop:setEnabled( .T. )
oButtonPause:setEnabled( .T. )
oMinutiDelGioco:setEnabled( .F. )
oLivelloDelGioco:setEnabled( .F. )
oComboSuperEroe:setEnabled( .F. )
RETURN

PROCEDURE fStopGioco()
oClockPerSprites:stop()
oClockPerCronometro:stop()
oGameOver:setVisible( .T. )
oButtonStart:setEnabled( .T. )
oButtonStop:setEnabled( .F. )
oButtonPause:setEnabled( .F. )
oMinutiDelGioco:setEnabled( .T. )
oLivelloDelGioco:setEnabled( .T. )
oComboSuperEroe:setEnabled( .T. )
RETURN

PROCEDURE fPauseGioco()
IF oClockPerSprites:isActive()
oClockPerSprites:stop()
oClockPerCronometro:stop()
oButtonPause:setText( "Continua" )
oComboSuperEroe:setEnabled( .T. )
ELSE
oClockPerSprites:start( 10 )
oClockPerCronometro:start( 1000 )
oButtonPause:setText( "Pause" )
oComboSuperEroe:setEnabled( .F. )
ENDIF
RETURN

PROCEDURE fIncrementaCronometro()
LOCAL nMinuti
oSuperEroe:setFocus()
nCronometro++
oCronometro:setText( SECTOTIME( nCronometro ) )
nMinuti := nCronometro / 60
IF nMinuti >= oMinutiDelGioco:value()
oClockPerSprites:stop()
oClockPerCronometro:stop()
oGameOver:setVisible( .T. )
oButtonStart:setEnabled( .T. )
oButtonStop:setEnabled( .F. )
oButtonPause:setEnabled( .F. )
oMinutiDelGioco:setEnabled( .T. )
oLivelloDelGioco:setEnabled( .T. )
oComboSuperEroe:setEnabled( .T. )
oDialog := QINPUTDIALOG()
cNomeGiocatore := oDialog:getText( oWnd, "Classifica", "Inserisci il tuo nome" )
USE classifica
APPEND BLANK
REPLACE field->DATA WITH Date()
REPLACE field->Giocatore WITH cNomeGiocatore
REPLACE field->Minuti WITH oMinutiDelGioco:value()
REPLACE field->Punti WITH oScore:value()
USE
fMostraClassifica()
ENDIF
RETURN

PROCEDURE fMostraClassifica()
LOCAL nRecords, k
LOCAL oIntestazione
oWndPerClassifica := QMAINWINDOW()
oWndPerClassifica:setWindowTitle( "Classifica" )
oWndPerClassifica:resize( 600, 400 )
USE classifica
nRecords = RecCount()
oIntestazione := QSTRINGLIST()
oIntestazione:append( "Data" )
oIntestazione:append( "Giocatore" )
oIntestazione:append( "Minuti" )
oIntestazione:append( "Punti" )
oTableClassifica := QTABLEWIDGET( oWndPerClassifica )
oTableClassifica:move( 20, 20 )
oTableClassifica:resize( 560, 360 )
oTableClassifica:setRowCount( nRecords )
oTableClassifica:setColumnCount( 4 )
oTableClassifica:setHorizontalHeaderLabels( oIntestazione )
INDEX ON StrZero( field->minuti, 6 ) + Descend( StrZero(
GO TOP
FOR k = 1 TO nRecords
oTableClassifica:setItem( k - 1, 0, QTABLEWIDGETITEM(
oTableClassifica:setItem( k - 1, 1, QTABLEWIDGETITEM(
oTableClassifica:setItem( k - 1, 2, QTABLEWIDGETITEM(
oTableClassifica:setItem( k - 1, 3, QTABLEWIDGETITEM(
IF field->punti == oScore:value() .AND. field->minuti
oTableClassifica:selectRow( k - 1 )
ENDIF
SKIP
NEXT k
USE

field->punti, 6 ) ) TO temp

DToC( field->data ) ) )
field->giocatore ) )
Str( field->minuti ) ) )
Str( field->punti ) ) )
== oMinutiDelGioco:value() .AND. AllTrim( field->giocatore ) == cNomeGiocatore .AND. field->data == Date()

oWndPerClassifica:show()
RETURN

procedure fCambiaSuperEroe()
local cNomeFoto
cNomeFoto:=oComboSuperEroe:currentText() + ".png"
oSuperEroe:SetPixmap( QPIXMAP( cNomeFoto ) )
return

Sample Applications - Difference between two dates


The following example calculates the difference betwee two dates. The user must type two dates and then press the Calculate button. The program shows the difference, in days.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oText
oButtonCalculate
oEdit1, oEdit2, oEdit3

SET DATE ITALIAN


oWnd := QMainWindow()
oWnd:resize( 400, 300 )
oWnd:setWindowTitle( "Giovanni" )
oText := QLabel( oWnd )
oText:setText( "Difference between two dates" )
oText:move( 130, 20 )
oText:resize( 171, 16 )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

73 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oEdit1 := QLineEdit( oWnd )


oEdit1:resize( 113, 20 )
oEdit1:move( 140, 100 )
oEdit2 := QLineEdit( oWnd )
oEdit2:resize( 113, 20 )
oEdit2:move( 140, 130 )
oEdit3 := QLineEdit( oWnd )
oEdit3:resize( 113, 20 )
oEdit3:move( 140, 180 )
oButtonCalculate := QPushButton( oWnd )
oButtonCalculate:resize( 75, 23 )
oButtonCalculate:move( 270, 180 )
oButtonCalculate:setText( "Calculate" )
oButtonCalculate:Connect( "clicked()", { || calculate( oEdit1, oEdit2, oEdit3 ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE calculate( oE1, oE2, oE3 )
LOCAL nDifference
nDifference := Abs( CToD( oE1:text() ) - CToD( oE2:text() ) )
oE3:setText( AllTrim( Str(nDifference ) ) )
RETURN

Sample Applications - Radio Simulation (Visual only)


The following example simulates a simple radio receiver. It's visual only, of course. I use labels to draw the window tuning and the cursor of sintony.

#include "hbqtgui.ch"
PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oTuning
oWheelVolume
oPalette
oLabelTuning, oLabelVolume
oDisplayVolume
oDisplayTuning, oWheelTuning, oCursor

oPalette := QPalette()
oPalette:SetColor( QPalette_Window, QColor( 210,221,236 ) )
oWnd := QMainWindow()
oWnd:resize( 500, 300 )
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:setPalette( oPalette )

// ---------------TUNING PANEL-------------oTuning := QLabel( oWnd )


oTuning:resize( 460, 100 )
oTuning:move( 20, 20 )
oTuning:setStyleSheet( "background-color: #333333; color: #FFFFFF; border: 1px solid #00FF00;" )
oTuning:setText( "88 - - 92 - - 96 - - 100 - - 104 - - 108" )
oTuning:setAlignment( Qt_AlignHCenter + Qt_AlignVCenter )
oTuning:setFont( QFont( "Arial",16 ) )
// ---------------TUNING-------------oWheelTuning := QDial( oWnd )
oWheelTuning:move( 320, 140 )
oWheelTuning:resize( 150, 150 )
oWheelTuning:setMinimum( 88 )
oWheelTuning:setMaximum( 108 )
oWheelTuning:setSingleStep( 1 )
oWheelTuning:setWrapping( .F. )
oWheelTuning:Connect( "valueChanged(int)", { ||tuning_change( oDisplayTuning, oWheelTuning, oCursor ) } )
oLabelTuning := QLabel( oWnd )
oLabelTuning:resize( 46, 13 )
oLabelTuning:move( 380, 130 )
oLabelTuning:setPalette( oPalette )
oLabelTuning:setText( "Tuning" )
oDisplayTuning := QLCDNumber( oWnd )
oDisplayTuning:resize( 111, 41 )
oDisplayTuning:move( 230, 130 )
oDisplayTuning:display( 88 )
// ---------------VOLUME-------------oWheelVolume := QDial( oWnd )
oWheelVolume:move( 39, 186 )
oWheelVolume:resize( 100, 100 )
oWheelVolume:setMinimum( 0 )
oWheelVolume:setMaximum( 10 )
oWheelVolume:setSingleStep( 1 )
oWheelVolume:setWrapping( .F. )
oWheelVolume:Connect( "valueChanged(int)", { || oDisplayVolume:display( oWheelVolume:value() ) } )
oLabelVolume := QLabel( oWnd )
oLabelVolume:resize( 46, 13 )
oLabelVolume:move( 74, 178 )
oLabelVolume:setPalette( oPalette )
oLabelVolume:setText( "volume" )
oDisplayVolume := QLCDNumber( oWnd )
oDisplayVolume:resize( 101, 31 )
oDisplayVolume:move( 40, 144 )

// ---------------CURSOR-------------oCursor := QLabel( oWnd )


oCursor:resize( 4, 98 )
oCursor:move( 45, 21 )
oCursor:setStyleSheet( "background-color: #FF0000" )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE tuning_change( oD, oW, oC )
LOCAL oPosition
oPosition := oW:value() * 20 - 1715
oD:display( oW:value() )
oC:move( oPosition, 21 )
RETURN

Sample Applications - Area and perimeter of a rectangle


The following example calculates the area and the perimeter of a rectangle. Input data are typed by user.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

74 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oRectangle
oButton
oWidth, oHeigth
oArea, oPerimeter

oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oRectangle := QLabel( oWnd )
oRectangle:resize( 200, 100 )
oRectangle:move( 50, 50 )
oRectangle:setStyleSheet( "background-color : red;" )
oWidth := QLineEdit( oWnd )
oWidth:move( 100, 20 )
oWidth:resize( 100, 20 )
oHeigth := QLineEdit( oWnd )
oHeigth:move( 260, 90 )
oHeigth:resize( 100, 20 )
oButton := QPushButton( oWnd )
oButton:move( 50, 180 )
oButton:resize( 200, 30 )
oButton:setText( "Calculate area and perimeter" )
oButton:connect( "clicked()", { || formulas( oWidth, oHeigth, oArea, oPerimeter ) } )
oArea := QLineEdit( oWnd )
oArea:move( 50, 220 )
oArea:resize( 100, 20 )
oPerimeter := QLineEdit( oWnd )
oPerimeter:move( 50, 250 )
oPerimeter:resize( 100, 20 )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE formulas( oWi, oHe, oAr, oPe )
LOCAL nArea, nPerimeter
nArea := Val( oWi:text() ) * Val( oHe:text() )
nPerimeter := ( Val( oWi:text() ) + Val( oHe:text() ) ) * 2
oAr:setText( AllTrim( Str(nArea ) ) )
oPe:setText( AllTrim( Str(nPerimeter ) ) )
RETURN

Sample Applications - Alarm


The following example shows how to create a simple programmable alarm.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oClock
oText, oText2
oSetting

oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 250, 150 )
oText := QLabel( oWnd )
oText:setText( "clocking..." )
oText:move( 100, 100 )
oSetting := QTimeEdit( oWnd )
oSetting:move( 100, 50 )
oText2 := QLabel( oWnd )
oText2:setText( "Alarm at" )
oText2:move( 40, 50 )
oClock := QTimer()
oClock:Connect( "timeout()", { || tick( oText, oSetting ) } )
oClock:start( 1000 )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN
PROCEDURE tick( oT, oS )
LOCAL oSetTime, nSetHou, nSetMin, nSetSec
LOCAL cCurTime, nCurHou, nCurMin, nCurSec
LOCAL oBox
oT:setText( Time() )
oSetTime := oS:Time()
nSetHou := oSetTime:hour()
nSetMin := oSetTime:minute()
nSetSec := oSetTime:second()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

75 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

cCurTime := Time()
nCurHou := Val( Left( cCurTime,2 ) )
nCurMin := Val( SubStr( cCurTime,4,2 ) )
nCurSec := Val( Right( cCurTime,2 ) )
IF nSetHou == nCurHou .AND. nSetMin == nCurMin .AND. nSetSec == nCurSec
oBox := QMessageBox()
oBox:setWindowTitle( "Alarm" )
oBox:setInformativeText( "It's " + cCurTime + " o'clock" )
oBox:exec()
ENDIF
RETURN

Sample Applications - Resistors


The following example shows a program to calculate the value of a resistor. It show also the colors of its bars.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oTerminals, oBody
oBand1, oBand2, oBand3
oChoose1, oChoose2, oChoose3
oValue

oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
//----- Draw Terminals------------oTerminals := QLabel( oWnd )
oTerminals:resize( 360, 10 )
oTerminals:move( 20, 50 )
oTerminals:setStyleSheet( "background-color : #C8C8C8;" )
//----- Draw body of resistance ------------oBody := QLabel( oWnd )
oBody:resize( 200, 60 )
oBody:move( 100, 25 )
oBody:setStyleSheet( "background-color : #DCCCBA;" )
//----- Draw BAND 1------------oBand1 := QLabel( oWnd )
oBand1:resize( 20, 60 )
oBand1:move( 120, 25 )
oBand1:setStyleSheet( "background-color : #000000;" )
oChoose1 := QSpinBox( oWnd )
oChoose1:move( 120, 100 )
oChoose1:resize( 42, 20 )
oChoose1:setMinimum( 0 )
oChoose1:setMaximum( 9 )
oChoose1:Connect( "valueChanged(int)", { ||resistor( oChoose1, oChoose2, oChoose3, oBand1, oBand2, oBand3, oValue ) } )
//----- Draw BAND 2------------oBand2 := QLabel( oWnd )
oBand2:resize( 20, 60 )
oBand2:move( 170, 25 )
oBand2:setStyleSheet( "background-color : #000000;" )
oChoose2 := QSpinBox( oWnd )
oChoose2:move( 170, 100 )
oChoose2:resize( 42, 20 )
oChoose2:setMinimum( 0 )
oChoose2:setMaximum( 9 )
oChoose2:Connect( "valueChanged(int)", { ||resistor( oChoose1, oChoose2, oChoose3, oBand1, oBand2, oBand3, oValue ) } )
//----- Draw BAND 3------------oBand3 := QLabel( oWnd )
oBand3:resize( 20, 60 )
oBand3:move( 220, 25 )
oBand3:setStyleSheet( "background-color : #000000;" )
oChoose3 := QSpinBox( oWnd )
oChoose3:move( 220, 100 )
oChoose3:resize( 42, 20 )
oChoose3:setMinimum( 0 )
oChoose3:setMaximum( 9 )
oChoose3:Connect( "valueChanged(int)", { ||resistor( oChoose1, oChoose2, oChoose3, oBand1, oBand2, oBand3, oValue ) } )
//----- Resistor value------------oValue := QLabel( oWnd )
oValue:resize( 350, 50 )
oValue:move( 25, 190 )
oValue:setStyleSheet( "background-color : #FFFFBB;" )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE resistor( oChoose1, oChoose2, oChoose3, oBand1, oBand2, oBand3, oValue )
LOCAL cColors[10]
LOCAL nC1, nC2, nC3
LOCAL v
cColors[1]
cColors[2]
cColors[3]
cColors[4]
cColors[5]
cColors[6]
cColors[7]
cColors[8]
cColors[9]
cColors[10]

:=
:=
:=
:=
:=
:=
:=
:=
:=
:=

"#000000"
"#836543"
"#FF0000"
"#FF9146"
"#FFFF00"
"#00FF00"
"#0000FF"
"#BF00BF"
"#C0C0C0"
"#FFFFFF"

nC1 := oChoose1:value()
nC2 := oChoose2:value()
nC3 := oChoose3:value()
oBand1:setStyleSheet( "background-color : " + cColors[nC1+1] + ";" )
oBand2:setStyleSheet( "background-color : " + cColors[nC2+1] + ";" )
oBand3:setStyleSheet( "background-color : " + cColors[nC3+1] + ";" )
v
v
v
v
v

:=
:=
:=
:=
:=

Val( AllTrim( Str(nC1 ) ) + AllTrim( Str(nC2 ) ) )


Int( v * 10 ^ nC3 )
AllTrim( Str( v ) ) + " Ohms"
"<font color=#000077 size=6>" + v + "</font>"
"<center>" + v + "</center>"

oValue:setText( v )
RETURN

Sample Applications - Aquarius


The following example shows how to create a virtual aquarius with fishes. The fishes move from side to side. The fishes are in .PNG format image, with transparency.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

76 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oSea
oClock
oFish1, oFish2, oFish3, oFish4, oFish5

oWnd := QMainWindow()
oWnd:SetFixedSize( 600, 400 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oClock := QTimer()
oClock:Connect( "timeout()", { || moveFish( oFish1, oFish2, oFish3, oFish4, oFish5 ) } )
oClock:start( 50 )
oSea := QLabel( oWnd )
oSea:move( 0, 0 )
oSea:resize( 600, 400 )
oSea:SetPixmap( QPixmap( "aquarium1.bmp" ) )
oFish1 := QLabel( oWnd )
oFish1:move( 300, 110 )
oFish1:resize( 180, 180 )
oFish1:SetPixmap( QPixmap( "fish1.png" ) )
oFish2 := QLabel( oWnd )
oFish2:move( 200, 40 )
oFish2:resize( 150, 150 )
oFish2:SetPixmap( QPixmap( "fish2.png" ) )
oFish3 := QLabel( oWnd )
oFish3:move( 100, 320 )
oFish3:resize( 100, 50 )
oFish3:SetPixmap( QPixmap( "fish3.png" ) )
oFish4 := QLabel( oWnd )
oFish4:move( 300, 250 )
oFish4:resize( 150, 70 )
oFish4:SetPixmap( QPixmap( "fish4.png" ) )
oFish5 := QLabel( oWnd )
oFish5:move( 50, 10 )
oFish5:resize( 150, 70 )
oFish5:SetPixmap( QPixmap( "fish5.png" ) )
oWnd:show()
QApplication():exec()
oClock:stop()
RETURN
PROCEDURE moveFish( oF1, oF2, oF3, oF4, oF5 )
oF1:move( oF1:x - 1, oF1:y )
IF oF1:x <- 180
oF1:move( 600, oF1:y )
end IF
oF2:move( oF2:x - 1.5, oF2:y )
IF oF2:x <- 150
oF2:move( 600, oF2:y )
end IF
oF3:move( oF3:x - 2, oF3:y )
IF oF3:x <- 100
oF3:move( 600, oF3:y )
end IF
oF4:move( oF4:x + 1.3 , oF4:y )
IF oF4:x > 600
oF4:move( - 150, oF4:y )
end IF
oF5:move( oF5:x + 1.5 , oF5:y )
IF oF5:x > 600
oF5:move( - 150, oF5:y )
end IF
RETURN

Sample Applications - Semaphore (automatic)


The following example shows how to create an automatic semaphore. It changes its color every a second.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oImg
oClock
nSequence

oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 250, 450 )
oImg := QLabel( oWnd )
oImg:move( 25, 25 )
oImg:resize( 200, 400 )
nSequence := 1
semaphore( oImg, @nSequence )

// First time

oClock := QTimer()
oClock:Connect( "timeout()", { || semaphore( oImg, @nSequence ) } )
oClock:start( 1000 )
oWnd:show()
QApplication():exec()
oClock:stop()

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

77 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

RETURN
PROCEDURE semaphore( oI, nSeq )
DO CASE
CASE nSeq == 1
oI:SetPixmap( QPixmap( "semaforo1.png" ) )
CASE nSeq == 2
oI:SetPixmap( QPixmap( "semaforo2.png" ) )
CASE nSeq == 3
oI:SetPixmap( QPixmap( "semaforo3.png" ) )
ENDCASE
nSeq ++
IF nSeq == 4
nSeq := 1
ENDIF
RETURN

Sample Applications - Semaphore (manual)


The following example shows how to create a manual semaphore. It changes its color if the button is pressed.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oImg
oButton
nSequence

oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 250, 500 )
oImg := QLabel( oWnd )
oImg:move( 25, 25 )
oImg:resize( 200, 400 )
nSequence := 1
semaphore( oImg, @nSequence )

// First time

oButton := QPushButton ( oWnd )


oButton:move( 50, 430 )
oButton:resize( 150, 30 )
oButton:setText( "Press to change color" )
oButton:Connect( "clicked()", { || semaphore( oImg, @nSequence ) } )

oWnd:show()
QApplication():exec()
RETURN
PROCEDURE semaphore( oI , nSeq )
DO CASE
CASE nSeq == 1
oI:SetPixmap( QPixmap( "semaforo1.png" ) )
CASE nSeq == 2
oI:SetPixmap( QPixmap( "semaforo2.png" ) )
CASE nSeq == 3
oI:SetPixmap( QPixmap( "semaforo3.png" ) )
ENDCASE
nSeq ++
IF nSeq == 4
nSeq := 1
ENDIF
RETURN

Sample Applications - Scrolling titles


The following example shows how to create scrolling titles, like movie titles. Them scroll up and shows some image, too.

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
OWinBg
oClock
nVTitleSpace, nSpeed, sTxt
oStr1, oStr2, oStr3
oImg1, oImg2

nVTitleSpace := 300
nSpeed := 2
oWnd := QMainWindow()
oWnd:SetFixedSize( 600, 400 )
oWnd:setWindowTitle( "The end" )
oClock := QTimer()
oClock:Connect( "timeout()", { || moveTitle( oClock, nSpeed, oStr1, oStr2, oStr3, oImg1, oImg2 ) } )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

78 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oClock:start( 50 )
OWinBg := QLabel( oWnd )
OWinBg:move( 0, 0 )
OWinBg:resize( 600, 400 )
OWinBg:SetPixmap( QPixmap( "bra01-background.png" ) )
oImg1 := QLabel( oWnd )
oImg1:move( 10, nVTitleSpace )
oImg1:resize( 300, 300 )
oImg1:SetPixmap( QPixmap( "bra01-arbour.png" ) )
oImg2 := QLabel( oWnd )
oImg2:move( 310, nVTitleSpace )
oImg2:resize( 600, 300 )
oImg2:SetPixmap( QPixmap( "bra01-logo_qt.png" ) )
oStr1 := QLabel( oWnd )
oStr1:move( 10, nVTitleSpace )
oStr1:resize( 550, 60 )
oStr1:SetText( "<center><h3>Harbour and Qt</h3></center><br>" )
oStr1:setStylesheet( "background: gray; border: 2px dotted white; border-radius: 20px; background: transparent; font-size: 35px; color: white;" )
sTxt
sTxt
sTxt
sTxt

:=
+=
+=
+=

"To see the code of this image and test scrolling little demo,<br>"
"please visit the tutorial site... there are a lot of other stuffs...<br>"
"<br>"
"http://www.gruppoeratostene.com/harbour/harbour-tutorials.htm<br>"

oStr2 := QLabel( oWnd )


oStr2:move( 10, nVTitleSpace * 2 )
oStr2:resize( 550, 140 )
oStr2:SetText( sTxt )
oStr2:setStylesheet( "font-size: 14px; color: white;" )
oStr3 := QLabel( oWnd )
oStr3:move( 10, nVTitleSpace * 3
oStr3:resize( 550, 140 )
oStr3:SetText( "<center> <h1>The
"www.harbour-project.org <br>
"</center>" )
oStr3:setStylesheet( "font-size:

)
End</h1><br><br>Special thank to Harbour and Qt<br>" + ;
qt.nokia.com" + ;
15px; color: white;" )

oWnd:show()
QApplication():exec()
oClock:stop()
RETURN
PROCEDURE moveTitle( oC, nS, oS1, oS2, oS3, oI1, oI2 )
oI1:move(
oI2:move(
oS1:move(
oS2:move(

oI1:x
oI2:x
oS1:x
oS2:x

,
,
,
,

oI1:y - nS )
oI2:y - nS )
oS1:y - nS )
oS2:y - nS )

IF oS3:y < 120


oC:stop()
ENDIF
oS3:move( oS3:x, oS3:y - nS )
RETURN

Sample Applications - Televideo Teletext


The following example shows the page 101 of Italian Teletext Service (Televideo). This page shows last news in Italy and in the World. The program searches for a new page every 30 seconds.

PROCEDURE Main()
LOCAL oWnd, oImg, oClock, nColpi
nColpi := 29
oWnd := QMainWindow()
oWnd:SetFixedSize( 700, 500 )
oWnd:setStyleSheet( " background-color: #CCCCFF; " )
oWnd:setWindowTitle( "Giovanni" )
oImg := QLabel( oWnd )
oImg:move( 28, 50 )
oImg:resize( 644, 400 )
oClock := QTimer()
oClock:Connect( "timeout()", {|| pUpdate( oWnd, oImg, @nColpi ) } )
oClock:start( 1000 )
pUpdate( oWnd, oImg, @nColpi )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE pUpdate( oWnd, oImg, nColpi )
LOCAL oHttp, cString
oWnd:setWindowTitle( Time() )
nColpi++
IF nColpi == 30
nColpi := 0
oHttp := TIPClientHTTP():new( "http://www.televideo.rai.it/televideo/pub/tt4web/Nazionale/16_9_page-101.png" )
oHttp:open()
cString := oHttp:readAll()
oHttp:close()
hb_MemoWrit( "televideo.png", cString )
oImg:SetPixmap( QPixmap( "televideo.png" ) )
ENDIF
RETURN

Sample Applications with Databases - DBF Database Append Record


The following example shows how to store data into a DBF database. Pressing the Save button, the data are saved into a DBF, thank to REPLACE command.

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

79 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE Main()
LOCAL
LOCAL
LOCAL
LOCAL
LOCAL

oWnd
oName, oLabel1
oBorn, oLabel2
oSons, oLabel3
oButton

SET DATE ITALIAN


SET CENTURY ON
oWnd := QMainWindow()
oWnd:setWindowTitle( "Finestra di Giovanni" )
oWnd:resize( 400, 300 )
oLabel1 := QLabel( oWnd )
oLabel1:setText( "Name" )
oLabel1:move( 50, 50 )
oLabel1:resize( 100, 20 )
oName := QLineEdit( oWnd )
oName:move( 160, 50 )
oName:resize( 150, 20 )
oLabel2 := QLabel( oWnd )
oLabel2:setText( "Date of birth " )
oLabel2:move( 50, 80 )
oLabel2:resize( 100, 20 )
oBorn := QLineEdit( oWnd )
oBorn:move( 160, 80 )
oBorn:resize( 90, 20 )
oLabel3 := QLabel( oWnd )
oLabel3:setText( "Number of sons" )
oLabel3:move( 50, 110 )
oLabel3:resize( 100, 20 )
oSons := QLineEdit( oWnd )
oSons:move( 160, 110 )
oSons:resize( 50, 20 )
oButton := QPushButton( oWnd )
oButton:setText( "Save" )
oButton:move( 150, 200 )
oButton:Connect( "clicked()", { || store( oName,oBorn,oSons ) } )
oWnd:show()
QApplication():exec()
RETURN
PROCEDURE STORE( oN, oB, oS )
USE sample
APPEND BLANK
REPLACE sample -> cognome
REPLACE sample -> nascita
REPLACE sample -> figli
USE

WITH oN:text()
WITH CToD( oB:text() )
WITH Val( oS:text() )

RETURN

Sample Applications with Databases - DBEdit simulation


The following example shows how to simulate the DbEdit function to browse a DBF. The fields and records cannot be changed but only viewed.

PROCEDURE Main()
//------------Declaration-------------LOCAL oWnd
LOCAL oCell, valore
LOCAL nNumField, nNumRecord, oLabels
LOCAL nX, nY
LOCAL button_top, button_bottom
LOCAL oTable
oWnd := QMainWindow()
oWnd:resize( 800, 600 )
oWnd:setWindowTitle( "DbEdit simulation" )
//------------Open DBF and count field and record-----------USE sample
nNumRecord := RecCount()
nNumField := FCount()
//------------Table-------------oTable := QTableWidget( oWnd )
oTable:move( 50, 50 )
oTable:resize( 700, 450 )

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

80 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

oTable:setRowCount( nNumRecord )
oTable:setColumnCount( nNumField )
oTable:setColumnWidth( 0, 200 )
//------------Fill table-------------for nX := 1 TO nNumRecord
for nY := 1 TO nNumField
oCell := QTableWidgetItem()
valore := FieldGet( nY )
DO CASE
CASE ValType( valore ) == "C"
oCell:setText( valore )
CASE ValType( valore ) == "N"
oCell:setText( AllTrim( Str(valore ) ) )
CASE ValType( valore ) == "D"
oCell:setText( DToC( valore ) )
CASE ValType( valore ) == "L"
oCell:setText( iif( valore == .T. ,"Yes","No" )
end CASE
oTable:setItem( nX - 1, nY - 1, oCell )
next nY
SKIP
next nX

//------------Label Table-------------oLabels := QStringList()


for nX := 1 TO 50
oLabels:append( field( nX ) )
next nX
oTable:setHorizontalHeaderLabels( oLabels )
USE
//------------Button-------------button_top := QPushButton( oWnd )
button_top :move( 100, 520 )
button_top:setText( "Top" )
button_top:Connect( "clicked()", { || top( oTable ) } )
button_bottom := QPushButton( oWnd )
button_bottom:move( 300, 520 )
button_bottom:setText( "Bottom" )
button_bottom:Connect( "clicked()", { || bottom( oTable ) } )
//------------Exec-------------oWnd:show()
QApplication():exec()
RETURN
PROCEDURE top( oT )
oT:scrollToTop()
oT:setCurrentCell( 0, 0 )
oT:setFocus()
RETURN
PROCEDURE bottom( oT )
oT:scrollToBottom()
oT:setCurrentCell( oT:rowCount() - 1, 0 )
oT:setFocus()
RETURN

Sample Applications with Databases - Flags and Database


The following example shows flags on a form. The flags are stored in a DBF archive. User can browse data by the "prev" and "next" buttons.

PROCEDURE Main()
LOCAL oWnd
LOCAL oPrev, oNext
LOCAL oImg, oState
oWnd := QMainWindow()
oWnd:SetFixedSize( 400, 300 )
oWnd:setWindowTitle( "Finestra Giovanni" )
oState := QLineEdit( oWnd )
oState:move( 100, 20 )
oState:resize( 200, 20 )
oState:setReadOnly( .T. )
oImg := QLabel( oWnd )
oImg:move( 50, 50 )
oImg:resize( 300, 200 )
oImg:setStyleSheet( "border: 2px solid #0000ff;" )
oPrev := QPushButton( oWnd )
oPrev:move( 50, 260 )
oPrev:setText( "Prev" )
oPrev:Connect( "clicked()", { || go_prev( oImg, oState ) } )
oNext := QPushButton( oWnd )
oNext:move( 250, 260 )
oNext:setText( "Next" )
oNext:Connect( "clicked()", { || go_next( oImg, oState ) } )
USE flags
first_time( oImg, oState )
oWnd:show()
QApplication():exec()
USE
RETURN
PROCEDURE go_prev( oI, oS )
skip - 1
oS:setText( flags -> state )
oI:SetPixmap( QPixmap( AllTrim(flags -> flag ) ) )
RETURN
PROCEDURE go_next( oI, oS )
SKIP
oS:setText( flags -> state )
oI:SetPixmap( QPixmap( AllTrim(flags -> flag ) ) )
RETURN

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

81 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

PROCEDURE first_time( oI, oS )


oS:setText( flags -> state )
oI:SetPixmap( QPixmap( AllTrim(flags -> flag ) ) )
RETURN

Sample Applications with Databases - Navigating in a DBF


The following example shows how to navigate in a DBF archive. User can browse data by the "prev" and "next" buttons. Some objects are STATIC to use them in external functions.

STATIC oLineEdit1, oLineEdit2, oLineEdit3


PROCEDURE Main()
LOCAL oWnd
LOCAL oLabel1, oLabel2, oLabel3
LOCAL oButton1, oButton2
oWnd := QMainWindow()
oWnd:resize( 400, 300 )
USE archivio
// ---------------------------------LABELS
oLabel1 := QLabel( oWnd )
oLabel1:setText( "Nombre" )
oLabel1:move( 5, 50 )
oLabel2 := QLabel( oWnd )
oLabel2:setText( "Apellido" )
oLabel2:move( 5, 100 )
oLabel3 := QLabel( oWnd )
oLabel3:setText( "Edad" )
oLabel3:move( 5, 150 )
// ---------------------------------FIELDS EDIT
oLineEdit1 := QLineEdit( oWnd )
oLineEdit1:move( 60, 50 )
oLineEdit1:resize( 300, 20 )
oLineEdit2 := QLineEdit( oWnd )
oLineEdit2:move( 60, 100 )
oLineEdit2:resize( 300, 20 )
oLineEdit3 := QLineEdit( oWnd )
oLineEdit3:move( 60, 150 )
oLineEdit3:resize( 300, 20 )
// ---------------------------------BUTTONS
oButton1 := QPushButton( oWnd )
oButton1:setText( "<" )
oButton1:move( 50, 200 )
oButton1:resize( 50, 50 )
oButton1:Connect( "clicked()", {|| fPrev() } )
oButton2 := QPushButton( oWnd )
oButton2:setText( ">" )
oButton2:move( 150, 200 )
oButton2:resize( 50, 50 )
oButton2:Connect( "clicked()", {|| fNext() } )
view()

// It Shows the FIRST TIME

oWnd:show()
QApplication():exec()
USE
RETURN
// --------------------------------------------PROCEDURE view()
oLineEdit1:setText( FIELD->nombre )
oLineEdit2:setText( FIELD->apellido )
oLineEdit3:setText( AllTrim( Str( FIELD->edad ) ) )
RETURN
// --------------------------------------------PROCEDURE fPrev()
SKIP -1
view()
RETURN
// --------------------------------------------PROCEDURE fNext()
SKIP
view()
RETURN

Appendix A - Photos

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

82 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

Appendix B - Contributors
Giovanni Di Maria (he is still the main developer)
Marco Braida
Massimo Belgrano
Mario Wan Stadnik (Qatan)
Apolinar
Aleksander Czajczynski

Appendix C - What users think


Sounds good! (Viktor Szakats)
Thanks again for your nice tutorial. (Viktor Szakats)
It's very nice job, thanks for this tutorial. (Viktor Szakats)
As said earlier it is a noble initiative and will stir a lot of enthusiam for Harbour users. (Pritpal Bedi)
Thanks for your great initiative providing that important tutorial. It is encouraging me to try HBQT and I found it is not as diffcult as I first thought. Thanks for your help. Regards. (Qatan)
Thank you Giovanni for the tutorial. (Guy Roussin)
Complimenti Giovanni il tuo lavoro del quale riporto per comodita' il link diretto. E' utilissimo... grazie per la condivisione... (Marco Braida)
Thanks a lot Giovanni! (Maurilio Longo)
That said, I really appreciate your tutorial! (Maurilio Longo)
I think your tutorial's link should be loaded on harbour's web page. (Angel Pais)
Many thanks for your great work. (Shum)
Congratulations for nice contribuition. (Itamar M. Lins Jr. Lins)
Dear Giovanni: Thank you for the tutorial of hbqt. It is very useful for me. Best regards. (Daniel Tello Argentina)
Imo this tutorial is good also as offcial harbour doc. (Massimo Belgrano)
It's great service for whole Harbour community. Well done and keep it up. Also a great example for how to contribute. (Viktor Szakats)
Hi Giovanni, magnificent tutorial. Many thanks. (Claudia Neumann)
Hi. I am having fun with hbqt and understand most of the excellent tutorial examples from Giovanni Di Maria. Thanks for that! I do however find the qt4 documentation quite daunting and ask for some help/further examples to do with
events. (Charlesg)
Thank you for your diligent effort on your tutorial on Harbour QT. Each example is very easy to understand the way you present them. I have learned quite a lot from them and can envision how to utilize them. Once again I thank you
for the tutorial. (Gabriel Zippilli - USA)
Tnx, for your work. (Antonio Montagut)
Hi Giovanni. Very good, your samples are the best "appetizers" for QT programming I've ever seen. (Viktor Szakats)
FWQT tutorial is based on excellent Giovanni Di Maria's HBQT tutorial. (Antonio Linares)
Your tutorial is really fantastic and invites to explore QT, thanks! :-). (Antonio Linares)
Ya tengo el browse de mi .dbf, ahora a tunearlo a mi gusto, mil gracias, no hubiese podido hacer mucho sin tu tutorial !!! (Fermn Barboza)
Great tutorial! ;-)) (Patrick Mast)
Excelente introduccion!... Gracias!! (Carlos Omran)
Hello Giovanni. I just discovered your manual and I am immensely grateful for this, congratulations. Excellent contribution. Best Regards, Edgar San. (Edgar Lee)
Many thanks for this tutorial. Cheers. (Luigi Ferraris)

Appendix D - Communities
Facebook Group HbQt
Google QtContribs Group
Google Harbour Developers Group

07/07/2016 19:29

HBQT - The tutorial about Harbour and QT programming - by Giovan...

83 de 83

http://www.elektrosoft.it/tutorials/hbqt/hbqt.asp

07/07/2016 19:29

Anda mungkin juga menyukai