zoukankan      html  css  js  c++  java
  • 解决QTableWidget不显示数据的问题

    QTableWidget通常用于数据的展示,通过其表格布局可以让用户更清晰的查看数据,同时也让数据的筛选变得更加直观。

    不过,初学者们和粗心大意的人总是会发现明明自己数据已经正常添加,可是程序运行之后却看不到QTableWidget上有任何一点数据,一片空白。

    这是怎么回事呢?我们先看一个可以复现这个问题的代码:

     1 func main() {
     2     widgets.NewQApplication(len(os.Args), os.Args)
     3 
     4     table := widgets.NewQTableWidget(nil)
     5     table.SetColumnCount(3)
     6     table.SetHorizontalHeaderLabels([]string{"编号", "姓名", "年龄"})
     7     // 去除边框
     8     table.SetShowGrid(false)
     9 
    10     // 设置数据
    11     num1 := widgets.NewQTableWidgetItem2("0", 0)
    12     name1 := widgets.NewQTableWidgetItem2("anmi", 0)
    13     age1 := widgets.NewQTableWidgetItem2("20", 0)
    14     table.SetItem(0, 0, num1)
    15     table.SetItem(0, 1, name1)
    16     table.SetItem(0, 2, age1)
    17 
    18     num2 := widgets.NewQTableWidgetItem2("1", 0)
    19     name2 := widgets.NewQTableWidgetItem2("terra", 0)
    20     age2 := widgets.NewQTableWidgetItem2("24", 0)
    21     table.SetItem(1, 0, num2)
    22     table.SetItem(1, 1, name2)
    23     table.SetItem(1, 2, age2)
    24 
    25     table.SetWindowTitle("QTableWidget")
    26     table.Show()
    27 
    28     widgets.QApplication_Exec()
    29 }
    导致数据无法显示的代码

    这是它的效果:

    没错,表头正常显示了,然而数据却不见了!

    我们再来看一下修复后的代码:

     1 func main() {
     2     widgets.NewQApplication(len(os.Args), os.Args)
     3 
     4     table := widgets.NewQTableWidget(nil)
     5     table.SetColumnCount(3)
     6     table.SetRowCount(2)
     7     table.SetHorizontalHeaderLabels([]string{"编号", "姓名", "年龄"})
     8     // 去除边框
     9     table.SetShowGrid(false)
    10 
    11     // 设置数据
    12     num1 := widgets.NewQTableWidgetItem2("0", 0)
    13     name1 := widgets.NewQTableWidgetItem2("anmi", 0)
    14     age1 := widgets.NewQTableWidgetItem2("20", 0)
    15     table.SetItem(0, 0, num1)
    16     table.SetItem(0, 1, name1)
    17     table.SetItem(0, 2, age1)
    18 
    19     num2 := widgets.NewQTableWidgetItem2("1", 0)
    20     name2 := widgets.NewQTableWidgetItem2("terra", 0)
    21     age2 := widgets.NewQTableWidgetItem2("24", 0)
    22     table.SetItem(1, 0, num2)
    23     table.SetItem(1, 1, name2)
    24     table.SetItem(1, 2, age2)
    25 
    26     table.SetWindowTitle("QTableWidget")
    27     table.Show()
    28 
    29     widgets.QApplication_Exec()
    30 }
    正确的代码

    显示效果:

    其实问题很简单,看代码的第六行,我们设置了行数。

    QTableWidget需要先设置一共有多少行数据才能正常显示,如果不设置那么默认是0行数据,也就是什么也不显示。

    所以需要QTableWidget的人一定不要忘了使用SetRowCount告诉widget有多少数据需要绘制出来。

  • 相关阅读:
    GRIDVIEW导出到EXCEL
    数据表示:字节 高位低位
    js学习笔记0
    12奇招,循序删除顽固的文件
    加快开关机速度
    PHP正则表达式的快速学习
    firefox下height不能自动撑开的解决办法
    给MySQL加密(也适用于Wamp5中)
    我的电脑创建资源管理器
    css 圆角7种CSS圆角框解决方案
  • 原文地址:https://www.cnblogs.com/apocelipes/p/9687963.html
Copyright © 2011-2022 走看看