窗体部件之QComboBox

QComboBox的样式表设定:

这里写图片描述

给QComboBox添加项两种方式:

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QListView>
#include <QDebug>
#include <QMessageBox>

#include "accountitem.h"

#include "itemdelegate.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    createComboBoxItem();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::createComboBoxItem()
{
//    //方法一:使用comboBox的addItem方法,并且可以配合使用setItemDelegate委托方法进行重绘
    ui->comboBox->addItem("1");
    ui->comboBox->addItem("2");
    ui->comboBox->addItem("3");
    ui->comboBox->addItem("4");
    ui->comboBox->addItem("5");

    ItemDelegate *pDelegate = new ItemDelegate(this);
    ui->comboBox->setItemDelegate(pDelegate);

    connect(pDelegate, SIGNAL(deleteItem(QModelIndex)), this, SLOT(deleteItemSlot(QModelIndex)));


//    //方法二:使用QListWidget的setItemWidget设置ComboBox自定义代理组件
//    listWidget = new QListWidget(this);
//    ui->comboBox->setModel(listWidget->model());
//    ui->comboBox->setView(listWidget);

//    for(int i = 0; i < 3; i++)
//    {
//        AccountItem *accountItem = new AccountItem(this);
//        accountItem->setAccountNumber(QString("I'm ") + QString::number(i, 10));

//        QListWidgetItem *listWidgetItem = new QListWidgetItem(listWidget);
//        listWidget->setItemWidget(listWidgetItem, accountItem);
//    }
}

void Widget::deleteItemSlot(QModelIndex index)
{
    if (QMessageBox::question(this, QStringLiteral("提示"), QStringLiteral("确认要删除所选账号吗?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
    {
        ui->comboBox->removeItem(index.row());
    }
}

itemdelegate.cpp

#include "itemdelegate.h"

#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QApplication>
#include <QToolTip>

ItemDelegate::ItemDelegate(QObject * parent)
    : QStyledItemDelegate(parent)
{

}

void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem  viewOption(option);
    if (viewOption.state & QStyle::State_HasFocus)
    {
        viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
    }

    QStyledItemDelegate::paint(painter, viewOption, index);

    int height = viewOption.rect.height();
    QPixmap pixmap = QPixmap(":/images/delete.png");
    QRect decorationRect = QRect(viewOption.rect.left() + viewOption.rect.width() - height, viewOption.rect.top() , height, height);
    painter->drawPixmap(decorationRect, pixmap);
}

bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    int height = option.rect.height() - 4;
    QRect decorationRect = QRect(option.rect.left() + option.rect.width() - height, option.rect.top() , height, height);

    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    if (event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
    {
        emit deleteItem(index);
    }

    if (event->type() == QEvent::MouseMove && decorationRect.contains(mouseEvent->pos()))
    {
        QCursor cursor(Qt::PointingHandCursor);
        QApplication::setOverrideCursor(cursor);
        QString strText = QStringLiteral("删除账号信息");
        QToolTip::showText(mouseEvent->globalPos(), strText);
//        QToolTip::showText(mouseEvent->globalPos(), strText, 0, QRect(), 500);
    }
    else
    {
        QCursor cursor(Qt::ArrowCursor);
        QApplication::setOverrideCursor(cursor);
        QToolTip::showText(mouseEvent->globalPos(), "");
    }

    return QStyledItemDelegate::editorEvent(event, model, option, index);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值