You can use the sectionMoved
signal and reverse the change.
#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();
}