【问题标题】:Exclude moved column order from QHeaderView saveState/restoreState从 QHeaderView saveState/restoreState 中排除移动的列顺序
【发布时间】:2014-10-16 09:41:18
【问题描述】:

QHeaderView 的 saveState/restoreState 有问题。我的应用程序中有几个 QTableViews。 QTableView 保存并恢复它的 QHeaderView 设置,但对于某些 QTableView,我想将移动的部分顺序从保存到 ini 文件中排除。

这意味着,saveState 应该保存已排序的列、排序指示符、列宽,但如果用户移动了列,则不会。

有没有办法不保存移动的列?

谢谢。

问候, 玛尼

【问题讨论】:

    标签: qt savestate qheaderview


    【解决方案1】:

    没有简单的方法可以做到这一点。我建议类似下一个:

    使用向量存储logicalIndexes 的移动标题。

    QVector<int> last;
    

    使用sectionMoved信号检测移动并将logicalIndex存储在向量中:

    connect( ui->tableView->horizontalHeader(),static_cast<void (QHeaderView::*)(int,int,int)>(&QHeaderView::sectionMoved),[=](int logicalIndex, int oldVisualIndex, int newVisualIndex)
    {//with lambda
        //you can also provide shecking is current logicalIdnex already exist in vector
        last.push_back(logicalIndex);
     });
    

    语法如此复杂和丑陋,因为QHeaderView中还有另一个sectionMoved,所以是必要的。如果您不知道新语法,请使用旧语法:

    connect( ui->tableView->horizontalHeader(), SIGNAL(sectionMoved(int,int,int)), this, SLOT(yourSlot(int,int,int)));
    

    但是在这个槽中创建yourSlot(int,int,int) 并执行last.push_back(logicalIndex);

    当您需要saveState 时,隐藏所有带有logicalIndex 的部分并保存在vector 中并保存:

    QByteArray array;
    for(int i = 0; i < last.size(); i++)
    {
        ui->tableView->horizontalHeader()->hideSection(last.at(i));
    }
    array = ui->tableView->horizontalHeader()->saveState();
    

    如果您想使用新语法和 lambda,请将 CONFIG += c++11 添加到 pro 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2018-01-08
      相关资源
      最近更新 更多