📄 q3table.cpp
字号:
\sa setCurrentItem()*/int Q3ComboTableItem::currentItem() const{ QWidget *w = table()->cellWidget(row(), col()); QComboBox *cb = ::qobject_cast<QComboBox*>(w); if (cb) return cb->currentItem(); return current;}/*! Returns the text of the combo table item's current list item. \sa currentItem() text()*/QString Q3ComboTableItem::currentText() const{ QWidget *w = table()->cellWidget(row(), col()); QComboBox *cb = ::qobject_cast<QComboBox*>(w); if (cb) return cb->currentText(); return entries.at(current);}/*! Returns the total number of list items in the combo table item.*/int Q3ComboTableItem::count() const{ QWidget *w = table()->cellWidget(row(), col()); QComboBox *cb = ::qobject_cast<QComboBox*>(w); if (cb) return cb->count(); return (int)entries.count();}/*! Returns the text of the combo's list item at index \a i. \sa currentText()*/QString Q3ComboTableItem::text(int i) const{ QWidget *w = table()->cellWidget(row(), col()); QComboBox *cb = ::qobject_cast<QComboBox*>(w); if (cb) return cb->text(i); return entries.at(i);}/*! If \a b is true the combo table item can be edited, i.e. the user may enter a new text item themselves. If \a b is false the user may may only choose one of the existing items. \sa isEditable()*/void Q3ComboTableItem::setEditable(bool b){ edit = b;}/*! Returns true if the user can add their own list items to the combobox's list of items; otherwise returns false. \sa setEditable()*/bool Q3ComboTableItem::isEditable() const{ return edit;}int Q3ComboTableItem::RTTI = 1;/*! \fn int Q3ComboTableItem::rtti() const Returns 1. Make your derived classes return their own values for rtti()to distinguish between different table item subclasses. You should use values greater than 1000, preferably a large random number, to allow for extensions to this class. \sa Q3TableItem::rtti()*/int Q3ComboTableItem::rtti() const{ return RTTI;}/*! \reimp */QSize Q3ComboTableItem::sizeHint() const{ fakeCombo->insertItem(currentText()); fakeCombo->setCurrentItem(fakeCombo->count() - 1); QSize sh = fakeCombo->sizeHint(); fakeCombo->removeItem(fakeCombo->count() - 1); return sh.expandedTo(QApplication::globalStrut());}/*! \class Q3CheckTableItem \brief The Q3CheckTableItem class provides checkboxes in Q3Tables. \compat A Q3CheckTableItem is a table item which looks and behaves like a checkbox. The advantage of using Q3CheckTableItems rather than real checkboxes is that a Q3CheckTableItem uses far less resources than a real checkbox would in a \l{Q3Table}. When the cell has the focus it displays a real checkbox which the user can interact with. When the cell does not have the focus the cell \e looks like a checkbox. Pixmaps may not be used in Q3CheckTableItems. Q3CheckTableItem items have the edit type \c WhenCurrent (see \l{EditType}). To change the checkbox's label use setText(). The checkbox can be checked and unchecked with setChecked() and its state retrieved using isChecked(). To populate a table cell with a Q3CheckTableItem use Q3Table::setItem(). Q3CheckTableItems can be distinguished from \l{Q3TableItem}s and \l{Q3ComboTableItem}s using their Run Time Type Identification (rtti) value. \img qtableitems.png Table Items \sa rtti() EditType Q3ComboTableItem Q3TableItem QCheckBox*//*! Creates a Q3CheckTableItem with an \l{EditType} of \c WhenCurrent as a child of \a table. The checkbox is initially unchecked and its label is set to the string \a txt.*/Q3CheckTableItem::Q3CheckTableItem(Q3Table *table, const QString &txt) : Q3TableItem(table, WhenCurrent, txt), checked(false){}/*! \reimp */void Q3CheckTableItem::setText(const QString &t){ Q3TableItem::setText(t); QWidget *w = table()->cellWidget(row(), col()); QCheckBox *cb = ::qobject_cast<QCheckBox*>(w); if (cb) cb->setText(t);}/*! \reimp */QWidget *Q3CheckTableItem::createEditor() const{ // create an editor - a combobox in our case ((Q3CheckTableItem*)this)->cb = new QCheckBox(table()->viewport(), "qt_editor_checkbox"); cb->setChecked(checked); cb->setText(text()); cb->setBackgroundColor(table()->viewport()->backgroundColor()); cb->setAutoFillBackground(true); QObject::connect(cb, SIGNAL(toggled(bool)), table(), SLOT(doValueChanged())); return cb;}/*! \reimp */void Q3CheckTableItem::setContentFromEditor(QWidget *w){ QCheckBox *cb = ::qobject_cast<QCheckBox*>(w); if (cb) checked = cb->isChecked();}/*! \reimp */void Q3CheckTableItem::paint(QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected){ QPalette pal = cg; p->fillRect(0, 0, cr.width(), cr.height(), selected ? pal.brush(QPalette::Highlight) : pal.brush(QPalette::Base)); QSize sz = QSize(table()->style()->pixelMetric(QStyle::PM_IndicatorWidth), table()->style()->pixelMetric(QStyle::PM_IndicatorHeight)); QPalette pal2(pal); pal2.setBrush(QPalette::Background, pal.brush(QPalette::Base)); QStyleOptionButton opt; opt.initFrom(table()); opt.rect.setRect(0, (cr.height() - sz.height()) / 2, sz.width(), sz.height()); opt.palette = pal2; opt.state &= ~QStyle::State_HasFocus; opt.state &= ~QStyle::State_MouseOver; if(isEnabled()) opt.state |= QStyle::State_Enabled; if (checked) opt.state |= QStyle::State_On; else opt.state |= QStyle::State_Off; if (isEnabled() && table()->isEnabled()) opt.state |= QStyle::State_Enabled; table()->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, p, table()); if (selected) p->setPen(pal.highlightedText().color()); else p->setPen(pal.text().color()); opt.rect.setRect(0, 0, cr.width(), cr.height()); QRect textRect = table()->style()->subElementRect(QStyle::SE_CheckBoxContents, &opt, table()); p->drawText(textRect, wordWrap() ? (alignment() | Qt::WordBreak) : alignment(), text());}/*! If \a b is true the checkbox is checked; if \a b is false the checkbox is unchecked. \sa isChecked()*/void Q3CheckTableItem::setChecked(bool b){ checked = b; table()->updateCell(row(), col()); QWidget *w = table()->cellWidget(row(), col()); QCheckBox *cb = ::qobject_cast<QCheckBox*>(w); if (cb) cb->setChecked(b);}/*! Returns true if the checkbox table item is checked; otherwise returns false. \sa setChecked()*/bool Q3CheckTableItem::isChecked() const{ // #### why was this next line here. It must not be here, as // #### people want to call isChecked() from within paintCell() // #### and end up in an infinite loop that way // table()->updateCell(row(), col()); QWidget *w = table()->cellWidget(row(), col()); QCheckBox *cb = ::qobject_cast<QCheckBox*>(w); if (cb) return cb->isChecked(); return checked;}int Q3CheckTableItem::RTTI = 2;/*! \fn int Q3CheckTableItem::rtti() const Returns 2. Make your derived classes return their own values for rtti()to distinguish between different table item subclasses. You should use values greater than 1000, preferably a large random number, to allow for extensions to this class. \sa Q3TableItem::rtti()*/int Q3CheckTableItem::rtti() const{ return RTTI;}/*! \reimp */QSize Q3CheckTableItem::sizeHint() const{ QSize sz = QSize(table()->style()->pixelMetric(QStyle::PM_IndicatorWidth), table()->style()->pixelMetric(QStyle::PM_IndicatorHeight)); sz.setWidth(sz.width() + 6); QSize sh(Q3TableItem::sizeHint()); return QSize(sh.width() + sz.width(), QMAX(sh.height(), sz.height())). expandedTo(QApplication::globalStrut());}/*! \class Q3Table \brief The Q3Table class provides a flexible editable table widget. \compat Q3Table is easy to use, although it does have a large API because of the comprehensive functionality that it provides. Q3Table includes functions for manipulating \link #headers headers\endlink, \link #columnsrows rows and columns\endlink, \link #cells cells\endlink and \link #selections selections\endlink. Q3Table also provides in-place editing and drag and drop, as well as a useful set of \link #signals signals\endlink. Q3Table efficiently supports very large tables, for example, tables one million by one million cells are perfectly possible. Q3Table is economical with memory, using none for unused cells. \code Q3Table *table = new Q3Table(100, 250, this); table->setPixmap(3, 2, pix); table->setText(3, 2, "A pixmap"); \endcode The first line constructs the table specifying its size in rows and columns. We then insert a pixmap and some text into the \e same \link #cells cell\endlink, with the pixmap appearing to the left of the text. Q3Table cells can be populated with \l{Q3TableItem}s, \l{Q3ComboTableItem}s or by \l{Q3CheckTableItem}s. By default a vertical header appears at the left of the table showing row numbers and a horizontal header appears at the top of the table showing column numbers. (The numbers displayed start at 1, although row and column numbers within Q3Table begin at 0.) If you want to use mouse tracking call setMouseTracking(true) on the \e viewport. \img qtableitems.png Table Items \target headers \section1 Headers Q3Table supports a header column, e.g. to display row numbers, and a header row, e.g to display column titles. To set row or column labels use Q3Header::setLabel() on the pointers returned by verticalHeader() and horizontalHeader() respectively. The vertical header is displayed within the table's left margin whose width is set with setLeftMargin(). The horizontal header is displayed within the table's top margin whose height is set with setTopMargin(). The table's grid can be switched off with setShowGrid(). If you want to hide a horizontal header call hide(), and call setTopMargin(0) so that the area the header would have occupied is reduced to zero size. Header labels are indexed via their section numbers. Note that the default behavior of Q3Header regarding section numbers is overriden for Q3Table. See the explanation below in the Rows and Columns section in the discussion of moving columns and rows. \target columnsrows \section1 Rows and Columns Row and column sizes are set with setRowHeight() and setColumnWidth(). If you want a row high enough to show the tallest item in its entirety, use adjustRow(). Similarly, to make a column wide enough to show the widest item use adjustColumn(). If you want the row height and column width to adjust automatically as the height and width of the table changes use setRowStretchable() and setColumnStretchable(). Rows and columns can be hidden and shown with hideRow(), hideColumn(), showRow() and showColumn(). New rows and columns are inserted using insertRows() and insertColumns(). Additional rows and columns are added at the bottom (rows) or right (columns) if you set setNumRows() or setNumCols() to be larger than numRows() or numCols(). Existing rows and columns are removed with removeRow() and removeColumn(). Multiple rows and columns can be removed with removeRows() and removeColumns(). Rows and columns can be set to be movable using rowMovingEnabled() and columnMovingEnabled(). The user can drag them to reorder them holding down the Ctrl key and dragging the mouse. For performance reasons, the default behavior of Q3Header section numbers is overridden by Q3Table. Currently in Q3Table, when a row or column is dragged and reordered, the section number is also changed to its new position. Therefore, there is no difference between the section and the index fields in Q3Header. The Q3Table Q3Header classes do not provide a mechanism for indexing independently of the user interface ordering. The table can be sorted using sortColumn(). Users can sort a column by clicking its header if setSorting() is set to true. Rows can be swapped with swapRows(), columns with swapColumns() and cells with swapCells(). For editable tables (see setReadOnly()) you can set the read-only property of individual rows and columns with setRowReadOnly() and setColumnReadOnly(). (Whether a cell is editable or read-only depends on these settings and the cell's Q3TableItem. The row and column which have the focus are returned by currentRow() and currentColumn() respectively. Although many Q3Table functions operate in terms of rows and columns the indexOf() function returns a single integer identifying a particular cell. \target cells \section1 Cells All of a Q3Table's cells are empty when the table is constructed. There are two approaches to populating the table's cells. The first and simplest approach is to use Q3TableItems or Q3TableItem subclasses. The second approach doesn't use Q3TableItems at all which is useful for very large sparse tables but requires you to reimplement a number of functions. We'll look at each approach in turn. To put a string in a cell use setText(). This function will create a new Q3TableItem for the cell if one doesn't already exist, and displays the text in it. By default the table item's widget will be a QLineEdit. A pixmap may be put in a cell with setPixmap(), which also creates a table item if required. A cell may contain \e both a pixmap and text; the pixmap is displayed to the left of the text. Another approach is to construct a Q3TableItem or Q3TableItem subclass, set its properties, then insert it into a cell with setItem(). If you want cells which contain comboboxes use the Q3ComboTableItem class. Similarly if you require cells containing checkboxes use the Q3CheckTableItem class. These table items look and behave just like the combobox or checkbox widgets but consume far less memory. Q3Table takes ownership of its Q3TableItems 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()). In-place editing of the text in Q3TableItems, and the values in Q3ComboTableItems and Q3CheckTableItems works automatically. Cells may be editable or read-only, see Q3TableItem::EditType. If you want fine control over editing see beginEdit() and endEdit(). The contents of a cell can be retrieved as a Q3TableItem using item(), or as a string with text() or as a pixmap (if there is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -