Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:43:27+00:00 2026-06-06T12:43:27+00:00

I am trying to implement a table that is sortable on more than one

  • 0

I am trying to implement a table that is sortable on more than one column. Qt’s QSortFilterProxyModel only supports sorting on one column (at least in Qt 4.6.2).

I’ve found this solution by dimkanovikov on github, but it lacks dynamic updating on added rows. What I mean by this, is that the model is changed and the beginInsertRows(), beginRemoveRows(), their corresponding end..-methods and the dataChanged() signals are emitted. Ideally I would like to only these rows to be updated, but the model should at least react to such changes.

There’s another FAQ item on Qt’s site that sorts a QTableWidget, but it lacks dynamic updating, too.

I am new to Qt and I’d like to get some pointers on how I should go about this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:43:28+00:00Added an answer on June 6, 2026 at 12:43 pm

    There’s one slightly inelegant solution, that is always used to sort multiple columns.

    You have to subclass QSortFilterProxyModel and reimplement bool lessThan(const QModelIndex &rLeft, const QModelIndex &rRight) const. Instead of only comparing between the two given indices, check all the columns:

    int const left_row  = rLeft.row();
    int const right_row = rRight.row();
    
    int const num_columns = sourceModel()->columnCount();
    for(int compared_column = rLeft.column(); compared_column<num_columns; ++compared_column) {
        QModelIndex const left_idx = sourceModel()->index(left_row, compared_column, QModelIndex());
        QModelIndex const right_idx = sourceModel()->index(right_row, compared_column, QModelIndex());
    
        QString const leftData = sourceModel()->data(left_idx).toString();
        QString const rightData = sourceModel()->data(right_idx).toString();
    
        int const compare = QString::localeAwareCompare(leftData, rightData);
        if(compare!=0) {
            return compare<0;
        }
    }
    
    return false;
    

    Then you can call sort(0) on your QSortFilterProxyModel subclass and it will sort all the columns. Also don’t forget to call setDynamicSortFilter(true) when you want the sorted rows to be dynamically resorted when the model data changes.

    To support sorting on arbitrary columns in ascending or descending order, you would have to keep this info in a QList and compare accordingly when lessThan is called. In the list you would have the columns in order of their priority and do the comparisons in the same order. You should also sort the other “inactive” columns in some predefined order, otherwise they will not be sorted by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to implement a Table View on my iPhone app that grabs
I'm trying to implement a segue from one a table view controller to another
I'm trying to select more than 80,000 record in SQL Server in a table
I'm trying to implement EF with an existing table structure that I'm not allowed
Trying to implement something similar to qtip, but using a table that compares the
I'm trying to implement a dispatch table, so that I can call a selector
For a game, I am trying to implement a scores table. According to game
when trying to implement an Aspect, that is responsible for catching and logging a
I'm stuck trying to implement a user-configurable filter on a table using jQuery (in
Trying to implement some nested loops that are spitting out good old nested html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.