如何插入QPushButton成的TableView? [英] How to insert QPushButton into TableView?
问题描述
我实现QAbstractTableModel,我想在每行的最后一列中插入QPushButton。当用户点击这个按钮时,显示有关该行的详细信息的新窗口。
I am implementing QAbstractTableModel and I would like to insert QPushButton in the last column of each row. When users clicks on this button, a new window is shown with more information about this row.
你有什么想法如何插入按钮?我知道有关委派制度,但所有的例子仅仅是关于如何与组合框编辑颜色...
Do you have any idea how to insert the button? I know about delegating system but all examples are only about "how to edit color with the combo box"...
推荐答案
模型 - 视图结构不进行插入部件到不同的细胞,但可以绘制细胞内的按钮。
The model-view architecture isn't made to insert widgets into different cells, but you can draw the push button within the cell.
的区别是:
- 这只会是一个按钮的图纸
- 如果没有额外的工作(也许相当多的额外的工作)按钮将不会在鼠标悬停突出
- 在后果#1以上,则不能使用信号和槽
这是说,这里是如何做到这一点:
That said, here's how to do it:
QAbstractItemDelegate (或 QStyledItemDelegate )和实施的 的paint()
方法。要绘制按钮控制(或任何其他控制为此事),你需要使用一个样式或的 QStylePainter :: drawControl()
方式:
Subclass QAbstractItemDelegate (or QStyledItemDelegate) and implement the paint()
method. To draw the pushbutton control (or any other control for that matter) you'll need to use a style or the QStylePainter::drawControl()
method:
class PushButtonDelegate : public QAbstractItemDelegate
{
// TODO: handle public, private, etc.
QAbstractItemView *view;
public PushButtonDelegate(QAbstractItemView* view)
{
this->view = view;
}
void PushButtonDelegate::paint(
QPainter* painter,
const QStyleOptionViewItem & option,
const QModelIndex & index
) const
{
// assuming this delegate is only registered for the correct column/row
QStylePainter stylePainter(view);
// OR: stylePainter(painter->device)
stylePainter->drawControl(QStyle::CE_PushButton, option);
// OR: view->style()->drawControl(QStyle::CE_PushButton, option, painter, view);
// OR: QApplication::style()->drawControl(/* params as above */);
}
}
由于委托让你的模型 - 视图领域里,使用有关选择的看法信号和编辑弹出的信息窗口。
Since the delegate keeps you within the model-view realm, use the views signals about selection and edits to popup your information window.
这篇关于如何插入QPushButton成的TableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!