Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to optimize qt tablewidget refreshing?
    primarykey
    data
    text
    <p>I'am using qt to develop a monitor which shows new log items received from the network.<br> I use <strong>QTableWidget</strong> to display the <strong>dynamic items received</strong> and the table only shows 100 rows.<br> The problem is: the monitor will receive some 400 'messages' per sec and update the rows.<br> I found using <strong>setItem(row,col,item)</strong> and <strong>new QTableWidgetItem()</strong> is quite time-consuming, I only want to show the newest items(three strings at 3 cols) from top to buttom.<br> But the method is really time-consuming, it costs 5.281s when I add 4000 test cases at one time, which should finish in 4000/400 = 10s.<br> <strong><em>So it costs the half time, how to shorten the time? Any better methods to use the qt tablewidget?</em></strong> ;) Thanks for reading!</p> <p>The filterLog function code:</p> <pre><code> start = clock(); filter_log_display(); duration_filterLogDisplay += (double)(finish - start) / CLOCKS_PER_SEC; </code></pre> <p>filter_log_display():</p> <pre><code> clock_t start = clock(); row_selectable = false; ui-&gt;tableWidget-&gt;setRowCount(0);//delete table items all row_selectable = true; int size_1 = logDisplayQueue.size() - 1; ui-&gt;tableWidget-&gt;verticalScrollBar()-&gt;setSliderPosition(0); if(size_1+1 &lt; 100) { ui-&gt;tableWidget-&gt;setRowCount(size_1 + 1); } else { ui-&gt;tableWidget-&gt;setRowCount(100); } clock_t finish = clock(); duration_setRowCount += (double)(finish - start) / CLOCKS_PER_SEC; for(int queue_i = size_1, index = 0; queue_i &gt;= 0; queue_i--, index++) { start = clock(); LogInfoItem* logItem = (LogInfoItem*)logDisplayQueue.at(queue_i); finish = clock(); duration_getItemFQueue += (double)(finish - start) / CLOCKS_PER_SEC; start = clock(); QString BITS_str = bits2Hexs(logItem-&gt;BITS); finish = clock(); duration_bits2Hexs += (double)(finish - start) / CLOCKS_PER_SEC; start = clock(); ui-&gt;tableWidget-&gt;setItem(index, 0, new QTableWidgetItem(logItem-&gt;time));//time ui-&gt;tableWidget-&gt;setItem(index, 1, new QTableWidgetItem(logItem-&gt;name));//name ui-&gt;tableWidget-&gt;setItem(index, 2, new QTableWidgetItem(BITS_str));//BITS finish = clock(); duration_setItem += (double)(finish - start) / CLOCKS_PER_SEC; start = clock(); if(queue_i == oldRowItemNo)ui-&gt;tableWidget-&gt;selectRow(index); finish = clock(); duration_ifSelectRow += (double)(finish - start) / CLOCKS_PER_SEC; } </code></pre> <p>Analysis about time: :) There are many places to optimize including this one :)</p> <p><img src="https://i.stack.imgur.com/JMMpK.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload