添加按钮到QTableview [英] Adding button to QTableview

查看:2299
本文介绍了添加按钮到QTableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created one table by using QTableview and QAbstractTableModel . In one of the cell i want to add one help button (right corner of that cell ).

有没有办法实现上面的一个? / p>

Is there any way to achieve the above one ?

推荐答案

你必须实现自己的委托。

You will have to implement your own delegate for that.

在Qt中,除了数据,模型和视图外,还有您的代表。它们提供输入功能,他们还负责在视图中渲染特殊项目,这是您需要的。

In Qt, aside from the Data, the Model and the View, you have your Delegates. They provide input capabilities, and they are also responsible for rendering "special" items in the View, which is what you need.

Qt doc对这些关键字(关键字: Model / View programming )有很好的涵盖,一些示例此处此处

Qt doc has a good coverage on those (keywords: Model/View programming), and you can also find some examples here and here.

还有一点 - 如果你使用一个普通的 QTableWidget ,你可以插入任何东西到任何单元格与它的 setCellWidget )函数。

Also (a little off-topic, but I think I should point this out), if you use an ordinary QTableWidget, you can insert anything into any cell with it's setCellWidget() function.

UPD

示例从Qt docs(我吮吸模型/查看在Qt的东西,所以不要打我硬的这个代码)。它将在右侧的每个单元格中绘制一个按钮,并捕获单元格中的点击事件,以检查点击是否在按钮上,并相应地做出反应。

here is a slightly modified example from Qt docs (I suck with model/view stuff in Qt so don't beat me hard for this code). It will draw a button in each cell on the right, and catch the click events in cells to check if the click was on the "button", and react accordingly.

也许这不是最好的方法,但正如我所提到的,我不太好与Qt的模型和意见。

Probably this is not the best way to do it, but as I mentioned, I'm not too good with Qt's models and views.

要做正确的事情并允许正确的编辑,您还需要实现 createEditor() setEditorData() setModelData()

To do things right and allow proper editing, you will need to also implement createEditor(), setEditorData() and setModelData() functions.

只需在 paint()函数中添加一个条件(注意,它将模型索引作为参数,因此您可以始终知道

To draw your stuff in a specific cell instead of all cells, just add a condition into the paint() function (note that it gets the model index as an argument, so you can always know in what cell you are painting, and paint accordingly).

delegate.h:

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






delegate.cpp:


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();
             }
     }
 }






main.cpp


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();
}

结果将如下所示:

这篇关于添加按钮到QTableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆