利用QItemDelegate在TableView内加入Button

buttondelegate.h

#ifndef BUTTONDELEGATE_H
#define BUTTONDELEGATE_H

#include
class ButtonDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    ButtonDelegate(QObject * parent=0);
    //createEditor是双击item后创建什么控件
    QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
	//setEditorData是显示控件后 设置里面的值,提供index 自己从model内提取数据
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
	//setmodeldata是确定 (按下回车后)设置model的值,
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;


    ~ButtonDelegate();
};

#endif // BUTTONDELEGATE_H

buttondelegate.cpp

#include "buttondelegate.h"
#include
ButtonDelegate::ButtonDelegate(QObject * parent):QItemDelegate(parent)
{

}
QWidget * ButtonDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index)const
{
    QPushButton * btn=new QPushButton("xx",parent);
    return btn;
}
void ButtonDelegate::setEditorData(QWidget *editor, const QModelIndex &index)const
{

}
void ButtonDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const
{

}
void ButtonDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index)const
{
    editor->setGeometry(option.rect);
}
ButtonDelegate::~ButtonDelegate()
{

}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
private:
    QStandardItemModel * m_pStandardItemModel;
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"buttondelegate.h"
#include
#include
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->m_pStandardItemModel=new QStandardItemModel();
    this->m_pStandardItemModel->setColumnCount(4);
    this->m_pStandardItemModel->setHeaderData(0,Qt::Horizontal,"歌曲名");
    this->m_pStandardItemModel->setHeaderData(1,Qt::Horizontal,"操作");
    this->m_pStandardItemModel->setHeaderData(2,Qt::Horizontal,"歌手");
    this->m_pStandardItemModel->setHeaderData(3,Qt::Horizontal,"时长");
    this->ui->tableView->setModel(this->m_pStandardItemModel);

    ///
    this->m_pStandardItemModel->setItem(0,0,new QStandardItem("可惜没如果"));
    this->m_pStandardItemModel->setItem(0,1,new QStandardItem("播放"));
    this->m_pStandardItemModel->setItem(0,2,new QStandardItem("林俊杰"));
    this->m_pStandardItemModel->setItem(0,3,new QStandardItem("4:16"));

    this->m_pStandardItemModel->setItem(1,0,new QStandardItem("可惜没如果"));
    this->m_pStandardItemModel->setItem(1,1,new QStandardItem("播放"));
    this->m_pStandardItemModel->setItem(1,2,new QStandardItem("林俊杰"));
    this->m_pStandardItemModel->setItem(1,3,new QStandardItem("4:16"));
    //
    this->ui->tableView->setItemDelegateForColumn(2,new ButtonDelegate());


    this->ui->tableView->setIndexWidget(this->ui->tableView->indexAt(QPoint(0,0)),new QPushButton("xx"));
}

MainWindow::~MainWindow()
{
    delete ui;
}

利用QItemDelegate在TableView内加入Button_第1张图片

你可能感兴趣的:(C++Qt框架,C++,Qt框架)