• python QQTableView中嵌入复选框CheckBox四种方法


    搜索了一下,QTableView中嵌入复选框CheckBox方法有四种

            第一种不能之前显示,必须双击/选中后才能显示,不适用。

            第二种比较简单,通常用这种方法。

            第三种只适合静态显示静态数据用

            第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件,图片等自定义风格。

     

    第一种方法是:编辑委托法

    这种方法直接利用委托中重载createEditor(),激活QCheckBox,这个缺点是必须双击/选中,才能显示CheckBox控件。一般不满足我们实际中的直接显示的需要。可以参考Qt中的QSpinBoxDelegate例子。

    第二种方法是:设置QAbstractTableModel的flags()函数法。

    通过Delegate创建QCheckBox来实现的Check列,只有在该列进入编辑模式时才能够Check/Uncheck。这显然不是我们想要的,网上翻来翻去,在一个国外论坛中看到了无需Delegate的实现方法,只需重写Model即可:
     
    主要是修改两个函数:
    //设置某一列为可选角色,绘画出QCheckBox
    Qt::ItemFlags flags(const QModelIndex &index) const; 
    //根据界面选择QCheckbox,修改Model中的数据
     bool setData(const QModelIndex &index, const QVariant &value, int role);


    [cpp] view plain copy
     
     print?
    1. 2.在StudentInfoModel .h头文件中的主要代码:    
    2. class StudentInfoModel : public QAbstractTableModel     
    3. {    
    4.     Q_OBJECT    
    5. public:    
    6.     StudentInfoModel(const int totalColumn, const int aColumnNumWithChechBox = 0, QObject *parent = 0)    
    7.     :totalColumn(totalColumn),colNumberWithCheckBox(aColumnNumWithChechBox),    
    8.         
    9.     QAbstractTableModel(parent) {rowCheckStateMap.clear();};    
    10. public:    
    11.     int rowCount(const QModelIndex &parent = QModelIndex()) const;    
    12.     int columnCount(const QModelIndex &parent = QModelIndex()) const;    
    13.     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;    
    14.     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;    
    15.     Qt::ItemFlags flags(const QModelIndex &index) const;    
    16.     bool setData(const QModelIndex &index, const QVariant &value, int role);    
    17.     
    18.     
    19. public:    
    20.     void AddStudentInfo(const StudentInfo &studentInfo);    
    21.     
    22.     
    23.     signals:    
    24.     void StudentInfoIsChecked(const StudentInfo &studentInfo);    
    25.     
    26.     
    27. private:    
    28.     typedef QVector<StudentInfo> StudentInfos;    
    29.     StudentInfos studentInfos;    
    30.     int totalColumn;    
    31.     int colNumberWithCheckBox;    
    32.     QMap<int, Qt::CheckState> rowCheckStateMap;    
    33. };    
    34.     
    35.     
    36. 3.在StudentInfoModel.cpp文件中的主要代码如下:    
    37. QVariant StudentInfoModel::data( const QModelIndex &index, int role ) const    
    38. {    
    39.     if (role == Qt::DisplayRole)     
    40.     {     
    41.         if (index.column() == 0)     
    42.             return QString::number(index.row()+1);     
    43.         if (index.column() == 1)     
    44.             return studentInfos[index.row()].stuNumber;     
    45.         if (index.column() == 2)    
    46.             return studentInfos[index.row()].stuName;     
    47.         if (index.column() == 3)    
    48.             return studentInfos[index.row()].stuID;     
    49.         if (index.column() == 4)    
    50.             return studentInfos[index.row()].stuPhoneNumber;    
    51.         if (index.column() == 5)     
    52.             return studentInfos[index.row()].department;     
    53.         if (index.column() == 6)     
    54.             return studentInfos[index.row()].stuDescription;     
    55.     }     
    56.     if (role == Qt::CheckStateRole)     
    57.     {     
    58.         if (index.column() == colNumberWithCheckBox)     
    59.         {     
    60.             if (rowCheckStateMap.contains(index.row()))     
    61.             return rowCheckStateMap[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked; return Qt::Unchecked;     
    62.         }     
    63.     }     
    64.     return QVariant();    
    65. }    
    66.     
    67.     
    68. Qt::ItemFlags StudentInfoModel::flags( const QModelIndex &index ) const    
    69. {    
    70.     if    
    71.     (!index.isValid())    
    72.     return 0;    
    73.     
    74.     
    75.     if (index.column() == colNumberWithCheckBox)    
    76.     return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;    
    77.     
    78.     
    79.     return  Qt::ItemIsEnabled | Qt::ItemIsSelectable;    
    80. }    
    81.     
    82.     
    83. bool StudentInfoModel::setData( const QModelIndex &index, const QVariant &value, int role )    
    84. {    
    85.     if(!index.isValid())    
    86.         return false;    
    87.     if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox)    
    88.     {    
    89.         if (value == Qt::Checked) //    
    90.         {    
    91.             rowCheckStateMap[index.row()] = Qt::Checked;     
    92.             if(studentInfos.size() > index.row())    
    93.             emit StudentInfoIsChecked(studentInfos[index.row()]);    
    94.         }    
    95.         else    
    96.         {    
    97.             rowCheckStateMap[index.row()] = Qt::Unchecked;    
    98.         }     
    99.     }    
    100.     return true;    
    101. }    

    第三种方法是:用QTableView中的方法void setIndexWidget(const QModelIndex &index, QWidget *widget)来设置QCheckBox。

    代码:setIndexWidget(index, new QTextEdit);

     Qt Assistant 写道
    The items shown in a table view, like those in the other item views, are rendered and edited using standard delegates. However, for some tasks it is sometimes useful to be able to insert widgets in a table instead. Widgets are set for particular indexes with the setIndexWidget() function, and later retrieved with indexWidget().

        Qt Assistant 写道关于setIndexWidget()

    Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.
    If index is invalid (e.g., if you pass the root index), this function will do nothing.
    The given widget's autoFillBackground property must be set to true, otherwise the widget's background will be transparent, showing both the model data and the item at the given index.
    If index widget A is replaced with index widget B, index widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.
    setIndexWidget(index, new QLineEdit);
    ...
    setIndexWidget(index, new QTextEdit);
    This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QItemDelegate instead.

    此功能只应该用来显示可视区域内对应一个数据项的静态内容。如果你想显示自定义的动态内容或执行自定义编辑器部件,子类化QItemDelegate代替。

    就是说这个只适合用来做不变数据的显示,而不适合做一些会产生插入,更新,删除这样的操作的数据显示.

    第四种方法是:实现QAbstractItemDelegate的paint()函数

  • 相关阅读:
    第12组 Beta冲刺(2/5)
    第12组 Beta冲刺(1/5)
    第12组 Alpha事后诸葛亮
    第12组 Alpha冲刺(6/6)
    第12组 Alpha冲刺(5/6)
    第12组 Alpha冲刺(4/6)
    第12组 Alpha冲刺(3/6)
    第12组 Alpha冲刺(2/6)
    Why I start blogging.
    第二十章 更新和删除数据
  • 原文地址:https://www.cnblogs.com/yhleng/p/6922860.html
Copyright © 2020-2023  润新知