Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use Qt::CaseInsensitive in Qtablewidget data sorting?
Forum Updated to NodeBB v4.3 + New Features

How to use Qt::CaseInsensitive in Qtablewidget data sorting?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mathi77in
    wrote on last edited by
    #1

    i am using QTablewidget. i want to sort one of the column in QTableWidget. Column data have CaseInsensitive(upper and lower). when I do setSortingEnabled() true it only sorts it in alphabetic order for Uppercase.

    Miss out the small letter alphabetic data. i want sort the Column data from both (upper and lower) alphabetic order. If any sample code or url pls?

    How to apply Qt::CaseInsensitive in Qtablewidget sorting?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      On my machine sorting in QTableWidget is case insensitive by default, so I assume you want a case sensitive sort: that is words starting with A-Z sort before words starting a-z.

      Make a subclass of QTableWidgetitem and override the default operator<() to do a case-sensitive comparison. Use that subclass for the items you add to the table.

      Here is a complete program. The left column sorts one way, the right column the default.
      @#include <QtGui>
      #include <QDebug>

      class MyItem: public QTableWidgetItem
      {
      public:
      MyItem(const QString &text, int type = Type):
      QTableWidgetItem(text, type)
      { }

      bool operator< ( const QTableWidgetItem & other ) const {
          return (QString::compare(
                  data(Qt::DisplayRole).toString(),
                  other.data(Qt::DisplayRole).toString(), Qt::CaseSensitive ) < 0 );
      }
      

      };

      int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);
      QStringList values;
      values << "Z" << "b" << "B" << "a" << "A";
      QTableWidget tw(5,2);
      for (int r = 0; r < 5; ++r) {
      tw.setItem(r, 0, new MyItem(values.at(r))); // left column uses our sorting
      tw.setItem(r, 1, new QTableWidgetItem(values.at(r))); // right column uses default
      }
      tw.setSortingEnabled(true);
      tw.show();
      return app.exec();
      }
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved