头文件如下
class lds_sumtable : public QWidget
{
Q_OBJECT
public:
explicit lds_sumtable(QWidget *parent = 0);
void setModel(QAbstractItemModel *model);
QTableView *tableView;//tableview 本身的函数在这里操作
void updateSum(int column, const QString &value);
private slots:
void update_tableview_bar(int min, int max);
void update_lineview_header(int logicalIndex, int oldSize, int newSize);
private:
QTableView *lineView;
class RedModel:public QStandardItemModel{
public:
RedModel(QObject *parent = 0):QStandardItemModel(parent){}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const{
if(role==Qt::TextColorRole){
return QColor("red");
}
return QStandardItemModel::data(index,role);
}
};
RedModel *linemodel;
QAbstractItemModel *parentmodel;
};
源文件如下:
lds_sumtable::lds_sumtable(QWidget *parent) :
QWidget(parent)
{
//tableView
tableView = new QTableView(this);
tableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//lineEdit
lineView = new QTableView(this);
lineView->setEditTriggers(QAbstractItemView::NoEditTriggers);
lineView->horizontalHeader()->hide();
lineView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
lineView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
lineView->setFixedHeight(30);
lineView->setShowGrid(false);
//layout
QGridLayout *gridLayout = new QGridLayout();
QVBoxLayout *verticalLayout = new QVBoxLayout();
QHBoxLayout *hblayout=new QHBoxLayout;
verticalLayout->addWidget(tableView);
verticalLayout->addWidget(lineView);
verticalLayout->addLayout(hblayout);
gridLayout->addLayout(verticalLayout, 0, 0, 1, 1);
gridLayout->addWidget(tableView->verticalScrollBar(), 0, 1, 1, 1);
gridLayout->addWidget(tableView->horizontalScrollBar(), 1, 0, 1, 1);
verticalLayout->setSpacing(0);
verticalLayout->setContentsMargins(0, 0, 0, 0);
gridLayout->setSpacing(0);
gridLayout->setContentsMargins(0, 0, 0, 0);
hblayout->setSpacing(0);
hblayout->setContentsMargins(0,0,0,0);
setLayout(gridLayout);
//lineView
linemodel=new RedModel;
lineView->setModel(linemodel);
//这里是 两个table的同步section 宽度
connect(tableView->horizontalHeader(),SIGNAL(sectionResized(int,int,int)),this,SLOT(update_lineview_header(int,int,int)),Qt::QueuedConnection);
//这里是 两个table的水平滚动条 同步
connect(tableView->horizontalScrollBar(),SIGNAL(valueChanged(int)),lineView->horizontalScrollBar(),SLOT(setValue(int)),Qt::QueuedConnection);
//这里是 同步水平滚动条与数据内容同步
connect(tableView->horizontalScrollBar(),SIGNAL(rangeChanged(int,int)),this,SLOT(update_tableview_bar(int,int)),Qt::QueuedConnection);
//这里是 垂直水平滚动条与数据内容同步
connect(tableView->verticalScrollBar(),SIGNAL(rangeChanged(int,int)),this,SLOT(update_tableview_bar(int,int)),Qt::QueuedConnection);
}
void lds_sumtable::setModel(QAbstractItemModel *model)
{
//table setmodel
parentmodel=model;
tableView->setModel(model);
//更新lineview的数据,只保留一行
linemodel->removeRows(0, linemodel->rowCount());
QList<QStandardItem *> items;
for(int c=0,c_count=model->columnCount();c<c_count;c++){
QStandardItem *item=new QStandardItem("");
if(c==0){
item->setText(tr("合计"));
}
items<<item;
}
linemodel->appendRow(items);
lineView->verticalHeader()->setModel(linemodel);
}
void lds_sumtable::update_tableview_bar( int min, int max)
{
QScrollBar *bar=qobject_cast<QScrollBar *>(sender());
if(!bar) return;
bar->setVisible(max>0?true:false);
}
void lds_sumtable::update_lineview_header(int logicalIndex, int oldSize, int newSize)
{
Q_UNUSED(oldSize)
lineView->horizontalHeader()->resizeSection(logicalIndex, newSize);
}
void lds_sumtable::updateSum(int column, const QString &value)
{
if(linemodel->rowCount()<=0
||linemodel->columnCount()<=column) {
qDebug() << tr("超出范围");
return;
}
linemodel->item(0, column)->setText(value);
//更新linemodel的 垂直labels,使其和table的垂直header的宽度一致
linemodel->setVerticalHeaderLabels(QStringList() << tr("%1").arg(parentmodel->rowCount()));
}
说明:
1.这次采用的方法是直接把tableview的滚动条放到grid布局里,通过rangeChanged信号,同步更新滚动条的随内容变换而实现或隐藏
2.采用两个tableview实现sum功能,这里对垂直header setmodel,并且即时更新
setVerticalHeaderLabels的第一行的值,以实现超过10后的,垂直header不平衡所致