学习记录-tableview委托之按键问题记录

Qt的tableview实现按键委托的问题:

1、使用一般的方法,即只是用createEditor、setEditorData、setModelData、updateEditorGeometry 

这几个函数实现的话,每次必须双击单元格,才会显示出按键;

2、绘制的按钮大小不会随着单元格的变化而变化;

为了解决这两个问题使用paint函数和editorEvent函数替换createEditor函数画出按键,问题解决。代码如下:

bool BDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if((1 == index.column()) && (14 != model->data(model->index(index.row(),0)))){
        if (event->type() == QEvent::MouseButtonPress) {

            QMouseEvent* e =(QMouseEvent*)event;

            if (option.rect.adjusted(4, 4, -4, -4).contains(e->x(), e->y()) && m_btns.contains(index)) {
                m_btns.value(index)->state |= QStyle::State_Sunken;
            }
        }
        if (event->type() == QEvent::MouseButtonRelease) {
            QMouseEvent* e =(QMouseEvent*)event;
            if (option.rect.adjusted(4, 4, -4, -4).contains(e->x(), e->y()) && m_btns.contains(index)) {
                m_btns.value(index)->state &= (~QStyle::State_Sunken);
                qDebug() << model->data(model->index(index.row(),0));
                ConItem01 *d = new ConItem01(); //弹窗提示
                d->setGeometry(0, 0, 800, 400);
                d->move(QApplication::desktop()->screenGeometry().center() - d->rect().center());
</pre><pre name="code" class="cpp"><span style="white-space:pre">		</span>d->exec();
                delete d;
            }

        return true;
    }
    return false;
}

void BDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(0 == index.column()){ //这个放第一行会出现显示多出一个标号,除非使用else if结构
        QStyleOptionViewItemV4 myOption = option;
        int x = index.model()->data(index, Qt::EditRole).toUInt();
        QString text = Items.at(x);
        myOption.text = text;
        QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
    }else if(1 == index.column() && (14 != index.model()->data(index.model()->index(index.row(),0)))){
        QStyleOptionButton* button = m_btns.value(index);
        if (!button) {
            button = new QStyleOptionButton();
            //button->rect = option.rect.adjusted(4, 4, -4, -4);//放这里,固定大小
            button->text = "设置参数";
            button->state |= QStyle::State_Enabled;

            (const_cast<BDelegate *>(this))->m_btns.insert(index, button);
        }
        button->rect = option.rect.adjusted(4, 4, -4, -4); //放这里,根据单元格大小变化
        painter->save();

        if (option.state & QStyle::State_Selected) {
            painter->fillRect(option.rect, option.palette.highlight());

        }
        painter->restore();
        QApplication::style()->drawControl(QStyle::CE_PushButton, button, painter);
    }else {
        return QItemDelegate::paint (painter, option, index);
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值