![](http://blog.51cto.com/image/skin/artType01.jpg)
![](https://i-blog.csdnimg.cn/blog_migrate/603adcd85ef19e7cbd537579dfdc3a3f.png)
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://devbean.blog.51cto.com/448512/264422
今天的QTableWidget是最后一个items class类型了。不过这并不是model-view的终结,因为我们现在还只是接触到了最简单的model-view的封装类,更复杂和强大的model-view类型的应用还没有见识到呢!
QTableWidget用起来也很方便,并不比前面的两个复杂到哪里去。我们运行的结果是这样子的:
![](https://i-blog.csdnimg.cn/blog_migrate/598cb3053c6ed3c6a5273005e92a433d.png)
下面是代码:
tablewidget.h
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
tablewidget.cpp
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78e1e3a115342bc8b092d7d92afd6281.png)
代码看起来很清楚。首先创建了QTableWidget对象,然后设置列数和行数。接下来使用一个QStringList,把每一列的标题设置了一下。然后调用addItem()函数。这个函数前两个参数分别是行row和列col,然后第三个参数构建一个QTableWidgetItem对象,这样,Qt就会把这个对象放在第row行第col列的单元格里面。注意,这里的行和列都是从0开始的。
本文出自 “豆子空间” 博客,请务必保留此出处http://devbean.blog.51cto.com/448512/264422