Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QTableItem class provides the cell content for QTable cells. More...
#include <qtable.h>
Inherits Qt.
Inherited by QComboTableItem and QCheckTableItem.
The QTableItem class provides the cell content for QTable cells.
For many applications QTableItems are ideal for presenting and editing the contents of QTable cells. In situations where you need to create very large tables you may prefer an alternative approach to using QTableItems: see the notes on large tables.
A QTableItem contains a cell's data, by default, a string and a pixmap. The table item also holds the cell's display size and how the data should be aligned. The table item specifies the cell's EditType and the editor used for in-place editing (by default a QLineEdit). If you want checkboxes use QCheckTableItem, and if you want comboboxes use QComboTableItem. The EditType (set in the constructor) determines whether the cell's contents may be edited; setReplaceable() sets whether the cell's contents may be replaced by another cell's contents.
If a pixmap is specified it is displayed to the left of any text. You can change the text or pixmap with setText() and setPixmap() respectively. For text you can use setWordWrap(). A table item's alignment is set in the constructor.
Reimplement createEditor() and setContentFromEditor() if you want to use your own widget instead of a QLineEdit for editing cell contents. Reimplement paint() if you want to display custom content. If you want a checkbox table item use QCheckTableItem, and if you want a combo table item use QComboTableItem.
When sorting table items the key() function is used; by default this returns the table item's text(). Reimplement key() to customize how your table items will sort.
Table items are inserted into a table using QTable::setItem(). If you insert an item into a cell that already contains a table item the original item will be deleted.
Example:
for ( int row = 0; row < table->numRows(); row++ ) { for ( int col = 0; col < table->numCols(); col++ ) { table->setItem( row, col, new QTableItem( table, WhenCurrent, QString::number( row * col ) ) ); } }
You can move a table item from one cell to another, in the same or a different table, using QTable::takeItem() and QTable::setItem() but see also QTable::swapCells().
Table items can be deleted with delete in the standard way; the table and cell will be updated accordingly.
See also QCheckTableItem, QComboTableItem and Advanced Widgets.
This enum is used to define whether a cell is editable or read-only (in conjunction with other settings), and how the cell should be displayed.
Using this EditType ensures that the editor created with createEditor() (by default a QLineEdit) is always visible. This has implications for the alignment of the content: the default editor aligns everything (even numbers) to the left whilst numerical values in the cell are by default aligned to the right.
If a cell with the edit type Always looks misaligned you could reimplement createEditor() for these items.
The OnTyping edit type is the default when QTableItem objects are created by the convenience functions QTable::setText() and QTable::setPixmap().
The cell is actually editable only if QTable::isRowReadOnly() is FALSE for its row, QTable::isColumnReadOnly() is FALSE for its column, and QTable::isReadOnly() is FALSE.
QComboTableItems have an isEditable() property. This property is used to indicate whether the user may enter their own text or are restricted to choosing one of the choices in the list. QComboTableItems may be interacted with only if they are editable in accordance with their EditType as described above.
The table item will use a QLineEdit for its editor, will not word-wrap and will occupy a single cell. Insert the table item into a table with QTable::setItem().
The table takes ownership of the table item, so a table item should not be inserted into more than one table at a time.
The table item will display the pixmap to the left of the text. It will use a QLineEdit for editing the text, will not word-wrap and will occupy a single cell. Insert the table item into a table with QTable::setItem().
The table takes ownership of the table item, so a table item should not be inserted in more than one table at a time.
If the table item is in a table (i.e. was inserted with setItem()), it will be removed from the table and the cell it occupied.
See also Qt::AlignmentFlags.
See also setSpan() and rowSpan().
If the function returns 0, the cell is read-only.
The returned widget should preferably be invisible, ideally with QTable::viewport() as parent.
If you reimplement this function you'll almost certainly need to reimplement setContentFromEditor(), and may need to reimplement sizeHint().
QWidget *ComboItem::createEditor() const { // create an editor - a combobox in our case ( (ComboItem*)this )->cb = new QComboBox( table()->viewport() ); QObject::connect( cb, SIGNAL( activated( int ) ), table(), SLOT( doValueChanged() ) ); cb->insertItem( "Yes" ); cb->insertItem( "No" ); // and initialize it cb->setCurrentItem( text() == "No" ? 1 : 0 ); return cb;
See also QTable::createEditor(), setContentFromEditor() and QTable::viewport().
Example: table/statistics/statistics.cpp.
This is set when the table item is constructed.
See also EditType and QTableItem().
See also setEnabled().
(This differs from EditType because EditType is concerned with whether the user is able to change the contents of a cell.)
See also setReplaceable() and EditType.
See also QTable::sorting.
If selected is TRUE the cell is displayed in a way that indicates that it is highlighted.
You don't usually need to use this function but if you want to draw custom content in a cell you will need to reimplement it.
Note that the painter is not clipped by default in order to get maximum efficiency. If you want clipping, use
p->setClipRect( table()->cellRect(row, col), QPainter::ClipPainter ); //... your drawing code p->setClipping( FALSE );
Example: table/statistics/statistics.cpp.
See also setPixmap() and text().
See also setSpan() and colSpan().
Although often frowned upon by purists, Run Time Type Identification is very useful for QTables as it allows for an efficient indexed storage mechanism.
When you create subclasses based on QTableItem make sure that each subclass returns a unique rtti() value. It is advisable to use values greater than 1000, preferably large random numbers, to allow for extensions to this class.
See also QCheckTableItem::rtti() and QComboTableItem::rtti().
Reimplemented in QComboTableItem and QCheckTableItem.
If the cell spans multiple columns, this function sets the left-most column and retains the width of the multi-cell table item.
See also col(), setRow() and colSpan().
If you reimplement createEditor() and return something that is not a QLineEdit you will almost certainly have to reimplement this function.
void ComboItem::setContentFromEditor( QWidget *w ) { // the user changed the value of the combobox, so synchronize the // value of the item (its text), with the value of the combobox if ( w->inherits( "QComboBox" ) ) setText( ( (QComboBox*)w )->currentText() ); else QTableItem::setContentFromEditor( w );
See also QTable::setCellContentFromEditor().
Example: table/statistics/statistics.cpp.
A disabled item doesn't respond to user interaction.
See also isEnabled().
Note that setPixmap() does not update the cell the table item belongs to. Use QTable::updateCell() to repaint the cell's contents.
For QComboTableItems and QCheckTableItems this function has no visible effect.
See also QTable::setPixmap(), pixmap() and setText().
(This differs from EditType because EditType is concerned with whether the user is able to change the contents of a cell.)
See also isReplaceable().
If the cell spans multiple rows, this function sets the top row and retains the height of the multi-cell table item.
See also row(), setCol() and rowSpan().
Warning: This function only works, if the item has already been inserted into the table using e.g. QTable::setItem(). This function also checks to make sure if rs and cs are within the bounds of the table and returns without changing the span if they are not.
See also rowSpan() and colSpan().
Note that setText() does not update the cell the table item belongs to. Use QTable::updateCell() to repaint the cell's contents.
See also QTable::setText(), text(), setPixmap() and QTable::updateCell().
Example: table/statistics/statistics.cpp.
See also wordWrap(), QTable::adjustColumn() and QTable::setColumnStretchable().
If you subclass QTableItem you will often need to reimplement this function.
Returns the QTable the table item belongs to.
See also QTable::setItem() and QTableItem().
If editMode() is Always, setContentFromEditor() is called before, so that the current value of the editor is returned.
If editMode() is something else and the editor of the cell is active, setContentFromEditor() is called, if the editor is something else than a QLineEdit. This means, in this case if the user enteres a text, text() returns the original value of the item until the user commits the new data (presses e.g. Return or Tab). For other editors (e.g. a combobox), always setContentFromEditor() is called, so always the value currently displayed in the editor is returned.
See also setText() and pixmap().
See also setWordWrap().
This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.
Copyright © 2002 Trolltech | Trademarks | Qt version 3.0.4
|