Qtablewidget Sorting problems

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.