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. Sorting QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Sorting QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 8 Posters 24.7k Views 2 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.
  • S Offline
    S Offline
    sheerin
    wrote on last edited by
    #1

    Hi I am not expert so please forgive me if I asked very easy question.
    i want to sort one of the column in QTableWidget, But when I do setSortingEnabled() true it only sorts it in alphabetic order though I have numbers in my cell. I tried understandign model view article in QtAssistant but believe me could not understand many thing and neither I could find any way how to solve me problem. I would be obliged if any of you experts can help me.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Immii
      wrote on last edited by
      #2

      sheerin, Dont feel shy ask even if you dont know how create a widget and show, we are here to help you and eventually you will also help others sooner or later.

      To solve your problem, You just re implement bool QTableWidgetItem:: operator<(....) function enable sorting by calling setsortingEnabled(true) for your tableWidget.

      I am sure this will solve your problem, Have Happy Holidays...

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sheerin
        wrote on last edited by
        #3

        Oh My god, I never expected such fast reply. I did post couple of question in popular Qt Centre but no reply Yet.
        But I am so sorry what should I implement in operator<<(), I can see this is there but I really dont knwo what what should be the code in this ? can you please help with some hint?

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vcsala
          wrote on last edited by
          #4

          [quote author="Immii" date="1293177250"]QTableWidgetItem:: operator<<(....) function [/quote]

          I beleive it was only a typo from Immii, you have to reimplement the @QTableWidgetItem::operator<(...)@ (less than operator), which is used by sorting to compare items.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            In general, if you implement operator<, yoou should also implement operator>, operator== and the opposit operators (operator!=, operator<=, operator>=) to be on the save way. If sorting ponly uses operator<, it will work, but perhaps some future algorithm will use operator>

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            1
            • I Offline
              I Offline
              Immii
              wrote on last edited by
              #6

              Yes that was my TYPO <<, of course I wanted to say <.
              This is what you have to do Sheerin copy and paste in your class and you are golden :)
              @
              bool myListItem ::operator< ( const QTableWidgetItem &other ) const
              {
              if( this->text().toInt() < other.text().toInt() )
              return true;
              else
              return false;
              }@

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Immii
                wrote on last edited by
                #7

                [quote author="Gerolf" date="1293178708"]In general, if you implement operator<, yoou should also implement operator>, operator== and the opposit operators (operator!=, operator<=, operator>=) to be on the save way. If sorting ponly uses operator<, it will work, but perhaps some future algorithm will use operator>[/quote]

                Gerolf, No other operators are not required to be reimplemented just for sorting, Yes if some other features is required then it may required to re implement. As far as sorting is concerned < operator will be enough to return true or false to decide which table item should come first.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sheerin
                  wrote on last edited by
                  #8

                  copied your exact code, and removed compiler errors, But I am not getting the result I wanted, it is still not sorting. Please help I have to submit my code today please please help

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vcsala
                    wrote on last edited by
                    #9

                    Can you just show the code (use '@' for code formatting) to check what should be the issue.

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Immii
                      wrote on last edited by
                      #10

                      Hmmm.... Okay here is the code I wrote for you I hope you wont be having any problem and will be able integrate this in your project. BTW is this some college project? If you are still unable to get it done take some help with your other friends or write us back with your code

                      @

                      #include <QtGui>

                      class myListItem: public QObject, public QTableWidgetItem
                      {
                      Q_OBJECT
                      public:
                      bool myListItem ::operator< ( const QTableWidgetItem &other ) const
                      {
                      if( this->text().toInt() < other.text().toInt() )
                      return true;
                      else
                      return false;
                      }
                      };

                      class MainWindow : public QMainWindow
                      {

                      public:
                      MainWindow(QWidget *parent = 0)
                      : QMainWindow(parent)
                      {
                      QWidget *w = new QWidget;
                      setCentralWidget(w);
                      QVBoxLayout *layout = new QVBoxLayout(w);
                      QTableWidget *list = new QTableWidget(2,1);
                      myListItem *item = new myListItem;
                      item->setText(QString("%1").arg(10));
                      list->setItem(0,0,item);
                      item = new myListItem;
                      item->setText(QString("%1").arg(15));
                      list->setItem(1,0,item);

                          list->setSortingEnabled(true);
                          layout->addWidget(list);
                      }
                      

                      };

                      #include "main.moc"
                      @

                      ? 1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sheerin
                        wrote on last edited by
                        #11

                        Thank you very much, Yes this solution works great. Now it looks to me that damn it was damn easy.
                        I am sorry I am new to Qt and dont know much of it, Can I bug you guys again with my little questions ??
                        This was simple but I did not get any reply from mailing list or from qt centre. so I think this site has more Qt guys and willing to help always.

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          vcsala
                          wrote on last edited by
                          #12

                          We are here to help you so just ask questions if you have. We learn from them as well.

                          1 Reply Last reply
                          0
                          • I Offline
                            I Offline
                            Immii
                            wrote on last edited by
                            #13

                            Good to hear finally you got it working. Yes you are most welcome to ask any damn easy or difficult question. we are here to help every one.

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              nala
                              wrote on last edited by
                              #14

                              Alternatively you can use the QTableWidgetItem function to ensure integer sorting:

                              virtual void   setData ( int role, const QVariant & value )
                              

                              e.g.
                              // value is the integer value for the cell.
                              QTableWidgetItem *it=tableWidget->item(row,col);
                              it->setData(0, value);

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #15

                                [quote author="nala" date="1340607940"]Alternatively you can use the QTableWidgetItem function to ensure integer sorting:

                                virtual void   setData ( int role, const QVariant & value )
                                

                                e.g.
                                // value is the integer value for the cell.
                                QTableWidgetItem *it=tableWidget->item(row,col);
                                it->setData(0, value);
                                [/quote]
                                While correct, I doubt that the one asking the question was still looking for a solution. After all, the question is more than a year and a half old...

                                1 Reply Last reply
                                0
                                • I Immii

                                  Hmmm.... Okay here is the code I wrote for you I hope you wont be having any problem and will be able integrate this in your project. BTW is this some college project? If you are still unable to get it done take some help with your other friends or write us back with your code

                                  @

                                  #include <QtGui>

                                  class myListItem: public QObject, public QTableWidgetItem
                                  {
                                  Q_OBJECT
                                  public:
                                  bool myListItem ::operator< ( const QTableWidgetItem &other ) const
                                  {
                                  if( this->text().toInt() < other.text().toInt() )
                                  return true;
                                  else
                                  return false;
                                  }
                                  };

                                  class MainWindow : public QMainWindow
                                  {

                                  public:
                                  MainWindow(QWidget *parent = 0)
                                  : QMainWindow(parent)
                                  {
                                  QWidget *w = new QWidget;
                                  setCentralWidget(w);
                                  QVBoxLayout *layout = new QVBoxLayout(w);
                                  QTableWidget *list = new QTableWidget(2,1);
                                  myListItem *item = new myListItem;
                                  item->setText(QString("%1").arg(10));
                                  list->setItem(0,0,item);
                                  item = new myListItem;
                                  item->setText(QString("%1").arg(15));
                                  list->setItem(1,0,item);

                                      list->setSortingEnabled(true);
                                      layout->addWidget(list);
                                  }
                                  

                                  };

                                  #include "main.moc"
                                  @

                                  ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #16

                                  @Immii said:

                                  this

                                  here what does this mean? and for multiple tables how do we implement

                                  ? 1 Reply Last reply
                                  0
                                  • ? A Former User

                                    @Immii said:

                                    this

                                    here what does this mean? and for multiple tables how do we implement

                                    ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by A Former User
                                    #17

                                    @VInay123 Which posting a you refering to?

                                    ? 1 Reply Last reply
                                    0
                                    • ? A Former User

                                      @VInay123 Which posting a you refering to?

                                      ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #18

                                      @Wieland It still not working about my sort problem, saw i tried to use this but still its not working

                                      ? 1 Reply Last reply
                                      0
                                      • ? A Former User

                                        @Wieland It still not working about my sort problem, saw i tried to use this but still its not working

                                        ? Offline
                                        ? Offline
                                        A Former User
                                        wrote on last edited by
                                        #19

                                        @VInay123 Like we already found out in another thread the reason it doesn't work for you is because you're using an ancient Qt version with a bug.

                                        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