QTableView 和空标题和列

发布于 2024-09-26 12:32:16 字数 3137 浏览 4 评论 0原文

我有一个 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?

alt text

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2024-10-03 12:32:16

我为 QTableView 制作了特殊函数来清除以前模型中的列。于是问题就消失了。

I've made special function for QTableView to clear columns from previous models. So the problem gone away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文