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

Sorting QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 8 Posters 25.3k 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.
  • 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