qtrace - connect widget and Tcl variable
Name
qtrace — connect widget and Tcl variable
Syntax
qtrace [widget] variable
Description
The qtrace command assigns a trace variable to a widget. A trace variable will at all times reflect the state or contents of the widget to which it is assigned. Writing to the variable will update the widget accordingly.
Assigning a value to a trace-variable may result in the corresponding widgets callback function being called. Use qblockedtrace to create a trace variable without this effect.
Arguments
widget
The widget to which the variable will be connected. If this argument is omitted the current widget will be used. This must be an instance of qlineedit, qlabel, qtextedit, [QTextEdit, qcheckbox, qradiobutton, qspinbox, qdoublespinbox, qhslider, and qvslider.
variable
The name of the variable. The variable should be global or exist in a namespace. When referring to a namespace variable the namespace name should be part of the argument, like "MyNamespace::my_variable".
Examples
Ex.1: QTrace
set w [qwidget ] qsetwindowtitle "Variable tracing" # Create text editor and "Show Text" button. set layout [qhboxlayout $w] set label [qlabel "First Name :"] qaddwidget $layout $label set lineedit [qlineedit] qaddwidget $layout $lineedit # Connect contents of line edit to variable 'name' qtrace $lineedit name # Set Tcl variable - this will automatically set the # text in the line edit to 'Jon' set name Jon qshow $w
The above example attaches the variable 'name' to the lineedit for entering a persons name. Throughout the execution of the program these entities will keep one another automatically updated. When the variable 'name' is assigned the name "Jon", the lineedit is updated accordingly. This can be seen in the screenshot. Also, if the user were to edit the lineedit directly, the 'name' variable would be updated accordingly.