Write the code today, found QT4 in the Qtablewidget display query results data, there is a problem, for specific reasons do not know whether the use of the wrong or qtablewidget itself exists bug. The phenomenon is as follows:
1. Query, can display the results of the query normally
2. Click Table Header Sort
3. Once again query, found that a write column content is empty
Constructor code:
ptable = new qtablewidget(this);
qvboxlayout*playout =newqvboxlayout;
This,setlayout(playout);
Qpushbutton*pbtn =new Qpushbutton(" query",this);
playout,addwidget(pbtn);
playout,addwidget(ptable);
BOOL ret =Connect(pbtn,SIGNAL(clicked (bool )), this, Slots (queryslot()));
ptable-setsortingenabled (true);
ptable-sortbycolumn(0,Qt::ascendingorder);
qstringlist labels ;
labels<<" power supply"<<" substation"<<" voltage Rating" ;
ptable,setcolumncount(labels. Size());
ptable,sethorizontalheaderlabels ( labels );
ptable-sethorizontalscrollmode(qabstractitemview::scrollperpixel);
ptable-setselectionbehavior(qabstractitemview::selectrows);
Query function code:
ptable,clearcontents();
ptable,setrowcount (0);
Qtablewidgetitem*pItem1 =NULL;
ptable,setrowcount(50);
for (inti = 0; I < 50; + +I)
{
for (intJ = 0; J< 3;++J)
{
pItem1 = new Qtablewidgetitem;
pItem1-setText(QString::Number (i) +","+QString ::Number (J));
ptable-setitem(i,J,pItem1);
}
}
is to execute the same logic, and it is unclear why this is happening.
The workaround that is currently found is to add the query function at the beginning of the
ptable,setsortingenabled (false);
End Plus
ptable-setsortingenabled (true);
ptable-sortbycolumn(0,Qt::ascendingorder);
The complete code is as follows:
ptable,setsortingenabled (false);
This will be displayed correctly.
ptable,clearcontents();
ptable,setrowcount (0);
Qtablewidgetitem*pItem1 =NULL;
ptable,setrowcount(50);
for (inti = 0; I < 50; + +I)
{
for (intJ = 0; J< 3;++J)
{
pItem1 = new Qtablewidgetitem;
pItem1-setText(QString::Number (i) +","+QString ::Number (J));
ptable-setitem(i,J,pItem1);
}
}
ptable-setsortingenabled (true);
ptable-sortbycolumn(0,Qt::ascendingorder);
Qtablewidget Sorting problems