...
Hello
...
World
...
This
...
script
...
creates
...
a
...
button,
...
with
...
the
...
text
...
"Hello
...
world
...
!"
...
inside
...
a
...
window.
...
When
...
you
...
click
...
this
...
button
...
a
...
information
...
window
...
will
...
pop
...
up,
...
saying
...
"Hello
...
World
...
!"
...
and
...
displaying
...
a
...
OK
...
button.
Panel | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
This will create a new empty text editor where the graphics window used to be. |
Let us go through the script in detail. First we need a toplevel window that will contain the button.
No Format | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
set w [qwidget ]
{noformat}
|
A
...
window
...
in
...
QTcl
...
is
...
an
...
instance
...
of
...
a
...
QWidget
...
object,
...
hence
...
the
...
command
...
name.
...
It
...
may
...
be
...
followed
...
by
...
a
...
list
...
of
...
flags.
...
Although
...
rarely
...
used,
...
the
...
flags
...
can
...
be
...
used
...
to
...
modify
...
the
...
widgets
...
look
...
and
...
behavior.
...
The
...
next
...
statement
...
tells
...
QTcl
...
to
...
delete
...
the
...
widget
...
from
...
memory
...
when
...
it
...
is
...
closed:
...
No Format | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
qsetattribute deleteonclose
{noformat}
|
If
...
this
...
attribute
...
was
...
not
...
set,
...
closing
...
the
...
widget
...
would
...
merely
...
make
...
it
...
invisible
...
while
...
still
...
residing
...
in
...
memory,
...
though
...
sometimes
...
this
...
is
...
the
...
desired
...
behaviour.
...
When
...
the
...
deleteonclose
...
attribute
...
is
...
set
...
redisplaying
...
the
...
widget
...
requires
...
the
...
whole
...
widget
...
to
...
be
...
recreated.
...
No
...
let`s
...
give
...
the
...
window
...
a
...
title:
...
No Format | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
qsetwindowtitle "Hello"
{noformat}
The [ref:qsetwindowtitle] command sets the window title. While there may be several windows available, QTcl will simply apply the command to the most recently created widget, which in this case is the QWidget window. The most recently created widget is also the so-called +[current widget|ug:Glossary - List of technical terms and concepts in Geocap#current widget]+. It does not matter whether the widget is visible or not. This is in fact how most Qt commands work.
Next we want to create a layout for our window. This can be done by using a layout manager:
{noformat:title=Create a layout|borderStyle=dashed|borderColor=#404040|borderWidth=1|bgColor=#F0F0F0} |
The qsetwindowtitle command sets the window title. While there may be several windows available, QTcl will simply apply the command to the most recently created widget, which in this case is the QWidget window. The most recently created widget is also the so-called current widget. It does not matter whether the widget is visible or not. This is in fact how most Qt commands work.
Next we want to create a layout for our window. This can be done by using a layout manager:
No Format | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
qvboxlayout {
}
{noformat}
The [ref:qvboxlayout] command creates a layout manager for the window. A layout manager is also known as a geometry manager. The task of the layout manager is to arrange the widgets in a particular order. For instance, the QVBoxLayout layout manager stacks widgets vertically in their order of creation. There are two other layout managers in QTcl:
# The QHBoxLayout, which is similar to QVBoxLayout except it arranges widgets horizontally
# The QGridLayout, which arranges widgets in a grid pattern.
Now we want to populate the layout with a widget, in this case a button.
{noformat:title=Create a push button|borderStyle=dashed|borderColor=#404040|borderWidth=1|bgColor=#F0F0F0} |
The qvboxlayout command creates a layout manager for the window. A layout manager is also known as a geometry manager. The task of the layout manager is to arrange the widgets in a particular order. For instance, the QVBoxLayout layout manager stacks widgets vertically in their order of creation. There are two other layout managers in QTcl:
- The QHBoxLayout, which is similar to QVBoxLayout except it arranges widgets horizontally
- The QGridLayout, which arranges widgets in a grid pattern.
Now we want to populate the layout with a widget, in this case a button.
No Format | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
qpushbutton "Hello world!"
{noformat}
|
The
...
command
...
...
creates
...
the
...
conventional
...
button
...
found
...
in
...
user
...
interfaces.
...
There
...
are
...
several
...
types
...
of
...
buttons
...
available
...
in
...
QTcl.
...
Right
...
now
...
this
...
button
...
only
...
has
...
one
...
argument
...
which
...
is
...
the
...
button
...
text.
...
Thus
...
we
...
need
...
a
...
second
...
argument
...
to
...
tell
...
the
...
button
...
what
...
to
...
do
...
when
...
being
...
clicked.
...
The
...
second
...
argument
...
will
...
be
...
a
...
Tcl
...
list
...
that
...
serves
...
as
...
the
...
callback
...
function
...
for
...
this
...
button.
...
This
...
simply
...
means
...
that
...
the
...
commands
...
in
...
the
...
list
...
will
...
be
...
evaluated
...
when
...
the
...
button
...
is
...
pushed.
...
Following
...
is
...
the
...
command
...
that
...
will
...
be
...
executed:
...
No Format | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
qinformation Information "Hello world!"
{noformat}
|
This
...
command
...
displays
...
a
...
simple
...
information
...
dialog
...
with
...
an
...
'OK'
...
button.
...
So
...
far
...
we
...
have
...
created
...
the
...
window
...
and
...
the
...
button
...
in
...
memory.
...
No
...
window
...
is
...
yet
...
visible
...
on
...
the
...
screen.
...
To
...
make
...
the
...
window
...
visible
...
we
...
have
...
to
...
invoke
...
the
...
final
...
...
command.
...
No Format | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
qshow $w
{noformat}
|
QTcl
...
will
...
then
...
draw
...
the
...
most
...
recently
...
created
...
widget,
...
or
...
...
...
,
...
on
...
to
...
the
...
screen.
...
In
...
this
...
case
...
the
...
current
...
widget
...
is
...
the
...
window,
...
which
...
is
...
then
...
displayed
...
along
...
with
...
its
...
contents.
...
Your
...
script
...
should
...
now
...
look
...
something
...
like
...
this:
...
No Format | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
# Create toplevel window
set w [qwidget ]
# Delete window from memory when closing
qsetattribute deleteonclose
qsetwindowtitle "Hello"
# Arrange contents vertically
qvboxlayout {
# Create a button that show an information dialog when pushed
qpushbutton "Hello world!" { qinformation Information "Hello world!" }
}
# Make window visible
qshow $w
{noformat}
{panel:title=Execute the script|borderStyle=dashed|borderColor=#404040|borderWidth=1|bgColor=#F0F0F0}
* Execute the script by right clicking anywhere in the text editor window and selecting *Execute*
A window with a *Hello world\!*\-button should now appear, and when clicking this button an information window saying "Hello world\!" with an *OK* button should appear.
{panel}
While this script is short, it has illustrated several key features of QTcl programming. For example, all QTcl commands begin with the letter 'q'. This serves the purpose of setting QTcl commands apart from the built-in Tcl commands. Also, several commands are located inside the '\{...}' delimited block that follows the [ref:qvboxlayout] statement. This syntactical structure tells QTcl that all widgets enclosed in the '\{...}' block should be treated according to the enclosing command, in this case [ref:qvboxlayout]. Moreover, since the QVBoxLayout is owned by the toplevel window, all widgets created inside this layout manager are also made child widgets of this window.
{pagebreak} |
Panel | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
A window with a Hello world!-button should now appear, and when clicking this button an information window saying "Hello world!" with an OK button should appear. |
While this script is short, it has illustrated several key features of QTcl programming. For example, all QTcl commands begin with the letter 'q'. This serves the purpose of setting QTcl commands apart from the built-in Tcl commands. Also, several commands are located inside the '{...}' delimited block that follows the qvboxlayout statement. This syntactical structure tells QTcl that all widgets enclosed in the '{...}' block should be treated according to the enclosing command, in this case qvboxlayout. Moreover, since the QVBoxLayout is owned by the toplevel window, all widgets created inside this layout manager are also made child widgets of this window.