Results 1 to 9 of 9

Thread: Sorting a qtablewidget......

  1. #1
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Sorting a qtablewidget......

    HELLO,

    i face a problem while sorting the table...

    there are 2 columns....after i enter all the data it sorts it and the result is only the first column is visible..second column remains empty....

    why is dis happening???

    i m using this...

    table = new QTableWidget(1, 2, this);
    label << tr("MEMORY") << tr("DATA");
    table->setHorizontalHeaderLabels(label);
    table->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
    table->verticalHeader()->hide();
    table->setShowGrid(true);
    table->setSortingEnabled(true);
    table->sortItems (0,Qt:escendingOrder );

    plz help me/..

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Sorting a qtablewidget......

    The code you posted seems ok (next time please use the CODE tags). Is the second column shown if you disable sorting? How do you populate the table?

  3. #3
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a qtablewidget......

    is it necessary dat evry tim we add a row sortin needs to b disabled

    bcoz i add a row n populate it at run tim after takin data from user????????

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Sorting a qtablewidget......

    Quote Originally Posted by reshma View Post
    is it necessary dat evry tim we add a row sortin needs to b disabled
    No, I just wanted to now if the second colomn is shown when you disable the sorting at all, because I suppose, that the error is in your way of populating the table, not with the sorting option.

  5. #5
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a qtablewidget......

    yes strangely it gets displayed correctly if sortin is disabled....

    row=table->rowCount();
    table->insertRow(table->rowCount());
    QTableWidgetItem *fir = new QTableWidgetItem();
    QTableWidgetItem *sec = new QTableWidgetItem();
    fir->setText(QString::number(a));
    sec->setText(QString::number(b,16));
    table->setItem(row, 0, fir);
    table->setItem(row, 1, sec);
    row++;
    table->setSortingEnabled(true);

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Sorting a qtablewidget......

    Correction:
    void QTableWidget::setItem ( int row, int column, QTableWidgetItem * item )

    Sets the item for the given row and column to item.

    The table takes ownership of the item.

    Note that if sorting is enabled (see sortingEnabled) and column is the current sort column, the row will be moved to the sorted position determined by item.

    If you want to set several items of a particular row (say, by calling setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. setItem() will not move the row).

    See also item() and takeItem().
    After inserting the first item "row" is not correct anymore for your second item.

  7. #7
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a qtablewidget......

    ok ....i got it....

    n i rectified the row problem too...

    its workin if i disable sorting before adding a row...

    thnks a lot

  8. #8
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Sorting a qtablewidget......

    sortin is working correctly only for 3 rows....

    when 4-5 or more rows are being added its not sorting correctly...

    why is it happening so...??

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Sorting a qtablewidget......

    What are your values in the sorting column? Be aware using the constructor of QTabelWidgetItem will all input handle as strings. (and orderm them like strings.) If you have integers better use setData with the display role.

Similar Threads

  1. Sorting QTableWidget alters selectioin of cells
    By zarkzervo in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2009, 15:13
  2. QTableWidget sorting
    By losiem in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2008, 21:20
  3. QTableWidget sorting with hidden rows
    By alex140773 in forum Qt Programming
    Replies: 0
    Last Post: 8th July 2008, 13:35
  4. QTableWidget Sorting
    By mclark in forum Newbie
    Replies: 4
    Last Post: 29th September 2006, 23:34
  5. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 22: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.