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
Qt 6.11 is out! See what's new in the release blog

Sorting QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 8 Posters 26.0k 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.
  • ? Offline
    ? Offline
    A Former User
    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
    • ? Offline
      ? Offline
      A Former User
      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
      • ? Offline
        ? Offline
        A Former User
        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
        • ? Offline
          ? Offline
          A Former User
          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
          • ? Offline
            ? Offline
            A Former User
            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
              • ? A Former User

                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