窗体部件之QComboBox
来源:互联网 发布:软件开发标书 编辑:程序博客网 时间:2024/06/04 18:48
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);}
阅读全文
0 0
- 窗体部件之QComboBox
- 窗体部件之Buddy
- 窗体部件之Dialogs
- 窗体部件之QActionGroup
- 窗体部件之QButtonGroup
- 窗体部件之QCompleter
- 窗体部件之QDesktopWidget
- 窗体部件之QDockWidget
- 窗体部件之QMdiArea
- 窗体部件之QWizard
- 窗体部件之QSizeGrip
- 窗体部件之QSystemTrayIcon
- 窗体部件之QToolBar
- 窗体部件之QToolBox
- 窗体部件之QCheckBox
- 窗体部件之QMenu
- 窗体部件之QTabWidget
- 窗体部件之QProgressBar
- Android关于Theme.AppCompat相关问题的深入分析 6
- cccc
- 【Python】获取windows桌面路径的5中方式
- 3、<meta>标记、标题标记<h1>-<h6>标记及滚动字幕标记<marquee></marquee>
- 【补充】NAND FLASH大页和小页的区别
- 窗体部件之QComboBox
- Eclipse 使用的一些设置---持续更新
- oop面向对象--第十五天
- 手机壁纸设置相关
- spring 自动装配 default-autowire="byName/byType"
- HDU 1073 Online Judge(水~)
- 多个".h"文件中声明及定义 全局变量和函数
- oracle驱动加载的区别
- tensorflow学习笔记(1):编程风格,基本原理,入门教程(更新中...)