QTableView中嵌入复选框CheckBox方法:实现QAbstractItemDelegate的paint()函数。

这种方法和《C++_GUI_Qt4_编程(第二版)》中第十章的自定义委托例子,画星星的作法是一样的,都是通过 QApplication::style()->drawControl(QStyle::CE_CheckBox,&check_box_style_option,painter);这个函数,只不过选择的元素是QCheckBox的。

    使用Delegate Paint()函数 具体如下:


    void QLineDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const    
    {    
        bool checked = index.model()->data(index, Qt::DisplayRole).toBool();    
        
        if(index.column() == 0)    
        {    
            QStyleOptionButton check_box_style_option;    
            check_box_style_option.state |= QStyle::State_Enabled;    
            if(checked)    
            {    
                check_box_style_option.state |= QStyle::State_On;    
            }    
            else    
            {    
                check_box_style_option.state |= QStyle::State_Off;    
            }    
            check_box_style_option.rect = CheckBoxRect(option);    
            QApplication::style()->drawControl(QStyle::CE_CheckBox,&check_box_style_option,painter);    
        }    
        
        QStyledItemDelegate::paint(painter,option,index);    
        QStyleOptionViewItem itemOption(option);    
        if(itemOption.state & QStyle::State_HasFocus)    
            itemOption.state = itemOption.state ^ QStyle::State_HasFocus;    
        QStyledItemDelegate::paint(painter,itemOption,index);    
        QPen oldPen = painter->pen();    
        painter->setPen(pen);    
        painter->drawLine(option.rect.topRight(),option.rect.bottomRight());    
        painter->drawLine(itemOption.rect.bottomLeft(),itemOption.rect.bottomRight());    
        painter->setPen(oldPen);    
    }    

完整的源代码如下:

    class QLineDelegate : public QStyledItemDelegate  
    {  
        Q_OBJECT  
    public:  
        QLineDelegate(QTableView* tableView);  
    protected:  
        void paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const;  
        bool editorEvent(QEvent *event,  
                                             QAbstractItemModel *model,  
                                             const QStyleOptionViewItem &option,  
                                             const QModelIndex &index) ;  
    private:  
        QPen         pen;  
        QTableView*  view;  
    };  
      
    static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options)  
    {  
        QStyleOptionButton check_box_style_option;  
        QRect check_box_rect = QApplication::style()->subElementRect(  
            QStyle::SE_CheckBoxIndicator,  
            &check_box_style_option);  
      
        QPoint check_box_point(view_item_style_options.rect.x() +  
                               view_item_style_options.rect.width() / 2 -  
                               check_box_rect.width() / 2,  
                               view_item_style_options.rect.y() +  
                               view_item_style_options.rect.height() / 2 -  
                               check_box_rect.height() / 2);  
        return QRect(check_box_point, check_box_rect.size());  
    }  
      
      
    QLineDelegate::QLineDelegate(QTableView* tableView)  
    {  
        int gridHint = tableView->style()->styleHint(QStyle::SH_Table_GridLineColor, new QStyleOptionViewItemV4());  
        QColor gridColor = static_cast<QRgb>(gridHint);  
        pen = QPen(gridColor,0,tableView->gridStyle());  
        view = tableView;  
    }  
      
    void QLineDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const  
    {  
        bool checked = index.model()->data(index, Qt::DisplayRole).toBool();  
      
        if(index.column() == 0)  
        {  
            QStyleOptionButton check_box_style_option;  
            check_box_style_option.state |= QStyle::State_Enabled;  
            if(checked)  
            {  
                check_box_style_option.state |= QStyle::State_On;  
            }  
            else  
            {  
                check_box_style_option.state |= QStyle::State_Off;  
            }  
            check_box_style_option.rect = CheckBoxRect(option);  
            QApplication::style()->drawControl(QStyle::CE_CheckBox,&check_box_style_option,painter);  
        }  
      
        QStyledItemDelegate::paint(painter,option,index);  
        QStyleOptionViewItem itemOption(option);  
        if(itemOption.state & QStyle::State_HasFocus)  
            itemOption.state = itemOption.state ^ QStyle::State_HasFocus;  
        QStyledItemDelegate::paint(painter,itemOption,index);  
        QPen oldPen = painter->pen();  
        painter->setPen(pen);  
        painter->drawLine(option.rect.topRight(),option.rect.bottomRight());  
        painter->drawLine(itemOption.rect.bottomLeft(),itemOption.rect.bottomRight());  
        painter->setPen(oldPen);  
    }  
      
    bool QLineDelegate::editorEvent(QEvent *event,  
                                       QAbstractItemModel *model,  
                                       const QStyleOptionViewItem &option,  
                                       const QModelIndex &index) {  
        if((event->type() == QEvent::MouseButtonRelease) ||  
            (event->type() == QEvent::MouseButtonDblClick))  
        {  
            QMouseEvent *mouse_event = static_cast<QMouseEvent*>(event);  
            if(mouse_event->button() != Qt::LeftButton ||  
               !CheckBoxRect(option).contains(mouse_event->pos()))  
            {  
                return false;  
            }  
      
            if(event->type() == QEvent::MouseButtonDblClick)  
            {  
                return true;  
            }  
        }  
        else if(event->type() == QEvent::KeyPress)  
        {  
            if(static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&  
               static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)  
            {  
                return false;  
            }  
        }  
        else  
        {  
            return false;  
        }  
      
        bool checked = index.model()->data(index, Qt::DisplayRole).toBool();  
        return model->setData(index, !checked, Qt::EditRole);  
    }  


 不过有一个小问题,CheckBox旁边有false/true或其它字符的原因是:你的Model中的data返回值,应该排除这列。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值