歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> QT:封裝一個簡易的二維表類SimpleTable

QT:封裝一個簡易的二維表類SimpleTable

日期:2017/3/1 10:14:37   编辑:Linux編程

QT:我自己封裝的一個簡易的二維表類SimpleTable。在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. }
Copyright © Linux教程網 All Rights Reserved