Results 1 to 4 of 4

Thread: QTableWidget: Strange row selection after changing item while sorting is enabled

  1. #1
    Join Date
    Sep 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Question QTableWidget: Strange row selection after changing item while sorting is enabled

    Hi guys,

    i have a QTableWidget with a few (in my case 5) columns and following properties:
    - Select Row
    - Sorting Enabled

    When the table is sorted in the first column and I change the content an item of the first column, the row changes to the new position in the sorting order but the selection for some rows stays on the same row number as before. (see picture)

    Capture.JPG

    After a few tests with different column numbers I saw, that only the first and the last column selection will change. When I click on any item, the selection goes to the normal behavior.

    Does somebody have the idea to fix the problem?

    Thanks,
    Tobi

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableWidget: Strange row selection after changing item while sorting is enabled

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget: Strange row selection after changing item while sorting is enabled

    Hi,
    here is a minimal example that produces the described issue. Btw I'm using Qt 5.4.1.

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTableWidget>
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14.  
    15. private:
    16. QTableWidget *table;
    17. };
    18.  
    19. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. setFixedSize(550,200);
    7.  
    8.  
    9. // Setup Table
    10. table = new QTableWidget(this);
    11. table->setColumnCount(5);
    12. table->setRowCount(5);
    13. table->setSelectionMode(QAbstractItemView::SingleSelection);
    14. table->setSelectionBehavior(QAbstractItemView::SelectRows);
    15. table->setSortingEnabled(true);
    16.  
    17. // filling the table
    18. for(int i=0; i<table->rowCount(); i++)
    19. table->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
    20.  
    21. // sort table
    22. table->sortByColumn(0,Qt::AscendingOrder);
    23.  
    24. // Add table to window widget
    25. setCentralWidget(table);
    26. }
    27.  
    28. MainWindow::~MainWindow()
    29. {
    30. delete table;
    31. }
    To copy to clipboard, switch view to plain text mode 

    When I change the number in the first cell from 1 to 6 I got the selection problem.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableWidget: Strange row selection after changing item while sorting is enabled

    I think it is a bug.

    I tried to change other column and row data randomly the app crashes with this message
    ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex: "persistent model indexes corrupted", file itemmodels\qabstractitemmodel.cpp, line 556
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. QTableWidget sorting
    By abrou in forum Newbie
    Replies: 2
    Last Post: 6th June 2012, 10:53
  2. QTableWidget sorting
    By losiem in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2008, 20:20
  3. Replies: 14
    Last Post: 9th November 2006, 08:35
  4. Model sorting vs. selection
    By VlJE in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 16:46
  5. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 21:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.