Alternatives to QHeaderView::moveSection() in Qt


    • You can use hideSection(int logicalIndex) to hide the section you want to move and then showSection(int newLogicalIndex) at the desired new position. This approach works but might cause flicker during the process.
  1. Model Manipulation (For advanced users)

    • If you have control over the underlying model that provides data to the header view, you can manipulate the model's section order directly. This involves working with the specific model class and its methods for moving sections.

Here are some helpful resources for further understanding:

  • Documentation for specific model classes like QStandardItemModel might have methods for section manipulation. You can find them in the Qt documentation or through online searches.


Hiding and Showing

This example demonstrates moving a section from index 1 to index 3 in a QHeaderView:

#include <QApplication>
#include <QHeaderView>

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  // Create a simple header view
  QHeaderView headerView(Qt::Horizontal);
  headerView.setSectionCount(5);
  headerView.setDefaultSectionSize(100);

  // Hide section at index 1
  int sectionToMove = 1;
  headerView.hideSection(sectionToMove);

  // Show it at the new position (index 3)
  int newPosition = 3;
  headerView.showSection(newPosition);

  headerView.show();

  return app.exec();
}

Model Manipulation (Using QStandardItemModel)

This example assumes you're using a QStandardItemModel and demonstrates moving a section from index 1 to index 3:

#include <QApplication>
#include <QHeaderView>
#include <QStandardItemModel>

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  // Create a header view and model
  QHeaderView headerView(Qt::Horizontal);
  QStandardItemModel model(5, 1);

  // Set some dummy data
  for (int i = 0; i < model.rowCount(); ++i) {
    model.setData(model.index(i, 0), "Column " + QString::number(i + 1));
  }

  headerView.setModel(&model);

  // Get the items to move
  QStandardItem* itemToMove = model.item(1, 0);

  // Remove the item from its current position
  model.takeRow(1);

  // Insert the item at the new position
  model.insertRow(3, itemToMove);

  headerView.show();

  return app.exec();
}

Remember, the hiding and showing approach might cause a flicker. The model manipulation approach is generally smoother but requires control over the underlying model.



    • The previous example showed hiding and showing sections. You can improve this by emitting signals like sectionHidden and sectionShown before and after the operations for better handling by connected slots.
  1. Custom Model with Section Movement Functionality

    • If you have more complex section movement needs, consider creating a custom model class that inherits from an existing model (like QStandardItemModel) and implements your own moveSection function. This function would handle logic for updating the underlying data structure and notifying the view about changes.
  2. Third-Party Libraries (For advanced users)

    • Explore third-party libraries specifically designed for manipulating Qt tables or views. These libraries might offer functionalities similar to moveSection. However, using third-party libraries adds complexity and dependency management.
ApproachProsCons
Hiding & Showing (Improved)Simpler to implement, good for basic casesMight cause flicker, requires manual signal emission
Custom ModelFlexible, allows for complex movement logicMore complex to implement, requires understanding models
Third-Party LibrariesPotentially powerful featuresAdds complexity, dependency management overhead