Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7069619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:28:03+00:00 2026-05-28T05:28:03+00:00

I have a QTableWidget that I populate like this: // Clear the table this->topPatchesTableWidget->setRowCount(0);

  • 0

I have a QTableWidget that I populate like this:

  // Clear the table
  this->topPatchesTableWidget->setRowCount(0);

  this->topPatchesTableWidget->setRowCount(numberToDisplay);
  for(unsigned int pairId = 0; pairId < numberToDisplay; ++pairId)
    {
    // Display patch match scores
    QTableWidgetItem* myLabel = new QTableWidgetItem;
    myLabel->setData(Qt::DisplayRole, myValues[pairId]);
    this->tableWidget->setItem(pairId, 0, myLabel);
    ... fill other columns ...
    }

(I have some other UI elements to set properties that compute the values in myValues). If I change the properties, recompute, and recreate the table, everything works as expected. If I sort the table by clicking on one of the headers, it sorts correctly. HOWEVER, if at this point (after sorting) I click my button again to recompute the values and recreate the table, the table is very broken. That is, many of the cells are empty, and the cells that are not empty don’t seem to be in any particular order.

By adding a manual call to

this->tableWidget->sortByColumn(0, Qt::AscendingOrder);

at the beginning of my CreateTable function, everything works as expected, but of course the newly created table is sorted by column 0 rather than the column that was selected for the last sort.

Does anyone have any idea why things would go so wrong without the call to sortByColumn? (I tried to make a simple example but I can’t replicate the problem in a demo program).

Thanks,

David

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:28:04+00:00Added an answer on May 28, 2026 at 5:28 am

    I had a similar problem in python, if a column header was selected to enable sorting the cells after that column stopped populating.

    I got around it by setting self.tableWidget.setSortingEnabled(False) at the beginning of the row add method, then setting it back to self.tableWidget.setSortingEnabled(True) at the end of the row add method. According to riverbank computing this is the officially recommended way to address this problem.

    QTableWidget.setItem (self, int row, int column, QTableWidgetItem 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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to have a QTableWidget that has 2 'header' rows. Essentially I'd like
I have a QTableWidget and would like that pressing Ctrl while clicking on a
I have a QTableWidget that has a combination of QLineEdit and normal table cell
I have a QTableWidget in my program that I use to display results and
I have an issue usign QTableWidgetItem. I normally use the QTableWidget like this this->setItem(i,
I have a QTableWidget that is used to display lots of data, basically derived
Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
I would like to have one column in QTableWidget NOT editable. In forums I
Say I have a QTableWidget with 10 columns. I set data in the table
I have a bunch of QComboBox es in a table. So that I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.