hydelios 2021-05-12 11:07 采纳率: 20%
浏览 85
已采纳

qt QtItemDelegate实现paint遇到问题

我打算在表格的某列里实现多个按键,用的QtItemDelegate实现paint,我使用了QVector,但不知道为什么在向button里push_back的时候程序会崩溃


void ButtonDelegateAC::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QVector<QStyleOptionButton*>* buttons = m_btns.value(index);

    if (!buttons) {
        QStyleOptionButton* button1 = new QStyleOptionButton();
        //button1->rect = option.rect.adjusted(4, 4, -(option.rect.width() / 2 + 4) , -4); //
        button1->text = "编辑账户";
        button1->state |= QStyle::State_Enabled;
        buttons->push_back(button1);

        QStyleOptionButton* button2 = new QStyleOptionButton();
        //button2->rect = option.rect.adjusted(button1->rect.width() + 4, 4, -4, -4);
        button2->text = "注销账户";
        button2->state |= QStyle::State_Enabled;
        buttons->push_back(button2);

        QStyleOptionButton* button3 = new QStyleOptionButton();
        //button2->rect = option.rect.adjusted(button1->rect.width() + 4, 4, -4, -4);
        button3->text = "注销账户";
        button3->state |= QStyle::State_Enabled;
        buttons->push_back(button3);

        QStyleOptionButton* button4 = new QStyleOptionButton();
        //button2->rect = option.rect.adjusted(button1->rect.width() + 4, 4, -4, -4);
        button4->text = "注销账户";
        button4->state |= QStyle::State_Enabled;
        buttons->push_back(button4);
        (const_cast<ButtonDelegateAC *>(this))->m_btns.insert(index, buttons);
    }
    (*buttons)[0]->rect = option.rect.adjusted(4, 4, -(option.rect.width() / 2 + 4) , -4); //
    (*buttons)[1]->rect = option.rect.adjusted((*buttons)[0]->rect.width() + 4, 4, -4, -4);
    (*buttons)[2]->rect = option.rect.adjusted((*buttons)[1]->rect.width() + 4, 4, -4, -4); //
    (*buttons)[3]->rect = option.rect.adjusted((*buttons)[2]->rect.width() + 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, (*buttons)[0], painter);
    QApplication::style()->drawControl(QStyle::CE_PushButton, (*buttons)[1], painter);
    QApplication::style()->drawControl(QStyle::CE_PushButton, (*buttons)[2], painter);
    QApplication::style()->drawControl(QStyle::CE_PushButton, (*buttons)[3], painter);
}

其中m_btns是一个map

m_btns.value(index);

定义如下

typedef QMap<QModelIndex, QVector<QStyleOptionButton*>* >  collButtons;
    collButtons m_btns;
  • 写回答

2条回答 默认 最新

  • qfl_sdu 2021-05-12 12:04
    关注

    buttons 可能是空指针,跟一下看看吧,如有帮助,请采纳一下,谢谢。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效