如何将QPushButton插入TableView?

发布于 2024-08-31 14:59:59 字数 152 浏览 9 评论 0原文

我正在实现 QAbstractTableModel,我想在每行的最后一列插入一个 QPushButton。当用户单击此按钮时,将显示一个新窗口,其中包含有关此行​​的更多信息。

您知道如何插入按钮吗?我了解委托系统,但所有示例都只是关于“如何使用组合框编辑颜色”......

I am implementing QAbstractTableModel and I would like to insert a QPushButton in the last column of each row. When users click 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"...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最好是你 2024-09-07 14:59:59

您可以使用

QPushButton* viewButton = new QPushButton("View");    
tableView->setIndexWidget(model->index(counter,2), viewButton);

You can use

QPushButton* viewButton = new QPushButton("View");    
tableView->setIndexWidget(model->index(counter,2), viewButton);
栖竹 2024-09-07 14:59:59

模型视图架构并不是为了将小部件插入到不同的单元格中,但您可以在单元格内绘制按钮。

区别在于:

  1. 它只是一个按钮的绘图
  2. 没有额外的工作(可能需要相当多的额外工作),鼠标悬停时按钮不会突出显示
  3. 由于上面的#1,您不能使用信号和

槽说,这是如何做到这一点:

子类 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 */);
    }
}

由于委托将您保持在模型视图领域内,因此请使用有关选择和编辑的视图信号来弹出信息窗口。

The model-view architecture isn't made to insert widgets into different cells, but you can draw the push button within the cell.

The differences are:

  1. It will only be a drawing of a pushbutton
  2. Without extra work (perhaps quite a bit of extra work) the button won't be highlighted on mouseover
  3. In consequence of #1 above, you can't use signals and slots

That said, here's how to do it:

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.

行至春深 2024-09-07 14:59:59

您可以使用setCellWidget(row,column,QWidget*)在特定单元格中设置小部件。

You can use setCellWidget(row,column,QWidget*) to set a widget in a specific cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文