q3table.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 2,173 行 · 第 1/5 页
CPP
2,173 行
QStyle::SC_ComboBoxEditField, fakeCombo); int align = alignment(); // alignment() changes entries p->drawText(textR, wordWrap() ? (align | Qt::WordBreak) : align, entries.value(current)); p->restore();}/*! Sets the list item \a i to be the combo table item's current list item. \sa currentItem()*/void Q3ComboTableItem::setCurrentItem(int i){ QWidget *w = table()->cellWidget(row(), col()); Q3ComboBox *cb = ::qobject_cast<Q3ComboBox*>(w); if (cb) { cb->setCurrentItem(i); current = cb->currentItem(); setText(cb->currentText()); } else { if (i < 0 || i >= entries.count()) return; current = i; setText(entries.at(i)); table()->updateCell(row(), col()); }}/*! \overload Sets the list item whose text is \a s to be the combo table item's current list item. Does nothing if no list item has the text \a s. \sa currentItem()*/void Q3ComboTableItem::setCurrentItem(const QString &s){ int i = entries.findIndex(s); if (i != -1) setCurrentItem(i);}/*! Returns the index of the combo table item's current list item. \sa setCurrentItem()*/int Q3ComboTableItem::currentItem() const{ QWidget *w = table()->cellWidget(row(), col()); Q3ComboBox *cb = ::qobject_cast<Q3ComboBox*>(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()); Q3ComboBox *cb = ::qobject_cast<Q3ComboBox*>(w); if (cb) return cb->currentText(); return entries.value(current);}/*! Returns the total number of list items in the combo table item.*/int Q3ComboTableItem::count() const{ QWidget *w = table()->cellWidget(row(), col()); Q3ComboBox *cb = ::qobject_cast<Q3ComboBox*>(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()); Q3ComboBox *cb = ::qobject_cast<Q3ComboBox*>(w); if (cb) return cb->text(i); return entries.value(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());}/*! \fn QString Q3ComboTableItem::text() const Returns the text of the table item or an empty string if there is no text. \sa Q3TableItem::text()*//*! \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::Window, 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 overridden 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().
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?