Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Wiki Markup
{alias:qtablewidget}

Name

qtablewidget — spreadsheet-like table with rows and columns

...

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 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
bgColor#eeeeee
borderWidth1

{ top-left-row top-left-column bottom-right-row bottom-right-column }

To retrieve the selection use the qselection command:

No Format
bgColor#eeeeee
borderWidth1

set table [qtable]
...
set selection [qselection $table]

Arguments

rows

The number of rows in the table.

columns

The number of columns in the table.

Examples

Ex.1: Simple Table

...

No Format
bgColor#eeeeee
borderWidth1

# 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

...

No Format
bgColor#eeeeee
borderWidth1

# 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

...