qtableview不显示表头和数据

avrilers 2012-05-09 09:06:16

    
offsetsTableModel offsetsModel;
QTableView *offsetsTableView=new QTableView();[code=C/C++]

offsetsTableView->setShowGrid(true);
offsetsTableView->setModel(&offsetsModel);
offsetsTableView->setAlternatingRowColors(true);


QLabel *aa= new QLabel(tr("qwert"));
QVBoxLayout *aaa= new QVBoxLayout;
aaa->addWidget(aa);
aaa->addWidget(offsetsTableView);
this->setLayout(aaa);[/code]


子类化QAbstractTableModel
.cpp


#ifndef OFFSETSTABLEMODEL_H
#define OFFSETSTABLEMODEL_H
#include <QAbstractTableModel>
class offsetsTableModel:public QAbstractTableModel
{
public:
offsetsTableModel();

int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
private:
QVector<QVector<double> > offsets;
};

#endif // OFFSETSTABLEMODEL_H


.h


#include "shipwindow.h"
#include <QtGui>
#include "mainwindow.h"
#include <QDebug>
#include <QVector>
#include <offsetstablemodel.h>

extern QVector<QVector<double> > xingzhi;//外部全局变量,在其他文件已读入数据

offsetsTableModel::offsetsTableModel()
{
// qDebug()<<xingzhi;
}

int offsetsTableModel::rowCount(const QModelIndex &parent) const
{
// qDebug()<<(xingzhi.size()-1); 有输出
return (xingzhi.size()-1);
}

int offsetsTableModel::columnCount(const QModelIndex &parent) const
{
// qDebug()<<(xingzhi.at(0).size()-1); 有输出
return (xingzhi.at(0).size()-1);
}

QVariant offsetsTableModel::data(const QModelIndex &index, int role) const
{
qDebug()<<"w"; // 没输出
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignRight | Qt::AlignVCenter);
} else if (role == Qt::DisplayRole) {
// qDebug()<<"wang";
double tableViewOffsets= xingzhi.at(index.row()+1).at(index.column()+1);
qDebug()<<tableViewOffsets;
return tableViewOffsets;
// return QString("%1").arg(tableViewOffsets, 0, 'f', 4);
}
return QVariant();
}
//section和index.row()都是从0开始
QVariant offsetsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role!=Qt::DisplayRole)
return QVariant();
if(orientation==Qt::Horizontal)
{
return tr("%1m水线").arg(xingzhi.at(0).at(section+1));
// return xingzhi.at(0).at(section+1);
}
if(orientation==Qt::Vertical)
{
return tr("%1站").arg(xingzhi.at(section+1).at(0));
}
return QVariant();
}


程序能运行,也能出现tableview,但是数据不显示,表头不显示,也没有网格
...全文
1333 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
NewEdition 2012-07-30
  • 打赏
  • 举报
回复
找到答案了,QTableWidget必须设定行数,才能正常显示

ui->VideoTableWidget->setColumnCount(5);
ui->VideoTableWidget->setRowCount(5);
NewEdition 2012-07-30
  • 打赏
  • 举报
回复
我也是这问题,数据从库里取出来了,绑定到Qtablewidget后不显示数据 求解
  • 打赏
  • 举报
回复
tableview 直接用Qtablewidget 好了
  • 打赏
  • 举报
回复
在构造函数里设置tableWidget 的行数 列数

16,798

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧