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. [Solved] Detecting sort on QTableWidget
QtWS25 Last Chance

[Solved] Detecting sort on QTableWidget

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 16.8k Views
  • 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.
  • D Offline
    D Offline
    dutchgold92
    wrote on last edited by
    #1

    Hi,
    I want to call a slot whenever the user clicks my QTableWidget's header to sort by that column, but none of the default signals seem to apply to this. Any suggestions?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      master of disaster
      wrote on last edited by
      #2

      QTableWidget has a member signal named itemClicked(). The parameter is a pointer to a QTableWidgetItem object that was clicked. I would suggest you to compare the value the pointer points to with the QTableWidgetItems in the header part of the QTableWidget to find out which column was clicked. Then you can enable sorting by this column, I think sortItems() would be the best way to do this.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jake007
        wrote on last edited by
        #3

        Hi!

        There are no signals, but you can easily derive class from QTableWidget and override sortItem() method.

        Example:
        @class TableWidget : QTableWidget
        {
        Q_OBJECT

        public:
        void sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder );

        signals:
        void sort();

        public slots:
        void onBeforeSort();
        };

        void TableWidget::sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )
        {
        emit sort();

        QTableWidget::sortItems(column, order);
        }@

        Regards,
        Jake


        Code is poetry

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dutchgold92
          wrote on last edited by
          #4

          [quote author="master of disaster" date="1356539132"]QTableWidget has a member signal named itemClicked(). The parameter is a pointer to a QTableWidgetItem object that was clicked. I would suggest you to compare the value the pointer points to with the QTableWidgetItems in the header part of the QTableWidget to find out which column was clicked. Then you can enable sorting by this column, I think sortItems() would be the best way to do this.[/quote]

          Thanks for the suggestion, but I've tried that; clicking on the header items doesn't emit this signal.

          [quote author="Jake007" date="1356540185"]Hi!

          There are no signals, but you can easily derive class from QTableWidget and override sortItem() method.

          Example:
          @class TableWidget : QTableWidget
          {
          Q_OBJECT

          public:
          void sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder );

          signals:
          void sort();

          public slots:
          void onBeforeSort();
          };

          void TableWidget::sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )
          {
          emit sort();

          QTableWidget::sortItems(column, order);
          }@

          Regards,
          Jake[/quote]

          Simple solution but sounds like it should work, I'll give it a try. Cheers!

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jake007
            wrote on last edited by
            #5

            It does not emit a signal, also if column is not sortable, code above will not emit a signal. You emit it manually only if sortItems is called.

            Two notes of the code above:

            • void onBeforeSort() was meant to connect it with a sort() signal. You can remove it if you don't need it.
            • It will emit a signal even if it's trigger from code. Use blockSignals in this case.

            Code is poetry

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dutchgold92
              wrote on last edited by
              #6

              [quote author="Jake007" date="1356558735"]It does not emit a signal, also if column is not sortable, code above will not emit a signal. You emit it manually only if sortItems is called.

              Two notes of the code above:

              • void onBeforeSort() was meant to connect it with a sort() signal. You can remove it if you don't need it.
              • It will emit a signal even if it's trigger from code. Use blockSignals in this case. [/quote]

              Okay, I see that now. But the problem is I only need to perform a particular action if the user sorts the table by clicking on the header; any sorting already performed in the code I have under control.

              1 Reply Last reply
              0
              • W Offline
                W Offline
                wssddc
                wrote on last edited by
                #7

                I think the signal sectionClicked from the horizontalHeader of your table should do what you want. (I use this, but I have table sorting disabled and do my own sorts.)

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dutchgold92
                  wrote on last edited by
                  #8

                  [quote author="wssddc" date="1356562858"]I think the signal sectionClicked from the horizontalHeader of your table should do what you want. (I use this, but I have table sorting disabled and do my own sorts.)
                  [/quote]

                  So assuming it does do what I want, I would need to extend QTableWidget to create a custom table widget, that uses a horizontal header which is itself a custom widget?

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    wssddc
                    wrote on last edited by
                    #9

                    I don't extend QTableWidget. The horizontal header is part of it. I just do
                    @
                    connect(ui->tableWidget->horizontalHeader(), SIGNAL(sectionClicked(int)),
                    this, SLOT(slot_table_clicked(int)));
                    @
                    and slot_table_clicked gets the column number that was clicked.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dutchgold92
                      wrote on last edited by
                      #10

                      [quote author="wssddc" date="1356650243"]I don't extend QTableWidget. The horizontal header is part of it. I just do
                      @
                      connect(ui->tableWidget->horizontalHeader(), SIGNAL(sectionClicked(int)),
                      this, SLOT(slot_table_clicked(int)));
                      @
                      and slot_table_clicked gets the column number that was clicked.
                      [/quote]

                      I hadn't realised I could connect signals and slots in this way. That solved my problem perfectly, thanks!

                      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