📄 q3table.cpp
字号:
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. It is possible to use your own widget as a cell's widget using setCellWidget(), but subclassing Q3TableItem might be a simpler approach. The cell's widget (if there is one) can be removed with clearCellWidget(). \keyword notes on large tables \target bigtables \section2 Large tables For large, sparse, tables using Q3TableItems or other widgets is inefficient. The solution is to \e draw the cell as it should appear and to create and destroy cell editors on demand. This approach requires that you reimplement various functions. Reimplement paintCell() to display your data, and createEditor() and setCellContentFromEditor() to support in-place editing. It is very important to reimplement resizeData() to have no functionality, to prevent Q3Table from attempting to create a huge array. You will also need to reimplement item(), setItem(), takeItem(), clearCell(), and insertWidget(), cellWidget() and clearCellWidget(). In almost every circumstance (for sorting, removing and inserting columns and rows, etc.), you also need to reimplement swapRows(), swapCells() and swapColumns(), including header handling. If you represent active cells with a dictionary of Q3TableItems and QWidgets, i.e. only store references to cells that are actually used, many of the functions can be implemented with a single line of code. For more information on cells see the Q3TableItem documenation. \target selections \section1 Selections Q3Table'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. Q3Table's support many simultaneous 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 Q3TableSelection objects. To easily add a new selection use selectCells(), selectRow() or selectColumn(). Alternatively, use addSelection() to add new selections using Q3TableSelection objects. The advantage of using Q3TableSelection objects is that you can call Q3TableSelection::expandTo() to resize the selection and can query and compare them. 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(). \target signals \section1 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 appropriate 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.*//*! \fn void Q3Table::currentChanged(int row, int col) This signal is emitted when the current cell has changed to \a row, \a col.*//*! \fn void Q3Table::valueChanged(int row, int col) This signal is emitted when the user changed the value in the cell at \a row, \a col.*//*! \fn int Q3Table::currentRow() const Returns the current row. \sa currentColumn()*//*! \fn int Q3Table::currentColumn() const Returns the current column. \sa currentRow()*//*! \enum Q3Table::EditMode \value NotEditing No cell is currently being edited. \value Editing A cell is currently being edited. The editor was initialised with the cell's contents. \value Replacing A cell is currently being edited. The editor was not initialised with the cell's contents.*//*! \enum Q3Table::SelectionMode \value NoSelection No cell can be selected by the user. \value Single The user may only select a single range of cells. \value Multi The user may select multiple ranges of cells. \value SingleRow The user may select one row at once. \value MultiRow The user may select multiple rows.*//*! \enum Q3Table::FocusStyle Specifies how the current cell (focus cell) is drawn. \value FollowStyle The current cell is drawn according to the current style and the cell's background is also drawn selected, if the current cell is within a selection \value SpreadSheet The current cell is drawn as in a spreadsheet. This means, it is signified 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.*//*! \fn void Q3Table::clicked(int row, int col, int button, const QPoint &mousePos) This signal is emitted when mouse button \a button is clicked. The cell where the event took place is at \a row, \a col, and the mouse's position is in \a mousePos. \sa Qt::MouseButton*//*! \fn void Q3Table::doubleClicked(int row, int col, int button, const QPoint &mousePos) This signal is emitted when mouse button \a button is double-clicked. The cell where the event took place is at \a row, \a col, and the mouse's position is in \a mousePos. \sa Qt::MouseButton*//*! \fn void Q3Table::pressed(int row, int col, int button, const QPoint &mousePos) This signal is emitted when mouse button \a button is pressed. The cell where the event took place is at \a row, \a col, and the mouse's position is in \a mousePos. \sa Qt::MouseButton*//*! \fn void Q3Table::selectionChanged() This signal is emitted whenever a selection changes. \sa Q3TableSelection*//*! \fn void Q3Table::contextMenuRequested(int row, int col, const QPoint & pos) This signal is emitted when the user invokes a context menu with the right mouse button (or with a system-specific keypress). The cell where the event took place is at \a row, \a col. \a pos is the position where the context menu will appear in the global coordinate system. This signal is always emitted, even if the contents of the cell are disabled.*//*! Creates an empty table object called \a name as a child of \a parent. Call setNumRows() and setNumCols() to set the table size before populating the table if you're using Q3TableItems.*/Q3Table::Q3Table(QWidget *parent, const char *name) : Q3ScrollView(parent, name, WNoAutoErase | WStaticContents), leftHeader(0), topHeader(0), currentSel(0), lastSortCol(-1), sGrid(true), mRows(false), mCols(false), asc(true), doSort(true), readOnly(false){ init(0, 0);}/*! Constructs an empty table called \a name with \a numRows rows and \a numCols columns. The table is a child of \a parent. If you're using \l{Q3TableItem}s to populate the table's cells, you can create Q3TableItem, Q3ComboTableItem and Q3CheckTableItem items and insert them into the table using setItem(). (See the notes on large tables for an alternative to using Q3TableItems.)*/Q3Table::Q3Table(int numRows, int numCols, QWidget *parent, const char *name) : Q3ScrollView(parent, name, WNoAutoErase | WStaticContents), leftHeader(0), topHeader(0), currentSel(0), lastSortCol(-1), sGrid(true), mRows(false), mCols(false), asc(true), doSort(true), readOnly(false){ init(numRows, numCols);}/*! \internal*/void Q3Table::init(int rows, int cols){#ifndef QT_NO_DRAGANDDROP setDragAutoScroll(false);#endif d = new Q3TablePrivate; d->geomTimer = new QTimer(this); d->lastVisCol = 0; d->lastVisRow = 0; connect(d->geomTimer, SIGNAL(timeout()), this, SLOT(updateGeometriesSlot())); shouldClearSelection = false; dEnabled = false; roRows.setAutoDelete(true); roCols.setAutoDelete(true); setSorting(false); unused = true; // It's unused, ain't it? :) selMode = Multi; contents.setAutoDelete(true); widgets.setAutoDelete(true); // Enable clipper and set background mode enableClipper(qt_table_clipper_enabled); viewport()->setFocusProxy(this); viewport()->setFocusPolicy(Qt::WheelFocus); setFocusPolicy(Qt::WheelFocus); viewport()->setBackgroundMode(PaletteBase); setBackgroundMode(PaletteBackground, PaletteBase); setResizePolicy(Manual); selections.setAutoDelete(true); // Create headers leftHeader = new Q3TableHeader(rows, this, this, "left table header"); leftHeader->setOrientation(Vertical); leftHeader->setTracking(true); leftHeader->setMovingEnabled(true); topHeader = new Q3TableHeader(cols, this, this, "right table header"); topHeader->setOrientation(Horizontal); topHeader->setTracking(true); topHeader->setMovingEnabled(true); if (QApplication::reverseLayout()) setMargins(0, fontMetrics().height() + 4, 30, 0); else setMargins(30, fontMetrics().height() + 4, 0, 0); topHeader->setUpdatesEnabled(false); leftHeader->setUpdatesEnabled(false); // Initialize headers int i = 0; for (i = 0; i < numCols(); ++i) topHeader->resizeSection(i, QMAX(100, QApplication::globalStrut().height())); for (i = 0; i < numRows(); ++i) leftHeader->resizeSection(i, QMAX(20, QApplication::globalStrut().width())); topHeader->setUpdatesEnabled(true); leftHeader->setUpdatesEnabled(true); // Prepare for contents contents.setAutoDelete(false); // Connect header, table and scrollbars connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), topHeader, SLOT(setOffset(int))); connect(verticalScrollBar(), SIGNAL(valueChanged(int)), leftHeader, SLOT(setOffset(int))); connect(topHeader, SIGNAL(sectionSizeChanged(int)), this, SLOT(columnWidthChanged(int))); connect(topHeader, SIGNAL(indexChange(int,int,int)), this, SLOT(columnIndexChanged(int,int,int))); connect(topHeader, SIGNAL(sectionClicked(int)), this, SLOT(columnClicked(int))); connect(leftHeader, SIGNAL(sectionSizeChanged(int)), this, SLOT(rowHeightChanged(int))); connect(leftHeader, SIGNAL(indexChange(int,int,int)), this, SLOT(rowIndexChanged(int,int,int))); // Initialize variables autoScrollTimer = new QTimer(this); connect(autoScrollTimer, SIGNAL(timeout()), this, SLOT(doAutoScroll())); curRow = curCol = 0; topHeader->setSectionState(curCol, Q3TableHeader::Bold); leftHeader->setSectionState(curRow, Q3TableHeader::Bold); edMode = NotEditing; editRow = editCol = -1; drawActiveSelection = true; installEventFilter(this); focusStl = SpreadSheet; was_visible = false; // initial size resize(640, 480);}/*! Releases all the resources used by the Q3Table object, including all \l{Q3TableItem}s and their widgets.*/Q3Table::~Q3Table(){ setUpdatesEnabled(false); contents.setAutoDelete(true); contents.clear(); widgets.clear(); delete d;}void Q3Table::setReadOnly(bool b){ readOnly = b; Q3TableItem *i = item(curRow, curCol); if (readOnly && isEditing()) { endEdit(editRow, editCol, true, false); } else if (!readOnly && i && (i->editType() == Q3TableItem::WhenCurrent || i->editType() == Q3TableItem::Always)) { editCell(curRow, curCol); }}/*! If \a ro is true, row \a row is set to be read-only; otherwise the row is set to be editable. Whether a cell in this row is editable or read-only depends on the cell's EditType, and this setting. \sa isRowReadOnly() setColumnReadOnly() setReadOnly()*/void Q3Table::setRowReadOnly(int row, bool ro){ if (ro) roRows.replace(row, new int(0)); else roRows.remove(row); if (curRow == row) { Q3TableItem *i = item(curRow, curCol); if (ro && isEditing()) { endEdit(editRow, editCol, true, false); } else if (!ro && i && (i->editType() == Q3TableItem::WhenCurrent || i->editType() == Q3TableItem::Always)) { editCell(curRow, curCol); } }}/*! If \a ro is true, column \a col is set to be read-only; otherwise the column is set to be editable. Whether a cell in this column is editable or read-only depends on the cell's EditType, and this setting. \sa isColumnReadOnly() setRowReadOnly() setReadOnly()*/void Q3Table::setColumnReadOnly(int col, bool ro){ if (ro) roCols.replace(col, new int(0)); else roCols.remove(col); if (curCol == col) { Q3TableItem *i = item(curRow, curCol); if (ro && isEditing()) { endEdit(editRow, editCol, true, false); } else if (!ro && i && (i->editType() == Q3TableItem::WhenCurrent || i->editType() == Q3TableItem::Always)) { editCell(curRow, curCol); } }}/*! \property Q3Table::readOnly \brief whether the table is read-only Whether a cell in the table is editable or read-only depends on the cell's \link Q3TableItem::EditType EditType\endlink, and this setting. \sa QWidget::enabled setColumnReadOnly() setRowReadOnly()*/bool Q3Table::isReadOnly() const{ return readOnly;}/*! Returns true if row \a row is read-only; other
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -