Wiki Markup |
---|
{alias:qtablewidget}
h2. Name
*qtablewidget* — |
Name
qtablewidget — spreadsheet-like
...
table
...
with
...
rows
...
and
...
columns
...
Syntax
Panel | ||||||
---|---|---|---|---|---|---|
| ||||||
qtablewidget [rows columns] |
Description
The qtablewidget command creates a QTableWidget object. This is a spreadsheet-like widget which contains editable cells that are arranged in rows and columns. Each cell is numbered according to the row and column position, the upper left corner cell denoted as (0, 0).
The actual cell contents may be either qtablewidgetitem or regular widgets. For simple textual contents using qtablewidgetitems is recommended. Adding and retrieving table items and widgets is done with reference to their row and column position.
Each row and column has a header that by default shows the number for that row or column. The header for a given row or column may also be set using the qsethorizontalheaderlabel and qsetverticalheaderlabel commands.
Populating the table is usually a matter of creating QTableWidgetItem and inserting them into row and column positions using qsetitem.
The table may have a so-called selection set. This is a rectangular area spanning one or more rows and columns. The selection can be created by dragging the cursor using the mouse or keyboard. A selection is identified by the coordinates of the upper left corner and the bottom right corner. This is represented in Tcl by a 4-element list returned by the qselection command:
No Format | ||||
---|---|---|---|---|
| ||||
{ top-left-row top-left-column bottom-right-row bottom-right-column }
{noformat}
|
To
...
retrieve
...
the
...
selection
...
use
...
the
...
...
command:
...
No Format | ||||
---|---|---|---|---|
| ||||
set table [qtable]
...
set selection [qselection $table]
{noformat}
h2. Arguments
_rows_
{indent}The number of rows in the table.
{indent}
_columns_
{indent}The number of columns in the table.
{indent}
h2. Examples
h3. |
Arguments
rows
columns
Examples
Ex.1:
...
Simple
...
Table
...
Anchor | ||||
---|---|---|---|---|
|
No Format | ||||
---|---|---|---|---|
| ||||
# Create toplevel window with table inside
set w [qwidget]
qsetwindowtitle QTableWidget
qvboxlayout {
set table [qtablewidget 3 3 ] ; # 3 rows and column
# Create horizontal labels
qsethorizontalheaderlabel $table 0 "Name" ; # column 0
qsethorizontalheaderlabel $table 1 "Company" ; # column 1
qsethorizontalheaderlabel $table 2 "Dept" ; # column 2
# Insert table items with text. Supply text on construction
set nameitem [qtablewidgetitem "John Fielding"]
qsetitem $table 0 0 $nameitem ; # Row 0, column 0
# Assign text to table item after creating it.
set companyitem [qtablewidgetitem]
qsettext $companyitem HAL
qsetitem $table 0 1 $companyitem
# Read text in table item
set item [qitem $table 0 1] ; # Item in position 0, 1
set company [qtext $item]
puts "Company is $company"
}
qshow $w
{noformat}
!att:_img_cmd^qtable.png!
Example +Simple Table+ shows how to create a simple QTable. Two table items are created and assigned text.
h3. |
Example Simple Table shows how to create a simple QTable. Two table items are created and assigned text.
Ex.2:
...
Table
...
with
...
Widgets
...
Anchor | ||||
---|---|---|---|---|
|
No Format | ||||
---|---|---|---|---|
| ||||
# This proc fills in values in the qlineedits corresponding to the
# selection
proc update_selection { } {
# Retrieve list with corner cells of selection :
# toprow, topcolumn, bottomrow, bottomcolumn
set sel [qselection $::table]
if { $sel == "" } return
# Now restore each lineedit in turn, setting the text to the proper
# cell value
qsettext $::toprow [lindex $sel 0]
qsettext $::leftcol [lindex $sel 1]
qsettext $::bottomrow [lindex $sel 2]
qsettext $::rightcol [lindex $sel 3]
# Display state if checked item
set checkstate [qcheckstate $::checkitem]
qsettext $::checkinfo $checkstate
}
# Toplevel window
set tablewindow [qwidget ]
qsetattribute deleteonclose
qsetwindowtitle "Table with Widgets" ; # Title
qhboxlayout {
# Create a table wit 5 rows and columns
set table [qtablewidget 5 5 ]
# Give the table a stretchfactor of 100 inside the layout.
# This will make the table rather than the other widgets stretch when resizing the window
#
qsetstretchfactor $table 100
# Make some headers along top and left
qsethorizontalheaderlabel $table 0 "Column 0"
qsethorizontalheaderlabel $table 1 "Column 1"
qsethorizontalheaderlabel $table 2 "Column 2"
# Create some simple text items
set item [qtablewidgetitem "First Item"]
qsetitem $table 0 0 $item
set checkitem [qtablewidgetitem "Checkable Item"]
qsetcheckstate $checkitem checked
qsetitem $table 1 1 $checkitem
# Create disabled item. The item state can be set using
# one of the following flags :
# "isselectable", "iseditable", "isdragenabled", "isdropenabled", "isusercheckable", "isenabled", "istristate", "0"
set disableditem [qtablewidgetitem Disabled]
qsetflags $disableditem 0
qsetitem $table 1 0 $disableditem
# Create some widgets and insert into table
set label [qlabel "Label"]
qsetcellwidget $table 2 2 $label
set combo [qcombobox]
qadditems $combo { Red Green Blue }
qsetcellwidget $table 2 0 $combo
# Now create a group of lineedits that show which cells are selected
qgroupbox "Selection"
qgridlayout {
# The following lines simply make various lineedits
# and tags them for retrieval in update_selection
qlabel "Top row"
set toprow [qlineedit]
qrewindcolumn
qnextrow
qlabel "Bottom row"
set bottomrow [qlineedit]
qrewindcolumn
qnextrow
qlabel "Left col"
set leftcol [qlineedit]
qrewindcolumn
qnextrow
qlabel "Right col"
set rightcol [qlineedit]
qrewindcolumn
qnextrow
qlabel "Check state"
set checkinfo [qlineedit]
qrewindcolumn
qnextrow
qpushbutton "Update" { update_selection }
}
}
qshow $tablewindow
{noformat}
!att:_img_cmd^qtable_selection.png!
The example +Table with Widgets+ shows how to retrieve a selection from a QTable. If the selection is non-empty (not "") the selection list is retrieved and the selection coordinates are displayed in separate text fields. Finally the state of the checkbutton is read.
{pagebreak} |
The example Table with Widgets shows how to retrieve a selection from a QTable. If the selection is non-empty (not "") the selection list is retrieved and the selection coordinates are displayed in separate text fields. Finally the state of the checkbutton is read.