QTableView 的使用
设置
example_model =new ExampleModel;(自定义的数据层model)
QStringList headerList;
headerList <<"名字"<<"序号"<<"性别"<<"年龄"<<"成绩";
example_model->setHorizontalHeaderLabels(headerList);
//表头
exampleTableView = new QTableView;
exampleTableView->setModel(example_model);
(model,数据层)
//把表格的背景调成相间色
exampleTableView->setAlterna ingRowColors( true );
);
exampleTableView->verticalHeader()->setVisible( false ); //垂直表头不显示
model 定义
class ExampleModel : public QAbstractTableModel //继承QAbstractTableModel
{
public:
ExampleModel(QObject *parent = 0);
~ExampleModel();
//重写的五个函数
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
void setHorizontalHeaderLabels(QStringList headerList);//设置表头
signals:
private:
int colNumberWithCheckBox_; //有QCheckBox的列,为0 表示第0列
QStringList headerList_; //表头
};
//返回列数,根据水平表头
int ExampleModel::columnCount(const QModelIndex & /* parent */) const
{
return ( !headerList_.size())?5:headerList_.size();
// return 5;
}
//获取当前关键字
QVariant ExampleModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
if(section<headerList_.size())
{
return headerList_[section];
}
}
return QVariant();
}
//设置表头
void ExampleModel::setHorizontalHeaderLabels(QStringList headerList)
{
headerList_ = headerList;
}
//显示的数据
QVariant ExampleModel::data( const QModelIndex &index, int role ) const
{
if (!index.isValid())
return QVariant();
int row = index.row();
int col = index.column();
switch(role)
{
case Qt::DisplayRole:
if (col == 1)
{
return QString::number(index.row()+1);
}
break;
// case Qt::FontRole:
// if ( col == 0) //change font only for cell(X,0)
// {
// QFont boldFont;
// boldFont.setBold(true);
// return boldFont;
// }
// break;
case Qt::ForegroundRole:
if ( col == 3) //第3列
{
QBrush redForground(Qt::red);
return redForground;
}
break;
case Qt::BackgroundRole:
if ((col == 1 )) //change background only for cell(X,1)
{
QBrush blueBackground(Qt::blue);
return blueBackground;
break;
case Qt::DecorationRole:
if (row == 0 )
{ //设置图片
return QPixmap(":images/current_pic.png");
}
break;
case Qt::TextAlignmentRole:
return Qt::AlignCenter; //字体中间对齐
break;
case Qt::CheckStateRole:
if (col == colNumberWithCheckBox_)//add a checkbox to cell(row,col)
{
return Qt::isChecked? Qt::Checked : Qt::Unchecked;
}
break;
}
return QVariant();
}
//为了设置QCheckBox而重写的
Qt::ItemFlags ExampleModel::flags( const QModelIndex &index ) const
{
if (!index.isValid())
return 0;
if (index.column() == colNumberWithCheckBox_)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
//若是想编辑可返回 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
//设置数据,根据QChecoBox的状态
bool ExampleModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if(!index.isValid())
return false;
if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox_)
{
}
return true;
}
注意
#include<QTableView>
#include<QHeaderView>
行为
单击
单击会发出信号clicked(QModelIndex),需要自定义 槽函数:如 exampleTableViewClicked(QModelIndex)
connect(exampleTableView,
SIGNAL(clicked(QModelIndex)),
this,
SLOT(exampleTableViewClicked(QModelIndex)));
在槽函数(exampleTableViewClicked)中判断点击的是哪个单元格,如:
index.column() == 0 && index.row() == 0 为单元格(0,0)
双击
单击会发出信号 doubleClicked(QModelIndex),,需要自定义 槽函数:如 exampleTableViewDoubleClicked(QModelIndex) connect(exampleTableView,
SIGNAL(doubleClicked(QModelIndex)),
this,
SLOT(exampleTableViewDoubleClicked(QModelIndex)));
右键菜单
//如果你用在QTableView中使用右键菜单,需启用该属性
exampleTableView->setContextMenuPolicy(Qt::CustomContextMenu);
连接信号槽,槽函数ShowMouseRightButton是自定义的
connect( exampleTableView,
SIGNAL(customContextMenuRequested(const QPoint)),
this,
SLOT(ShowMouseRightButton(const QPoint)));
author: wsh
email: tongzhuodenilove@163.com