在表格中加载按钮,但是点击这一列任意一个按钮,自己弹出一个小窗口,按钮在这里面显示,换成qcombox就在表格里显示正常,各位大侠帮忙看看 另外,按钮点击事件怎么和表格联系在一起,即点击哪一行的按钮,删除哪一行。
//删除按钮代理类
class DelBtnDelegate:public QItemDelegate
{
Q_OBJECT
public:
DelBtnDelegate(QObject *parent = 0): QItemDelegate(parent) { }
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
// QComboBox *editor = new QComboBox(parent);//换成qcombox正常
// editor->addItem("Female");
QPushButton *editor = new QPushButton("delete");
return editor;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const
{
cout<<"set editor"<<endl;
}
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
cout<<"set model data"<<endl;
}
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
cout<<"update "<<endl;
editor->setGeometry(option.rect);
}
};
//构造函数
ften_Client::Often_Client(QWidget *parent) :
QDialog(parent),
ui(new Ui::Often_Client)
{
ui->setupUi(this);
DelBtnDelegate *pbtn = new DelBtnDelegate;
ui->tableWidget->setItemDelegateForColumn(1,pbtn);//第二列为按钮
}
------解决方案--------------------
setEditorData()和setModelData 就是两者关联的函数
通过参数QModelIndex &index进行关联
------解决方案--------------------