我正在实施QAbstractTableModel
,我想QPushButton
在每行的最后一列中插入一个。当用户单击此按钮时,将显示一个新窗口,其中包含有关此行的更多信息。
你知道如何插入按钮吗?我知道委派系统,但所有示例都只是关于“如何使用组合框编辑颜色”......
我正在实施QAbstractTableModel
,我想QPushButton
在每行的最后一列中插入一个。当用户单击此按钮时,将显示一个新窗口,其中包含有关此行的更多信息。
你知道如何插入按钮吗?我知道委派系统,但所有示例都只是关于“如何使用组合框编辑颜色”......
您可以使用
QPushButton* viewButton = new QPushButton("View");
tableView->setIndexWidget(model->index(counter,2), viewButton);
模型视图架构不是为了将小部件插入到不同的单元格中,但您可以在单元格中绘制按钮。
区别在于:
也就是说,这是如何做到的:
子类QAbstractItemDelegate(或QStyledItemDelegate)并实现该paint()
方法。要绘制按钮控件(或任何其他控件),您需要使用样式或QStylePainter::drawControl()
方法:
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 */);
}
}
由于委托使您处于模型视图领域,因此请使用有关选择和编辑的视图信号来弹出您的信息窗口。
您可以使用setCellWidget(row,column,QWidget*)
在特定单元格中设置小部件。