📄 q3table.cpp
字号:
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. It is important to ensure that your custom widget can accept the keyboard focus, so that the user can use the tab key to navigate the table as normal. Therefore, if the widget returned by createEditor() does not itself accept the keyboard focus, it is necessary to nominate a child widget to do so on its behalf. For example, a QHBox with two child QLineEdit widgets may use one of them to accept the keyboard focus: \code QWidget* MyTableItem::createEditor() const { QHBox* hbox = new QHBox(table()->viewport()); hbox->setFocusProxy(new QLineEdit(hbox)); new QLineEdit(hbox); return hbox; } \endcode By default, table items may be replaced by new Q3TableItems during the lifetime of a Q3Table. Therefore, if you create your own subclass of Q3TableItem, and you want to ensure that this does not happen, you must call setReplaceable(false) in the constructor of your subclass. \img qtableitems.png Table Items \sa Q3CheckTableItem Q3ComboTableItem*//*! \fn Q3Table *Q3TableItem::table() const Returns the Q3Table the table item belongs to. \sa Q3Table::setItem() Q3TableItem()*//*! \enum Q3TableItem::EditType \target wheneditable 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. \value Always The cell always \e looks editable. 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 \c Always looks misaligned you could reimplement createEditor() for these items. \value WhenCurrent The cell \e looks editable only when it has keyboard focus (see Q3Table::setCurrentCell()). \value OnTyping The cell \e looks editable only when the user types in it or double-clicks it. It resembles the \c WhenCurrent functionality but is, perhaps, nicer. The \c OnTyping edit type is the default when Q3TableItem objects are created by the convenience functions Q3Table::setText() and Q3Table::setPixmap(). \value Never The cell is not editable. The cell is actually editable only if Q3Table::isRowReadOnly() is false for its row, Q3Table::isColumnReadOnly() is false for its column, and Q3Table::isReadOnly() is false. Q3ComboTableItems 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. Q3ComboTableItems may be interacted with only if they are editable in accordance with their EditType as described above.*//*! Creates a table item that is a child of table \a table with no text. The item has the \l EditType \a et. 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 Q3Table::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.*/Q3TableItem::Q3TableItem(Q3Table *table, EditType et) : txt(), pix(), t(table), edType(et), wordwrap(false), tcha(true), rw(-1), cl(-1), rowspan(1), colspan(1){ enabled = true;}/*! Creates a table item that is a child of table \a table with text \a text. The item has the \l EditType \a et. 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 Q3Table::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.*/Q3TableItem::Q3TableItem(Q3Table *table, EditType et, const QString &text) : txt(text), pix(), t(table), edType(et), wordwrap(false), tcha(true), rw(-1), cl(-1), rowspan(1), colspan(1){ enabled = true;}/*! Creates a table item that is a child of table \a table with text \a text and pixmap \a p. The item has the \l EditType \a et. 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 Q3Table::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.*/Q3TableItem::Q3TableItem(Q3Table *table, EditType et, const QString &text, const QPixmap &p) : txt(text), pix(p), t(table), edType(et), wordwrap(false), tcha(true), rw(-1), cl(-1), rowspan(1), colspan(1){ enabled = true;}/*! The destructor deletes this item and frees all allocated resources. 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.*/Q3TableItem::~Q3TableItem(){ if (table()) table()->takeItem(this);}int Q3TableItem::RTTI = 0;/*! Returns the Run Time Type Identification value for this table item which for Q3TableItems is 0. When you create subclasses based on Q3TableItem 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. \sa Q3CheckTableItem::rtti() Q3ComboTableItem::rtti()*/int Q3TableItem::rtti() const{ return RTTI;}/*! Returns the table item's pixmap or a null pixmap if no pixmap has been set. \sa setPixmap() text()*/QPixmap Q3TableItem::pixmap() const{ return pix;}/*! Returns the text of the table item or an empty string if there is no text. To ensure that the current value of the editor is returned, setContentFromEditor() is called: \list 1 \i if the editMode() is \c Always, or \i if editMode() is \e not \c Always but the editor of the cell is active and the editor is not a QLineEdit. \endlist This means that text() returns the original text value of the item if the editor is a line edit, until the user commits an edit (e.g. by pressing Enter or Tab) in which case the new text is returned. For other editors (e.g. a combobox) setContentFromEditor() is always called so the currently display value is the one returned. \sa setText() pixmap()*/QString Q3TableItem::text() const{ QWidget *w = table()->cellWidget(rw, cl); if (w && (edType == Always || rtti() == Q3ComboTableItem::RTTI || rtti() == Q3CheckTableItem::RTTI)) ((Q3TableItem*)this)->setContentFromEditor(w); return txt;}/*! Sets pixmap \a p to be this item's pixmap. Note that setPixmap() does not update the cell the table item belongs to. Use Q3Table::updateCell() to repaint the cell's contents. For \l{Q3ComboTableItem}s and \l{Q3CheckTableItem}s this function has no visible effect. \sa Q3Table::setPixmap() pixmap() setText()*/void Q3TableItem::setPixmap(const QPixmap &p){ pix = p;}/*! Changes the table item's text to \a str. Note that setText() does not update the cell the table item belongs to. Use Q3Table::updateCell() to repaint the cell's contents. \sa Q3Table::setText() text() setPixmap() Q3Table::updateCell()*/void Q3TableItem::setText(const QString &str){ txt = str;}/*! This virtual function is used to paint the contents of an item using the painter \a p in the rectangular area \a cr using the color group \a cg. If \a 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. The painter passed to this function is translated so that 0, 0 is the top-left corner of the item that is being painted. Note that the painter is not clipped by default in order to get maximum efficiency. If you want clipping, use \code p->setClipRect(table()->cellRect(row, col), QPainter::ClipPainter); //... your drawing code p->setClipping(false); \endcode*/void Q3TableItem::paint(QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected){ p->fillRect(0, 0, cr.width(), cr.height(), selected ? cg.brush(QColorGroup::Highlight) : cg.brush(QColorGroup::Base)); int w = cr.width(); int h = cr.height(); int x = 0; if (!pix.isNull()) { p->drawPixmap(0, (cr.height() - pix.height()) / 2, pix); x = pix.width() + 2; } if (selected) p->setPen(cg.highlightedText()); else p->setPen(cg.text()); p->drawText(x + 2, 0, w - x - 4, h, wordwrap ? (alignment() | WordBreak) : alignment(), text());}/*!This virtual function creates an editor which the user caninteract with to edit the cell's contents. The defaultimplementation creates a QLineEdit.If the function returns 0, the cell is read-only.The returned widget should preferably be invisible, ideally withQ3Table::viewport() as parent.If you reimplement this function you'll almost certainly need toreimplement setContentFromEditor(), and may need to reimplementsizeHint().\sa Q3Table::createEditor() setContentFromEditor() Q3Table::viewport() setReplaceable()*/QWidget *Q3TableItem::createEditor() const{ QLineEdit *e = new QLineEdit(table()->viewport(), "qt_tableeditor"); e->setFrame(false); e->setText(text()); return e;}/*!Whenever the content of a cell has been edited by the editor \a w,Q3Table calls this virtual function to copy the new values into theQ3TableItem.If you reimplement createEditor() and return something that is nota QLineEdit you will need to reimplement this function.\sa Q3Table::setCellContentFromEditor()*/void Q3TableItem::setContentFromEditor(QWidget *w){ QLineEdit *le = ::qobject_cast<QLineEdit*>(w); if (le) { QString input = le->text(); if (le->validator()) le->validator()->fixup(input); setText(input); }}/*! The alignment function returns how the text contents of the cell are aligned when drawn. The default implementation aligns numbers to the right and any other text to the left. \sa Qt::Alignment*/// ed: For consistency reasons a setAlignment() should be provided// as well.int Q3TableItem::alignment() const{ bool num; bool ok1 = false, ok2 = false; (void)text().toInt(&ok1); if (!ok1) (void)text().toDouble(&ok2); // ### should be .-aligned num = ok1 || ok2; return (num ? AlignRight : AlignLeft) | AlignVCenter;}/*! If \a b is true, the cell's text will be wrapped over multiple lines, when necessary, to fit the width of the cell; otherwise the text will be written as a single line. \sa wordWrap() Q3Table::adjustColumn() Q3Table::setColumnStretchable()*/void Q3TableItem::setWordWrap(bool b){ wordwrap = b;}/*! Returns true if word wrap is enabled for the cell; otherwise returns false. \sa setWordWrap()*/bool Q3TableItem::wordWrap() const{ return wordwrap;}/*! \internal */void Q3TableItem::updateEditor(int oldRow, int oldCol){ if (edType != Always) return; if (oldRow != -1 && oldCol != -1) table()->clearCellWidget(oldRow, oldCol); if (rw != -1 && cl != -1) table()->setCellWidget(rw, cl, createEditor());}/*! Returns the table item's edit type. This is set when the table item is constructed. \sa EditType Q3TableItem()*/Q3TableItem::EditType Q3TableItem::editType() const{ return edType;}/*! If \a b is true it is acceptable to replace the contents of the cell with the contents of another Q3TableItem. If \a b is false the contents of the cell may not be replaced by the contents of another table item. Table items that span more than one cell may not have their contents replaced by another table item. (This differs from \l EditType because EditType is concerned with whether the \e user is able to change the contents of a cell.) \sa isReplaceable()*/void Q3TableItem::setReplaceable(bool b){ tcha = b;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -