zoukankan      html  css  js  c++  java
  • 自己封装的一个简易的二维表类SimpleTable

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

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

    上代码:

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

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

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

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

  • 相关阅读:
    Python 于 webgame 的应用(上)
    TCP Nagle剖析
    配置Windows下的Git 服务器简易教程
    程序员的绘图利器 — Graphviz
    patch的制作和使用
    PyCon China 2012,我们上海见!
    Python 于 webgame 的应用(下)
    TCP接收缓存大小的动态调整
    TCP的TSO处理
    上传压死下载 & 常见TCP选项
  • 原文地址:https://www.cnblogs.com/findumars/p/4993555.html
Copyright © 2011-2022 走看看