在QT中,QTableWidget处理二维表格的功能很强大(QTableView更强大),但有时我们只想让它显示少量数据(文字和图片),这时,使用QTableWidget就有点不方便了(个人感觉)。
所以我对QTableWidget再做了一次封装(SimpleTable类),让它在处理小型表格时更方便。
代码很简单,要解释的就写在注释里面了,欢迎大家使用。

如果大家发现这个类的BUG的话,欢迎提出,大家共同学习。

上代码:

  1. //simpletable.h
  2. #ifndef SIMPLETABLE_H_
  3. #define SIMPLETABLE_H_
  4. #include <QtCore>
  5. #include <QtGui>
  6. class SimpleTable : public QTableWidget
  7. {
  8. Q_OBJECT
  9. private:
  10. public:
  11. //构造函数,无实际内容,直接调用的构造函数
  12. SimpleTable(QWidget *parent = 0) : QTableWidget(parent) { }
  13. SimpleTable(int row, int column, QWidget *parent = 0)
  14. : QTableWidget(row, column, parent) { }
  15. //设置某个单元格中的文字
  16. void SetCellText( int cx, int cy, const QString &text,
  17. int alignment = Qt::AlignLeft,
  18. const QIcon icon = QIcon() );
  19. //在某个单元格中放置一张小图片
  20. void SetCellPixmap(int cx, int cy, const QPixmap &pixmap,
  21. Qt::Alignment alignment = Qt::AlignCenter);
  22. //获取某个单元格中的字符串(如果没有,就返回一个空字符串)
  23. QString GetCellText(int cx, int cy);
  24. //获取某个单元格的图片(如果没有,就返回一张空图片)
  25. QPixmap GetCellPixmap(int cx, int cy);
  26. };
  27. #endif
  28. //simpletable.cpp
  29. #include "simpletable.h"
  30. void SimpleTable::SetCellText(int cx, int cy, const QString &text,
  31. int alignment, const QIcon icon)
  32. {
  33. //检查是否越界
  34. if( cx>=rowCount() || cy>=columnCount() )
  35. {
  36. qDebug() << "Fail, Out of Range";
  37. return;
  38. }
  39. //如果此单元格中已经有item了,就直接更改item中的内容
  40. //否则,新建一个item
  41. QTableWidgetItem *titem = item(cx, cy);
  42. if( NULL == titem )
  43. titem = new QTableWidgetItem;
  44. titem->setText(text);
  45. titem->setTextAlignment(alignment);
  46. //如果图标不为空,就为此item设置图标
  47. if( !icon.isNull() )
  48. titem->setIcon(icon);
  49. setItem(cx, cy, titem);
  50. }
  51. void SimpleTable::SetCellPixmap(int cx, int cy, const QPixmap &pixmap,
  52. Qt::Alignment alignment)
  53. {
  54. if( cx>=rowCount() || cy>=columnCount() )
  55. {
  56. qDebug() << "Fail, Out of Range";
  57. return;
  58. }
  59. //在item中设置图片有很多方法,但我还是觉得在其中放置带图片一个Label最简单
  60. QLabel *label = new QLabel(this);
  61. label->setAlignment(alignment);
  62. label->setPixmap(pixmap);
  63. setCellWidget(cx, cy, label);
  64. }
  65. QString SimpleTable::GetCellText(int cx, int cy)
  66. {
  67. QString result;
  68. if( cx>=rowCount() || cy>=columnCount() )
  69. {
  70. qDebug() << "Fail, Out of Range";
  71. return result;
  72. }
  73. QTableWidgetItem *titem = item(cx, cy);
  74. if( NULL != titem )
  75. {
  76. result = titem->text();
  77. }
  78. return result;
  79. }
  80. QPixmap SimpleTable::GetCellPixmap(int cx, int cy)
  81. {
  82. QPixmap result;
  83. if( cx>=rowCount() || cy>=columnCount() )
  84. {
  85. qDebug() << "Fail, Out of Range";
  86. return result;
  87. }
  88. QTableWidgetItem *titem = item(cx, cy);
  89. if( NULL == titem )
  90. return result;
  91. QLabel *label = dynamic_cast<QLabel*>( cellWidget(cx, cy) );
  92. result = *label->pixmap();
  93. return result;
  94. }

以下是一个简单的测试例子:

    1. //main.cpp
    2. //测试例子
    3. #include "simpletable.h"
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7. SimpleTable table(20, 10);
    8. for(int i=0; i<20; i++)
    9. {
    10. for(int j=0; j<10; j++)
    11. {
    12. table.SetCellText(i, j, QString::number(i*10+j));
    13. }
    14. }
    15. table.show();
    16. return app.exec();
    17. }

http://blog.csdn.net/small_qch/article/details/7674733

最新文章

  1. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
  2. 了解了下 Google 最新的 Fuchsia OS
  3. Calculator(1.0)
  4. 移动销售端app的需求分析
  5. EL标签使用
  6. stl-基本知识
  7. CStringUtf8ToUnicode
  8. Android之Handler
  9. dedecms lnmp 环境搭建。备忘录非教程
  10. leetcode@ [279]Perfect Squares
  11. Maven学习小结(三 基本概念)
  12. 面试准备--Spring(AOP)
  13. LintCode-最长公共子串
  14. 由于物化视图定义为on commit导致update更新基表慢的解决方案
  15. java 细说String
  16. JavaFX - 富互联网应用
  17. Java高并发--缓存
  18. 根据href给当前导航添加样式
  19. 洛谷——P1345 [USACO5.4]奶牛的电信Telecowmunication
  20. C 六度空间理论的实现

热门文章

  1. 多线程实际运用&lt;第七篇&gt;
  2. Redis Clients Handling
  3. 关于oracle动态视图v$datafile和v$datafile_header(转)
  4. 用来控制 XML 序列化的属性
  5. mvc 日历控件
  6. CSS清除浮动的方法
  7. stagefright框架(二)- 和OpenMAX的運作
  8. SQL Server 2008 忘记sa密码的解决办法
  9. Unhandled event loop exception 解决办法
  10. 《JavaScript 闯关记》之对象