📄 qtable.3qt
字号:
table.setItem( j, 1, new QCheckTableItem( &table, "Check me" ) );.fiIn the example above we create a column of QCheckTableItems and insert them into the table using setItem()..PPQTable takes ownership of its QTableItems and will delete them when the table itself is destroyed. You can take ownership of a table item using takeItem() which you use to move a cell's contents from one cell to another, either within the same table, or from one table to another. (See also, swapCells())..PPIn-place editing of the text in QTableItems, and the values in QComboTableItems and QCheckTableItems works automatically. Cells may be editable or read-only, see QTableItem::EditType..PPThe contents of a cell can be retrieved as a QTableItem using item(), or as a string with text() or as a pixmap (if there is one) with pixmap(). A cell's bounding rectangle is given by cellGeometry(). Use updateCell() to repaint a cell, for example to clear away a cell's visual representation after it has been deleted with clearCell(). The table can be forced to scroll to show a particular cell with ensureCellVisible(). The isSelected() function indicates if a cell is selected..PPIt is possible to use your own widget as a cell's widget using setCellWidget(), but subclassing QTableItem might be a simpler approach. The cell's widget (if there is one) can be removed with clearCellWidget()..PP<h4> Large tables </h4>.PPFor large, sparse, tables using QTableItems or other widgets is inefficient. The solution is to \fIdraw\fR the cell as it should appear and to create and destroy cell editors on demand..PPThis approach requires that you reimplement various functions. Reimplement paintCell() to display your data, and createEditor() and setCellContentFromEditor() to facilitate in-place editing. It is very important to reimplement resizeData() to have no functionality, to prevent QTable from attempting to create a huge array. You will also need to reimplement item(), setItem(), clearCell(), and insertWidget(), cellWidget() and clearCellWidget(). If you wish to support sorting you should also reimplement swapRows(), swapCells() and possibly swapColumns()..PPIf you represent active cells with a dictionary of QTableItems and QWidgets, i.e. only store references to cells that are actually used, most of the functions can be implemented with a single line of code. (See the table/bigtable/main.cpp example.).PPFor more information on cells see the QTableItem documenation..SH "Selections"QTable's support single selection, multi-selection (multiple cells) or no selection. The selection mode is set with setSelectionMode(). Use isSelected() to determine if a particular cell is selected, and isRowSelected() and isColumnSelected() to see if a row or column is selected..PPQTable's support multiple selections. You can programmatically select cells with addSelection(). The number of selections is given by numSelections(). The current selection is returned by currentSelection(). You can remove a selection with removeSelection() and remove all selections with clearSelection(). Selections are QTableSelection objects..SH "Signals"When the user clicks a cell the currentChanged() signal is emitted. You can also connect to the lower level clicked(), doubleClicked() and pressed() signals. If the user changes the selection the selectionChanged() signal is emitted; similarly if the user changes a cell's value the valueChanged() signal is emitted. If the user right-clicks (or presses the platform-specific key sequence) the contextMenuRequested() signal is emitted. If the user drops a drag and drop object the dropped() signal is emitted with the drop event..PPSee also Advanced Widgets..SS "Member Type Documentation".SH "QTable::EditMode".TP\fCQTable::NotEditing\fR - No cell is currently being edited..TP\fCQTable::Editing\fR - A cell is currently being edited. The editor was initialised with the cell's contents..TP\fCQTable::Replacing\fR - A cell is currently being edited. The editor was not initialised with the cell's contents..SH "QTable::FocusStyle"Specifies how the current cell (focus cell) is drawn..TP\fCQTable::FollowStyle\fR - The current cell is drawn according to the current style and the cell's background is also drawn selected, if the current cell is position within a selection.TP\fCQTable::SpreadSheet\fR - The current cell is drawn as in a spread sheet. This means, it is indicated by a black rectangle around the cell, and the background of the current cell is always drawn with the widget's base color - even when selected..SH "QTable::SelectionMode".TP\fCQTable::NoSelection\fR - No cell can be selected by the user..TP\fCQTable::Single\fR - The user may only select a single range of cells..TP\fCQTable::Multi\fR - The user may select multiple ranges of cells..TP\fCQTable::SingleRow\fR - The user may select one row at once (there is always the row of the current item selected).TP\fCQTable::MultiRow\fR - The user may select multiple rows.SH MEMBER FUNCTION DOCUMENTATION.SH "QTable::QTable ( QWidget * parent = 0, const char * name = 0 )"Creates an empty table object called \fIname\fR as a child of \fIparent\fR..PPCall setNumRows() and setNumCols() to set the table size before populating the table if you're using QTableItems..PPSee also QWidget::clearWFlags() and Qt::WidgetFlags..SH "QTable::QTable ( int numRows, int numCols, QWidget * parent = 0, const char * name = 0 )"Constructs an empty table called \fIname\fR with \fInumRows\fR rows and \fInumCols\fR columns. The table is a child of \fIparent\fR..PPIf you're using QTableItems to populate the table's cells, you can create QTableItem, QComboTableItem and QCheckTableItem items and insert them into the table using setItem(). (See the notes on large tables for an alternative to using QTableItems.).PPSee also QWidget::clearWFlags() and Qt::WidgetFlags..SH "QTable::~QTable ()"Destructor. Deletes all the resources used by a QTable object, including all QTableItems and their widgets..SH "void QTable::activateNextCell ()\fC [virtual protected]\fR"This function is called to activate the next cell if in-place editing was finished by pressing the Return key..PPThe default behaviour is to move from top to bottom, i.e. move to the cell beneath the cell being edited. Reimplement this function if you want different behaviour, e.g. moving from left to right..SH "int QTable::addSelection ( const QTableSelection & s )\fC [virtual]\fR"Adds a selection described by \fIs\fR to the table and returns its number or -1 if the selection is invalid..PPRemember to call QTableSelection::init() and QTableSelection::expandTo() to make the selection valid (see also QTableSelection::isActive())..PPSee also numSelections(), removeSelection() and clearSelection()..SH "void QTable::adjustColumn ( int col )\fC [virtual slot]\fR"Resizes column \fIcol\fR so that the column width is wide enough to display the widest item the column contains..PPSee also adjustRow()..SH "void QTable::adjustRow ( int row )\fC [virtual slot]\fR"Resizes row \fIrow\fR so that the row height is tall enough to display the tallest item the row contains..PPSee also adjustColumn()..SH "QWidget * QTable::beginEdit ( int row, int col, bool replace )\fC [virtual protected]\fR"This function is called to start in-place editing of the cell at \fIrow\fR, \fIcol\fR. Editing is achieved by creating an editor (createEditor() is called) and setting the cell's editor with setCellWidget() to the newly created editor. (After editing is complete endEdit() will be called to replace the cell's content with the editor's content.) If \fIreplace\fR is TRUE the editor will be initialized with the cell's content (if any), i.e. the user will be modifying the original cell content; otherwise the user will be entering new data..PPSee also endEdit()..SH "QRect QTable::cellGeometry ( int row, int col ) const\fC [virtual]\fR"Returns the bounding rectangle of the cell at \fIrow\fR, \fIcol\fR in content coordinates..SH "QRect QTable::cellRect ( int row, int col ) const\fC [virtual]\fR"Returns the geometry of cell \fIrow\fR, \fIcol\fR in the cell's coordinate system. This is a convenience function useful in paintCell(). It is equivalent to QRect( QPoint(0,0), cellGeometry( row, col).size() );.PPSee also cellGeometry()..SH "QWidget * QTable::cellWidget ( int row, int col ) const\fC [virtual]\fR"Returns the widget that has been set for the cell at \fIrow\fR, \fIcol\fR, or 0 if no widget has been set..PPIf you don't use QTableItems you may need to reimplement this function: see the notes on large tables..PPSee also clearCellWidget() and setCellWidget()..SH "void QTable::clearCell ( int row, int col )\fC [virtual]\fR"Removes the QTableItem at \fIrow\fR, \fIcol\fR..PPIf you don't use QTableItems you may need to reimplement this function: see the notes on large tables..SH "void QTable::clearCellWidget ( int row, int col )\fC [virtual]\fR"Removes the widget (if there is one) set for the cell at \fIrow\fR, \fIcol\fR..PPIf you don't use QTableItems you may need to reimplement this function: see the notes on large tables..PPThis function deletes the widget at \fIrow\fR, \fIcol\fR. Note that the widget is not deleted immediately but QObject::deleteLater() is called on the widget to avoid problems with timing issues..PPSee also cellWidget() and setCellWidget()..SH "void QTable::clearSelection ( bool repaint = TRUE )\fC [slot]\fR"Clears all selections and repaints the appropriate regions if \fIrepaint\fR is TRUE..PPSee also removeSelection()..SH "void QTable::clicked ( int row, int col, int button, const QPoint & mousePos )\fC [signal]\fR"This signal is emitted when mouse button \fIbutton\fR is clicked. The cell where the event took place is at \fIrow\fR, \fIcol\fR, and the mouse's position is in \fImousePos\fR..SH "int QTable::columnAt ( int x ) const\fC [virtual]\fR"Returns the number of the column at position \fIx\fR. \fIx\fR must be given in content coordinates..PPSee also columnPos() and rowAt()..SH "void QTable::columnClicked ( int col )\fC [virtual protected slot]\fR"This function is called when the column \fIcol\fR has been clicked. The default implementation sorts this column if sorting() is TRUE..SH "void QTable::columnIndexChanged ( int section, int fromIndex, int toIndex )\fC [virtual protected slot]\fR"This function is called when column order is to be changed, i.e. when the user moved the column header \fIsection\fR from \fIfromIndex\fR to \fItoIndex\fR..PPIf you want to change the column order programmatically, call swapRows() or swapColumns();.PPSee also QHeader::indexChange() and rowIndexChanged()..SH "bool QTable::columnMovingEnabled () const"Returns TRUE if columns can be moved by the user; otherwise returns FALSE. See the "columnMovingEnabled" property for details..SH "int QTable::columnPos ( int col ) const\fC [virtual]\fR"Returns the x-coordinate of the column \fIcol\fR in content coordinates..PPSee also columnAt() and rowPos()..SH "int QTable::columnWidth ( int col ) const\fC [virtual]\fR"Returns the width of column \fIcol\fR..PPSee also setColumnWidth() and rowHeight()..SH "void QTable::columnWidthChanged ( int col )\fC [virtual protected slot]\fR"This function should be called whenever the column width of \fIcol\fR has been changed. It updates the geometry of any affected columns and repaints the table to reflect the changes it has made..SH "void QTable::contentsDragEnterEvent ( QDragEnterEvent * e )\fC [virtual protected]\fR"This event handler is called whenever a QTable object receives a QDragEnterEvent \fIe\fR, i.e. when the user pressed the mouse button to drag something..PPThe focus is moved to the cell where the QDragEnterEvent occurred..PPReimplemented from QScrollView..SH "void QTable::contentsDragLeaveEvent ( QDragLeaveEvent * e )\fC [virtual protected]\fR"This event handler is called when a drag activity leaves \fIthis\fR QTable object with event \fIe\fR..PPReimplemented from QScrollView..SH "void QTable::contentsDragMoveEvent ( QDragMoveEvent * e )\fC [virtual protected]\fR"This event handler is called whenever a QTable object receives a QDragMoveEvent \fIe\fR, i.e. when the user actually drags the mouse..PPThe focus is moved to the cell where the QDragMoveEvent occurred..PPReimplemented from QScrollView..SH "void QTable::contentsDropEvent ( QDropEvent * e )\fC [virtual protected]\fR"This event handler is called when the user ends a drag and drop by dropping something onto \fIthis\fR QTable and thus triggers the drop event, \fIe\fR..PPReimplemented from QScrollView..SH "void QTable::contextMenuRequested ( int row, int col, const QPoint & pos )\fC [signal]\fR"This signal is emitted when the user invokes a context menu with the right mouse button (or with a system-specific keyboard key). The cell where the event took place is at \fIrow\fR, \fIcol\fR. \fIpos\fR is the position where the context menu will appear in the global coordinate system..SH "QWidget * QTable::createEditor ( int row, int col, bool initFromCell ) const\fC [virtual protected]\fR"This function returns the widget which should be used as an editor for the contents of the cell at \fIrow\fR, \fIcol\fR..PPIf \fIinitFromCell\fR is TRUE, the editor is used to edit the current contents of the cell (so the editor widget should be initialized with this content). If \fIinitFromCell\fR is FALSE, the content of the cell is replaced with the new content which the user entered into the widget created by this function..PPThe default functionality is as follows: if \fIinitFromCell\fR is TRUE or the cell has a QTableItem and the table item's QTableItem::isReplaceable() is FALSE then the cell is asked to create an appropriate editor (using QTableItem::createEditor()). Otherwise a QLineEdit is used as the editor..PPIf you want to create your own editor for certain cells, implement a custom QTableItem subclass and reimplement QTableItem::createEditor()..PPIf you are not using QTableItems and you don't want to use a QLineEdit as the default editor, subclass QTable and reimplement this function with code like this:.PP.nf.br QTableItem *i = item( row, col );.br if ( initFromCell || ( i && !i->isReplaceable() ) ).br // If we had a QTableItem ask the base class to create the editor.br return QTable::createEditor( row, col, initFromCell );.br else.br return ...(create your editor).br.fiOwnership of the editor widget is transferred to the caller..PPIf you reimplement this function return 0 for read-only cells. You will need to reimplement setCellContentFromEditor() to retrieve the data the user entered..PPSee also QTableItem::createEditor()..SH "int QTable::currEditCol () const\fC [protected]\fR"Returns the current edited column.SH "int QTable::currEditRow () const\fC [protected]\fR"Returns the current edited row.SH "void QTable::currentChanged ( int row, int col )\fC [signal]\fR"This signal is emitted when the current cell has changed to \fIrow\fR, \fIcol\fR..SH "int QTable::currentColumn () const"Returns the current column..PPSee also currentRow()..SH "int QTable::currentRow () const"Returns the current row..PPSee also currentColumn()..SH "int QTable::currentSelection () const\fC [virtual]\fR"Returns the number of the current selection or -1 if there is no current selection..PPSee also numSelections()..SH "void QTable::doubleClicked ( int row, int col, int button, const QPoint & mousePos )\fC [signal]\fR"This signal is emitted when mouse button \fIbutton\fR is double-clicked. The cell where the event took place is at \fIrow\fR, \fIcol\fR, and the mouse's position is in \fImousePos\fR..SH "bool QTable::dragEnabled () const\fC [slot]\fR"If this function returns TRUE, the table supports dragging..PPSee also setDragEnabled()..SH "QDragObject * QTable::dragObject ()\fC [virtual protected]\fR"If the user presses the mouse on a selected cell, starts moving (i.e. dragging), and dragEnabled() is TRUE, this function is called to obtain a drag object. A drag using this object begins immediately unless dragObject() returns 0..PPBy default this function returns 0. You might reimplement it and create a QDragObject depending on the selected items..PPSee also dropped()..SH "void QTable::drawContents ( QPainter * p, int cx, int cy, int cw, int ch )\fC [virtual protected]\fR"Draws the table contents on the painter \fIp\fR. This function is optimized so that it only draws the cells inside the \fIcw\fR pixels wide and \fIch\fR pixels high clipping rectangle at position \fIcx\fR, \fIcy\fR..PPAdditionally, drawContents() highlights the current cell..PPReimplemented from QScrollView..SH "void QTable::dropped ( QDropEvent * e )\fC [signal]\fR"This signal is emitted when a drop event occurred on the table..PP\fIe\fR contains information about the drop..SH "void QTable::editCell ( int row, int col, bool replace = FALSE )\fC [virtual slot]\fR"Starts editing the cell at \fIrow\fR, \fIcol\fR..PPIf \fIreplace\fR is TRUE the content of this cell will be replaced by the content of the editor when editing is finished, i.e. the user will be entering new data; otherwise the current content of the cell (if any) will be modified in the editor..PPSee also beginEdit()..SH "EditMode QTable::editMode () const\fC [protected]\fR"Returns the current edit mode.SH "void QTable::endEdit ( int row, int col, bool accept, bool replace )\fC [virtual protected]\fR"This function is called when in-place editing of the cell at \fIrow\fR, \fIcol\fR is requested to stop.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -