代码之家  ›  专栏  ›  技术社区  ›  izidor

如何将qpushbutton插入TableView?

  •  4
  • izidor  · 技术社区  · 16 年前

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

    你知道怎么插入这个按钮吗?我知道授权系统,但所有的例子都是关于“如何用组合框编辑颜色”…

    3 回复  |  直到 8 年前
        1
  •  3
  •   Kaleb Pederson    16 年前

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

    区别在于:

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

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

        2
  •  5
  •   Jens A. Koch    10 年前

    你可以使用

    QPushButton* viewButton = new QPushButton("View");    
    tableView->setIndexWidget(model->index(counter,2), viewButton);
    
        3
  •  0
  •   Jens A. Koch    10 年前

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