QTableView 和空标题和列
我有一个 QTableView 对象,当我从 showIndicators 切换到 showIndiValues 时,我得到了这个丑陋的空标题行。问题出在哪里?
void DBTableView::showIndicators()
{
enum {
indicators_Id = 0,
indicators_Name = 1,
indicators_CondName = 2,
indicators_MeasureName = 3,
indicators_Description = 4,
indicators_Responsible = 5
};
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT * FROM viewIndicators");
model->setHeaderData(indicators_Id, Qt::Horizontal, tr("ID"));
model->setHeaderData(indicators_Name, Qt::Horizontal, tr("Name"));
model->setHeaderData(indicators_CondName, Qt::Horizontal, tr("Condition Name"));
model->setHeaderData(indicators_MeasureName, Qt::Horizontal, tr("Measure"));
model->setHeaderData(indicators_Description, Qt::Horizontal, tr("Description"));
model->setHeaderData(indicators_Responsible, Qt::Horizontal, tr("Responsible"));
/* TableView part */
setModel(model);
hideColumn(indicators_Id);
QHeaderView *horizontalHeader = this->horizontalHeader();
horizontalHeader->setStretchLastSection(true);
QHeaderView *verticalHeader = this->verticalHeader();
verticalHeader->setStretchLastSection(true);
setSelectionBehavior(QAbstractItemView::SelectRows);
setColumnWidth(indicators_Id, 0);
setColumnWidth(indicators_Name, 400);
setColumnWidth(indicators_CondName, 120);
setColumnWidth(indicators_MeasureName, 120);
setColumnWidth(indicators_Description, 400);
createIndicatorContextMenu();
show();
}
void DBTableView::showIndiValues()
{
enum {
iv_Id = 0,
indicatorId = 1,
indicatorName = 2,
intervalName = 3,
iv_DataValue = 4,
iv_DataDate = 5,
iv_DataBirth = 6
};
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT * FROM viewIndiValues");
model->setHeaderData(iv_Id, Qt::Horizontal, tr("ID"));
model->setHeaderData(indicatorId, Qt::Horizontal, tr("Indicator ID"));
model->setHeaderData(indicatorName, Qt::Horizontal, tr("Indicator name"));
model->setHeaderData(intervalName, Qt::Horizontal, tr("Interval name"));
model->setHeaderData(iv_DataValue, Qt::Horizontal, tr("Value"));
model->setHeaderData(iv_DataDate, Qt::Horizontal, tr("Date"));
model->setHeaderData(iv_DataBirth, Qt::Horizontal, tr("Creation date"));
setModel(model);
hideColumn(iv_Id);
hideColumn(indicatorId);
QHeaderView *horizontalHeader = this->horizontalHeader();
horizontalHeader->setStretchLastSection(false);
QHeaderView *verticalHeader = this->verticalHeader();
verticalHeader->setStretchLastSection(false);
setSelectionBehavior(QAbstractItemView::SelectItems);
setColumnWidth(indicatorName, 400);
setColumnWidth(intervalName, 200);
setColumnWidth(iv_DataValue, 200);
setColumnWidth(iv_DataDate, 120);
setColumnWidth(iv_DataBirth, 120);
createIndiValueContextMenu();
show();
}
I have one QTableView object and when I am switching from showIndicators to showIndiValues I am getting this ugly empty header line. Where is the problem?
void DBTableView::showIndicators()
{
enum {
indicators_Id = 0,
indicators_Name = 1,
indicators_CondName = 2,
indicators_MeasureName = 3,
indicators_Description = 4,
indicators_Responsible = 5
};
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT * FROM viewIndicators");
model->setHeaderData(indicators_Id, Qt::Horizontal, tr("ID"));
model->setHeaderData(indicators_Name, Qt::Horizontal, tr("Name"));
model->setHeaderData(indicators_CondName, Qt::Horizontal, tr("Condition Name"));
model->setHeaderData(indicators_MeasureName, Qt::Horizontal, tr("Measure"));
model->setHeaderData(indicators_Description, Qt::Horizontal, tr("Description"));
model->setHeaderData(indicators_Responsible, Qt::Horizontal, tr("Responsible"));
/* TableView part */
setModel(model);
hideColumn(indicators_Id);
QHeaderView *horizontalHeader = this->horizontalHeader();
horizontalHeader->setStretchLastSection(true);
QHeaderView *verticalHeader = this->verticalHeader();
verticalHeader->setStretchLastSection(true);
setSelectionBehavior(QAbstractItemView::SelectRows);
setColumnWidth(indicators_Id, 0);
setColumnWidth(indicators_Name, 400);
setColumnWidth(indicators_CondName, 120);
setColumnWidth(indicators_MeasureName, 120);
setColumnWidth(indicators_Description, 400);
createIndicatorContextMenu();
show();
}
void DBTableView::showIndiValues()
{
enum {
iv_Id = 0,
indicatorId = 1,
indicatorName = 2,
intervalName = 3,
iv_DataValue = 4,
iv_DataDate = 5,
iv_DataBirth = 6
};
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT * FROM viewIndiValues");
model->setHeaderData(iv_Id, Qt::Horizontal, tr("ID"));
model->setHeaderData(indicatorId, Qt::Horizontal, tr("Indicator ID"));
model->setHeaderData(indicatorName, Qt::Horizontal, tr("Indicator name"));
model->setHeaderData(intervalName, Qt::Horizontal, tr("Interval name"));
model->setHeaderData(iv_DataValue, Qt::Horizontal, tr("Value"));
model->setHeaderData(iv_DataDate, Qt::Horizontal, tr("Date"));
model->setHeaderData(iv_DataBirth, Qt::Horizontal, tr("Creation date"));
setModel(model);
hideColumn(iv_Id);
hideColumn(indicatorId);
QHeaderView *horizontalHeader = this->horizontalHeader();
horizontalHeader->setStretchLastSection(false);
QHeaderView *verticalHeader = this->verticalHeader();
verticalHeader->setStretchLastSection(false);
setSelectionBehavior(QAbstractItemView::SelectItems);
setColumnWidth(indicatorName, 400);
setColumnWidth(intervalName, 200);
setColumnWidth(iv_DataValue, 200);
setColumnWidth(iv_DataDate, 120);
setColumnWidth(iv_DataBirth, 120);
createIndiValueContextMenu();
show();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我为 QTableView 制作了特殊函数来清除以前模型中的列。于是问题就消失了。
I've made special function for QTableView to clear columns from previous models. So the problem gone away.