搞了两天了,都没出来 -- 在tableview中 添加一列 复选框,不知道怎么实现,请达人帮助!
RT,谢谢了!在论坛找了 很多关于此类的帖子,都没说具体(是本人水平有限,Qt初学者),请大虾帮忙……
在线等…………………… --------------------编程问答-------------------- 这个, 应该很简单啊! 你看看class QStandardItem
void QStandardItem::setCheckable ( bool checkable )
Sets whether the item is user-checkable. If checkable is true, the item can be checked by the user; otherwise, the user cannot check the item.
The item delegate will render a checkable item with a check box next to the item's text.
See also isCheckable(), setCheckState(), and setTristate().
另外,如果你要在tableView里边用ComboBox, CheckBox, 甚至自己写的widget可以用delegate,这个例子程序里边有的 --------------------编程问答-------------------- 能给个例程么,不知如何使用;
我用的时tableview + qsqltablemodel 谢谢 --------------------编程问答-------------------- 自己顶一下,请大虾相助啊…… --------------------编程问答-------------------- 重写qsqltablemodel的flags在你的哪例里返回Qt::ItemIsUserCheckable --------------------编程问答-------------------- #ifndef MYMODEL_H
#define MYMODEL_H
#include <QSqlTableModel>
class myModel : public QSqlTableModel
{
Q_OBJECT
public:
explicit myModel(QObject *parent = 0);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
private:
int aColWithCheckbox;
};
#endif // MYMODEL_H
#include "mymodel.h"
myModel::myModel(QObject *parent = 0)
{
aColWithCheckbox = 2;
}
Qt::ItemFlags myModel::flags(const QModelIndex &index) const {
Qt::ItemFlags flags = QSqlTableModel::flags(index);
if (index.column() == aColWithCheckbox)
flags |= Qt::ItemIsUserCheckable;
else
flags |= Qt::ItemIsEditable;
return flags;
}
QVariant myModel::data(const QModelIndex &index, int role) const {
QVariant value = QSqlTableModel::data(index, role);
if (role == Qt::CheckStateRole && index.column() == aColWithCheckbox)
return (QSqlQueryModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
else
return value;
}
--------------------编程问答-------------------- 复选框能正常显示了,但是 问题又来了……
复选框 不响应 鼠标 的点击事件 ,请问又如何解决呢,谢谢…… --------------------编程问答-------------------- 继承QStandardItemDelegate --------------------编程问答-------------------- 大约代码是这样,你就改下就行了
Qt::ItemFlags CTableView::flags( const QModelIndex & index ) const
{
if (index.column() == 0)
return Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
return Qt::ItemIsSelectable | Qt::ItemIsEnabled ;
}
QVariant CTableView::data( const QModelIndex & index, int role /*= Qt::DisplayRole */ ) const
{
if (0 == index.column())
{
if (Qt::CheckStateRole == role)
return checkVec.at(index.row())?Qt::Checked : Qt::Unchecked;
}
return QVariant();
}
bool CTableView::setData( const QModelIndex & index, const QVariant & value, int role /*= Qt::EditRole */ )
{
qDebug("setData");
if (0 == index.column() && Qt::CheckStateRole == role)
{
checkVec[index.row()] = value.toBool();
return true;
}
return false;
}
--------------------编程问答--------------------
该类 是继承自qsqltablemodel 么?
代码里面的 checkVec 是数组么?
以数据的行数 初始化的么?
有好地方 没看明白呢?
能给个 完整代码么? 谢谢了 --------------------编程问答--------------------
按照我的理解试了一下 你的代码,出现的情况是这样的: 复选框都能正常显示
如果设置 没有数据的列 比如 0列 在数据库的某表中 没有数据, 复选框能正常使用 但是 点击多次后便出现错误,程序也莫名其妙的结束运行,编译器提示错误:
Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly
如果设置在有数据的列,则没有任何问题 但 复选框又不能 响应 鼠标点击 事件了……
求解 --------------------编程问答--------------------
晕..你改下啊;checkVec就是你数据库里存放是checked还是unchecked的数据啊一般是存0或1嘛
你在flags返回Qt::ItemIsEditable可编辑
然后在setData()里把你是选中还是不选中写到数据库里,我那个heckVec是模拟你的数据库了里checked数据了
你说数据库中没有这些数据?这肯定有的啊否则你怎么知道是选中还是不选中嘛
--------------------编程问答-------------------- 是继承qsqltablemodel ,我那类里写错了名字~呵呵 --------------------编程问答-------------------- 3Q,我修改了一下 算法,哈哈…… ,已经实现了…… --------------------编程问答--------------------
按照我的理解试了一下 你的代码,出现的情况是这样的: 复选框都能正常显示
如果设置 没有数据的列 比如 0列 在数据库的某表中 没有数据, 复选框能正常使用 但是 点击多次后便出现错误,程序也莫名其妙的结束运行,编译器提示错误:
Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly
如果设置在有数据的列,则没有任何问题 但 复选框又不能 响应 鼠标点击 事件了……
求解
最终鼠标的事件响应是怎么做的呢? --------------------编程问答-------------------- 1,直接添加控件
for(int i = 0; i < tableCount; ++i) {
const QString table = tableNames.at(i);
{
QWidget *widget = new QWidget(ui->tableWidget_table);
QHBoxLayout *hLayout = new QHBoxLayout();
QCheckBox *checkBox = new QCheckBox();
hLayout->addWidget(checkBox);
hLayout->setMargin(0); // 必须添加, 否则CheckBox不能正常显示
hLayout->setAlignment(checkBox, Qt::AlignCenter);
widget->setLayout(hLayout);
ui->tableWidget_table->setCellWidget(i, TABLE_CHECKED_COLUMN, widget);
}
2,使用代理 实例里面有的
补充:移动开发 , Qt