头文件:
#pragma once
#include <QToolButton>
#include <QListView>
#include <QWidgetAction>
#include <QPushButton>
#include <QHBoxLayout>
#include <QMenu>
#include <QStandardItemModel>
#include <qdebug.h>
class CustomComBox : public QPushButton
{
Q_OBJECT
public:
CustomComBox();
CustomComBox(int index);
QListView * getList();//可以用来修改列表字体大小
int currentIndex();
void setCurrentIndex(int index);
QString currentText();
void setCurrentText(QString str);
void addItems(QStringList sstrList);
void clear();
private:
int _index;//版本控制
int _count;//当前comboBox总条数
QMenu *_menu;
QListView * _listView;
QWidgetAction* _widgetaction;
QWidget *_btnWidget;
QHBoxLayout* _layoutH;
QStandardItemModel *_ItemModel;
public slots:
void on_showClick(QModelIndex index);
protected:
void resizeEvent(QResizeEvent *event);
};
cpp文件:
#include "customcombox.h"
#include "../resource/resource_def.h"
CustomComBox::CustomComBox()
{
_index = 0;
_count = 0;
setFixedSize(100, 25);
//右侧长宽控制方向
//setPopupMode(QToolButton::InstantPopup);
_menu = new QMenu(this);
_listView = new QListView();
_widgetaction = new QWidgetAction(_listView);
_listView->setFrameShape(QFrame::NoFrame);//设置无边框
//设置弹出的菜单大小
_listView->setFixedSize(100,20);//没有的时候弄宽一点好看些
_widgetaction->setDefaultWidget(_listView);
_menu->addAction(_widgetaction);
setMenu(_menu);
_ItemModel = new QStandardItemModel(this);
QObject::connect(_listView, &QListView::clicked, this, &CustomComBox::on_showClick);//绑定事件
}
//不传0
CustomComBox::CustomComBox(int index)
{
_index = index;
_count = 0;
setFixedSize(100, 25);
//右侧长宽控制方向
//setPopupMode(QToolButton::InstantPopup);
_menu = new QMenu(this);
_listView = new QListView();
_widgetaction = new QWidgetAction(_listView);
_listView->setFrameShape(QFrame::NoFrame);//设置无边框
//设置弹出的菜单大小
_listView->setFixedSize(100, 20);//没有的时候弄宽一点好看些
_widgetaction->setDefaultWidget(_listView);
_menu->addAction(_widgetaction);
setMenu(_menu);
_ItemModel = new QStandardItemModel(this);
QObject::connect(_listView, &QListView::clicked, this, &CustomComBox::on_showClick);//绑定事件
}
QString CustomComBox::currentText()
{
return this->text();
}
int CustomComBox::currentIndex() {
return _listView->currentIndex().row();
}
void CustomComBox::addItems(QStringList strList) {
_ItemModel->clear();
_count = strList.size();
for (int i = 0; i < _count; i++)
{
QString string = static_cast<QString>(strList.at(i));
QStandardItem *item = new QStandardItem(string);
_ItemModel->appendRow(item);
if (i == 0) {
setText(string);
}
}
_listView->setModel(_ItemModel); // listview设置Model
if (_count > 0) {
int height = _count * _listView->sizeHintForRow(0);//自适应高度
_listView->setFixedHeight(height);//设置高度
}
if (strList.size() > 0) {
_listView->setCurrentIndex(_ItemModel->index(0, 0));
}
}
QListView* CustomComBox::getList() {
if (_listView != NULL) {
return _listView;
}
}
void CustomComBox::on_showClick(QModelIndex index)
{
QString strTemp;
strTemp = index.data().toString();
setText(strTemp);
int a = currentIndex();
QString text = currentText();
QString curInfo = QString("index:%1,indecText:%2").arg(a).arg(text);
qDebug(curInfo.toLocal8Bit().data());
_menu->hide();
}
void CustomComBox::setCurrentIndex(int index) {
int row = _ItemModel->rowCount();
if (index >= row) {
return;
}
QModelIndex modelIndex = _ItemModel->index(index, 0);
_listView->setCurrentIndex(modelIndex);
setText(modelIndex.data().toString());
}
void CustomComBox::setCurrentText(QString str) {
int row = _ItemModel->rowCount();
for (int i = 0; i < row; i++)
{
QModelIndex modelIndex = _ItemModel->index(i, 0);
if (modelIndex.data().toString() == str) {
setText(str);
_listView->setCurrentIndex(modelIndex);
}
}
}
void CustomComBox::clear() {
_ItemModel->clear();
this->setText("");
}
//尺寸自适应
void CustomComBox::resizeEvent(QResizeEvent *event)
{
int height, width;
width = this->width();
height = this->height();
_listView->setFixedWidth(width);
QString style;
if (_index == 0)
{
style = QString(
"CustomComBox{padding-left:5px;text-align:left;background-color:#ECF5FF;font-size:14px;border:1px solid #CCCCCC;color:#409EFF;font-family:Microsoft YaHei;border-radius: 3px;}"
"CustomComBox:menu-indicator{image:url(" UI_DROP_DOWN_ICONB ");width:20px;height:%2;}"
"QListView:item:selected{background-color:#cbe6ff;color:#409EFF;}"
"QListView:item:hover{background-color:#cbe6ff;color:#409EFF;}"
"QListView{background-color:#ECF5FF;color:#409EFF;}"
"QAbstractItemView{outline:0px;}"//选中日期样式,去掉虚线框
"QMenu{border:none;}").arg(height - 2);
}
else if(_index == 1)
{
style = QString(
"CustomComBox{padding-left:5px;text-align:left;border:1px solid #CCCCCC;font-size:14px;background-color:#ffffff;border-radius: 3px;font-family:Microsoft YaHei;}"
"CustomComBox:menu-indicator{image:url(" UI_IMAGE_QCOMBOBOX_DROP_DOWN ");width:20px;height:%2;}"
"QListView:item:selected{background-color:#cbe6ff;color:black;}"
//"QListView:item:hover{background-color:#cbe6ff;color:black;}"
"QListView{background-color:#ffffff;}"
"QAbstractItemView{outline:0px;}"//选中日期样式,去掉虚线框
"QMenu{border:none;}").arg(height - 2);
}
setStyleSheet(style);//更新样式,主要右边箭头位置
}