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

  • Home
  • SEARCH
  • 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 7809625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:29:56+00:00 2026-06-02T03:29:56+00:00

THE CODE: populateTable() { tableWidget->clearContents(); tableWidget->setRowCount(stringList.size()); for(int i = 0; i < stringList.size(); ++i)

  • 0

THE CODE:

populateTable()
{
  tableWidget->clearContents();
  tableWidget->setRowCount(stringList.size());

  for(int i = 0; i < stringList.size(); ++i)
  {
    tableWidget->setItem(i, 0, new QTableWidgetItem(stringList.at(i)));
  }
}

THE PROBLEM:

The first time populateTable() is run, everything is fine. But the next consequent times, it runs significantly slower than before.

DISCUSSION:

After careful testing, I suspect clearContents() to be the problem. Because simply changing the code from:

tableWidget->clearContents();

to:

tableWidget->setRowCount(0);

fixes the problem, but now it makes another problem; Setting the row count to ‘0’ does not seem to delete the heap allocated QTableWidgetItems, it just seems to leave ownership of the items, so it leaves a memory leak. (or at least i just think so…)

Qt’s documentation in QTableWidget is rather vague, so I don’t exactly know what clearContents() actually does. In the documentation it says "Removes all items not in the headers from the view" so that has left me asking, do the contents of the table just hide? Does it get deleted? I’m not exactly sure. My theory is that clearContents() only hides the items and any next attempts to populate the table actually deletes and removes each item then allocates a new one to set on the table, which in turn is an expensive operation.

Another interesting thing is that Qt’s documentation on QTableWidget suggests that the proper way to populate a QTableWidget is to allocate a QTableWidgetItem on heap then set it on a table cell with setItem(). Just like I presented in the code above, which I find to be wierd…

IN SUMMARY:

Is there an alternate way to populate and repopulate a Qt table without having all of these problems? If not, is there a way to fix these problems?

  • 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-06-02T03:29:57+00:00Added an answer on June 2, 2026 at 3:29 am

    Qt’s documentation on QTableWidget suggests that the proper way to populate a QTableWidget is to allocate a QTableWidgetItem on heap then set it on a table cell with setItem() just like i presented in the code above, which I find to be weird…

    I don’t find it weird, considering that the documentation for QTableWidget::setItem says explicitly that the widget takes ownership of the item!

    <Setting the row count to ‘0’ does not seem to delete the heap allocated QTableWidgetItems, it just seems to leave ownership of the items, so it leaves a memory leak. (or at least i just think so…)

    Rather than guessing whether it’s leaking or not, you could create your own subclass of QTableWidgetItem…and put code in its destructor so you have a place to put a breakpoint and know for sure. Alternatively, the Qt sources are quite legible. setRowCount calls removeRows, which does indeed delete QTableWidgetItems:

    But this is again consistent with the documentation for QTableWidget::setRowCount:

    Sets the number of rows in this model to rows. If this is less than rowCount(), the data in the unwanted rows is discarded.

    You really shouldn’t be seeing much of a difference between clearContents() and setRowCount(0). Have you generated a small reproducible example, not entwined in any larger program, that demonstrates this phenomenon?

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

Sidebar

Related Questions

Code goes first: void foo(int x) { void bar(int); //is this forward-decl legal? bar(x);
Code goes first: class A { public: ... int *foo() const { return _px;
Code can be perfect, and also perfectly useless at the same time. Getting requirements
Code below sets default values for new row if row is added using form.
Code: private final int A0 = 0; private final int A1 = 1; .....
Code snippet: for (int i = 0; i < vals.Length -1; i++) { series1.Points.Add(Convert.ToDouble(vals[i]));
Code for webpage such that if anyone closes it, then next time when he
code id date time difference AiK4JJ kcy2000ok 2012-01-31 17:25:41 13 unBG1D gktoql 2012-01-31 17:25:35
Code: LinkButton linkButton = new LinkButton(); linkButton.OnClientClick=changecolor;--i need to change this to on mouse
Code: public IList<T> GetByMultipleIds(int[] ids) { List<T> result = _session.Linq<T>() .Where(x => ids.Contains(x.Id)).ToList(); return

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.