首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否禁止将列移动到索引0?

是否禁止将列移动到索引0?
EN

Stack Overflow用户
提问于 2018-07-08 14:06:24
回答 1查看 36关注 0票数 1

有没有一种方法可以允许对索引部分(列)进行重新排序(又称setSectionsMovable(true)),但不允许将任何列移动到索引0?我想让列可以通过拖拽来重新排序,但不允许将拖拽的列放在表的最开始。

类似于“如果拖动的列目标索引等于0,则在释放鼠标时,取消拖动并不执行任何操作”之类的语句。这个是可能的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-08 14:32:49

您可以使用sectionMoved信号并撤销更改。

代码语言:javascript
复制
#include <QApplication>
#include <QStandardItemModel>
#include <QTableView>
#include <QHeaderView>

class CustomHeaderView: public QHeaderView{
public:
    CustomHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
        : QHeaderView(orientation, parent)
    {
        connect(this, &CustomHeaderView::sectionMoved, this, &CustomHeaderView::onSectionMoved);
    }
private slots:
    void onSectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex){
        Q_UNUSED(logicalIndex)
        if(newVisualIndex == 0){
            moveSection(newVisualIndex, oldVisualIndex);
        }
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTableView w;
    CustomHeaderView *headerview = new CustomHeaderView(Qt::Horizontal, &w);
    w.setHorizontalHeader(headerview);
    w.horizontalHeader()->setSectionsMovable(true);
    QStandardItemModel model(10, 10);
    for(int i = 0; i < model.columnCount(); ++i)
        for(int j = 0; j < model.rowCount(); ++j)
            model.setItem(i, j, new QStandardItem(QString("%1-%2").arg(i).arg(j)));
    w.setModel(&model);
    w.show();

    return a.exec();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51229311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档