向QTableview添加按钮

Lea*_*ner 11 c++ qt qt4

我用QTableview和创建了一个表QAbstractTableModel.在其中一个单元格中,我想在该单元格的右上角添加一个帮助按钮.

有没有办法实现这个目标?

Sin*_*all 20

您必须为此实现自己的委托.

在Qt中,除了数据,模型和视图之外,您还有代表.它们提供输入功能,并且还负责在视图中呈现"特殊"项目,这是您所需要的.

Qt doc对这些(关键字:)有很好的报道Model/View programming,你也可以在这里这里找到一些例子.

另外(有点偏离主题,但我想我应该指出这一点),如果你使用普通的QTableWidget,你可以将任何东西插入任何具有它setCellWidget()功能的单元格中.

UPD

这里有一个来自Qt docs 的略微修改的例子(我在Qt中使用模型/视图的东西很糟糕所以不要因为这段代码而难以击败我).它将在右侧的每个单元格中绘制一个按钮,并捕获单元格中的单击事件以检查单击是否在"按钮"上,并做出相应的反应.

可能这不是最好的方法,但正如我所提到的,我对Qt的模型和视图并不太好.

做正确的事情,并允许适当的编辑,你还需要落实createEditor(),setEditorData()setModelData()功能.

要在特定单元格而不是所有单元格中绘制您的东西,只需在paint()函数中添加一个条件(请注意,它将模型索引作为参数,因此您始终可以知道您正在绘制的单元格中,并相应地绘制).


delegate.h:

class MyDelegate : public QItemDelegate
{
    Q_OBJECT

public:
    MyDelegate(QObject *parent = 0);
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};
Run Code Online (Sandbox Code Playgroud)

delegate.cpp:

 #include <QtGui>
 #include "delegate.h"

 MyDelegate::MyDelegate(QObject *parent)
     : QItemDelegate(parent)
 {
 }


 void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 {
     QStyleOptionButton button;
     QRect r = option.rect;//getting the rect of the cell
     int x,y,w,h;
     x = r.left() + r.width() - 30;//the X coordinate
     y = r.top();//the Y coordinate
     w = 30;//button width
     h = 30;//button height
     button.rect = QRect(x,y,w,h);
     button.text = "=^.^=";
     button.state = QStyle::State_Enabled;

     QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
 }

 bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
 {
     if( event->type() == QEvent::MouseButtonRelease )
     {
         QMouseEvent * e = (QMouseEvent *)event;
         int clickX = e->x();
         int clickY = e->y();

         QRect r = option.rect;//getting the rect of the cell
         int x,y,w,h;
         x = r.left() + r.width() - 30;//the X coordinate
         y = r.top();//the Y coordinate
         w = 30;//button width
         h = 30;//button height

         if( clickX > x && clickX < x + w )
             if( clickY > y && clickY < y + h )
             {
                 QDialog * d = new QDialog();
                 d->setGeometry(0,0,100,100);
                 d->show();
             }
     }

     return true;
 }
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "delegate.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QStandardItemModel model(4, 2);
    QTableView tableView;
    tableView.setModel(&model);

    MyDelegate delegate;
    tableView.setItemDelegate(&delegate);

    tableView.horizontalHeader()->setStretchLastSection(true);
    tableView.show();
    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

结果将如下所示:

结果


Ele*_*nic 7

我有一个解决方案,没有任何复杂的重新发明整个油漆过程.我有一个TableView,其中每行都有一个按钮.请注意,在我的情况下,for循环遍历每一行.

QSignalMapper *signalMapper = new QSignalMapper(this);

for( int i=0; i<rows.length(); i++ ) { //replace rows.length with your list or vector which consists of the data for your rows.
     //do something with your data for normal cells...         
     auto item = model->index(i, COLUMN_FOR_WHATEVER_YOU_WANT);
     model->setData(item, QVariant::fromValue(yourObject.getSpecificInformation()));

     //make new button for this row 
     item = model->index(i, COLUMN_BUTTON); 
     QPushButton *cartButton = new QPushButton("Add");
     ui->table_view->setIndexWidget(item, cartButton);

     signalMapper->setMapping(cartButton, i);
     connect(cartButton, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
}
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(doSomething(int)));
Run Code Online (Sandbox Code Playgroud)

然后,您将自动获取用户单击按钮的行的索引.你只需要自己制作插槽:

private SLOTS:
    void doSomething(int row);
Run Code Online (Sandbox Code Playgroud)

如果你有特定的细胞,它会起作用.

注意,我不关心这个例子中的内存泄漏,我不知道如果你更新你的TableView会发生什么...(它工作正常,但它可能不会删除新的按钮指针被创造)


waz*_*zza 5

setIndexWidget为我工作.例:

QPushButton* helpButton = new QPushButton("Help");

tableView->setIndexWidget(model->index(position,COLUMN_NUMBER), helpButton);
Run Code Online (Sandbox Code Playgroud)

如果你只是想添加一个按钮并点击它做一些事情,使用setIndexWidget()添加一个按钮就可以了.我相信我们不需要繁琐的绘画方法或实施代表.