今天写代码,发现Qt4中QTableWidget显示查询结果数据时存在一个问题,具体原因不知道是用法不对还是QTableWidget本身存在的bug。现象如下:

1.      查询,能正常显示查询结果

2.      点击表头排序

3.      再一次进行查询,发现某写列内容为空

构造函数代码:

pTable = new QTableWidget(this);

QVBoxLayout*pLayout = newQVBoxLayout;

this->setLayout(pLayout);

QPushButton*pBtn = new QPushButton("查询",this);

pLayout->addWidget(pBtn);

pLayout->addWidget(pTable);

boolret =connect(pBtn, SIGNAL(clicked ( bool  ) ) , this ,SLOT( QuerySlot(  ) ) );

pTable->setSortingEnabled (true);

pTable->sortByColumn(0,Qt::AscendingOrder);

QStringListlabels ;

labels<<"供电单位"<<"变电站"<<"电压等级";

pTable->setColumnCount( labels.size() );

pTable->setHorizontalHeaderLabels (  labels );

pTable->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);

pTable->setSelectionBehavior(QAbstractItemView::SelectRows);

查询函数代码:

pTable->clearContents();

pTable->setRowCount ( 0 );

QTableWidgetItem*pItem1 = NULL;

pTable->setRowCount(50);

for(int i = 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 (false);

结尾加上

pTable->setSortingEnabled (true);

pTable->sortByColumn(0,Qt::AscendingOrder);

完整代码如下:

pTable->setSortingEnabled (false);

这样就可以正确显示。

pTable->clearContents();

pTable->setRowCount ( 0 );

QTableWidgetItem*pItem1 = NULL;

pTable->setRowCount(50);

for(int i = 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);

http://blog.csdn.net/hai200501019/article/details/45746477

最新文章

  1. MFC 创建多层目录
  2. ZOJ Problem Set - 1205 Martian Addition
  3. mysql 字符串
  4. js浮点数精确计算(加、减、乘、除)
  5. 新浪微博API开放平台进行程序开发第一步(java)
  6. Dom事件初步了解
  7. Flash视频播放器开发经验总结
  8. C#之面向对象初步
  9. Leetcode解题记录
  10. 2015 多校联赛 ——HDU5373(模拟)
  11. RDC去省赛玩前の日常训练 Chapter 2
  12. underscore用法大全
  13. web.xml详细选项配置
  14. AutoHotKey (AHK) 按键表+自定义快捷键简化操作的教程
  15. tomcat运行超时问题解决
  16. bzoj3678 简单题
  17. Swoole 结合TP5创建http服务
  18. 21 week4 submit buidAndRun() node-rest-client
  19. rbac 权限分配, 基于formset实现,批量增加
  20. 51nod 1636 教育改革

热门文章

  1. MemberShip使用大全
  2. c#程序添加其他网站的webservice引用。
  3. SpringMVC 详解
  4. Codeforces 18C C. Stripe
  5. 字节流复制mp3文件(带缓冲区)
  6. nginx启动
  7. Android 6.0 新特性 整理 资料来自网络
  8. 转:C++:从子类访问父类的私有函数
  9. BZOJ 3196
  10. linux嵌入式: 实现自己的tree命令