#include "stdafx.h"
#include "CustomPopupComboBox.h"
bool CCheckBoxItem::hitButton(const QPoint & pos) const
{
return true;
}
CComboBox::CComboBox(CustomPopupComboBox::ItemType type, QWidget * parent /*= nullptr*/)
: QWidget(parent)
, m_Type(type)
{
InitializeUI();
}
void CComboBox::SetSeparatorSymbol(char symbol)
{
if (m_SeparatorSymbol != symbol && m_Type == CustomPopupComboBox::CheckBoxItem)
{
QString currentText = m_ComboBox->currentText();
currentText.replace(m_SeparatorSymbol, symbol);
m_SeparatorSymbol = symbol;
}
}
void CComboBox::AddText(const QString & text)
{
AddItem(text);
}
void CComboBox::AddTexts(const QVector<QString> & items)
{
for (auto itemItor = items.begin(); itemItor != items.end(); ++itemItor)
{
AddItem(*itemItor);
}
}
void CComboBox::SetItemWidth(int width)
{
m_ItemWidth = width;
m_ComboBox->view()->setFixedWidth(m_ItemWidth);
ResetItemWidth(width);
}
void CComboBox::SetItemHeight(int height)
{
m_ItemWidth = height;
ResetItemHeight(height);
}
void CComboBox::SetMaxVisibleCount(int count)
{
}
bool CComboBox::eventFilter(QObject * object, QEvent * event)
{
if (QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event))
{
if (mouseEvent->type() == QEvent::MouseButtonPress)
{
if (QLabel * label = dynamic_cast<QLabel *>(object))
{
QString text = label->text();
m_ComboBox->setCurrentText(text);
return true;
}
}
}
return QWidget::eventFilter(object, event);
}
QWidget * CComboBox::NewItem(const QString & text)
{
QWidget * item;
switch (m_Type)
{
case CustomPopupComboBox::LabelItem:
{
QPushButton * pushButton = new QPushButton(text);
connect(pushButton, &QPushButton::clicked, this, [this](){
if (QPushButton * item = dynamic_cast<QPushButton *>(this->sender()))
{
QString text = item->text();
if (text.isEmpty())
{
return;
}
static QPushButton * oldButton = nullptr;
if (oldButton)
{
oldButton->setChecked(false);
}
oldButton = item;
m_ComboBox->setCurrentText(text);
item->setCheckable(true);
item->setChecked(true);
}
});
item = pushButton;
}
break;
case CustomPopupComboBox::RadioItem:
{
CRadioButtonItem * radioButton = new CRadioButtonItem(text);
connect(radioButton, &QRadioButton::clicked, this, [this](bool checked){
if (checked)
{
if (QRadioButton * item = dynamic_cast<QRadioButton *>(this->sender()))
{
QString text = item->text();
if (text.isEmpty())
{
return;
}
m_ComboBox->setCurrentText(text);
}
}
});
item = radioButton;
}
break;
case CustomPopupComboBox::CheckBoxItem:
{
CCheckBoxItem * checkBox = new CCheckBoxItem(text);
checkBox->setFocus();
connect(checkBox, &QCheckBox::clicked, this, [this]{
if (QCheckBox * item = dynamic_cast<QCheckBox *>(this->sender()))
{
QString text = item->text();
if (text.isEmpty())
{
return;
}
if (m_BottomWidget == nullptr || m_BottomWidget->isHidden())
{
text = m_ComboBox->currentText();
}
else
{
text = m_CurrentMemory;
}
if (text.isEmpty() == false)
{
if (item->isChecked())
{
if (text.split("|").indexOf(item->text()) == -1)
{
text.append("|" + item->text());
}
}
else
{
text.remove("|" + item->text());
text.remove(item->text());
if (text.size() != 0 && text.at(0) == '|')
{
text.remove(0, 1);
}
}
}
else
{
if (item->isChecked())
{
text.append(item->text());
}
}
if (m_BottomWidget == nullptr || m_BottomWidget->isHidden())
{
m_ComboBox->setCurrentText(text);
}
else
{
m_CurrentMemory = text;
}
}
});
item = checkBox;
}
break;
default:
break;
}
return item;
}
void CComboBox::InitializeUI()
{
QVBoxLayout * mainLayout = new QVBoxLayout;
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
m_ComboBox = new QComboBox(this);
m_ComboBox->setMaxVisibleItems(100);
if (QWidget * parent = m_ComboBox->view()->parentWidget())
{
parent->setAttribute(Qt::WA_TranslucentBackground);
parent->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
//取消弹框阴影
HWND hwnd = reinterpret_cast<HWND>(parent->winId());
DWORD class_style = ::GetClassLong(hwnd, GCL_STYLE);
class_style &= ~CS_DROPSHADOW;
::SetClassLong(hwnd, GCL_STYLE, class_style); // windows系统函数
}
mainLayout->addWidget(m_ComboBox);
setLayout(mainLayout);
}
CListPopupComboBox::CListPopupComboBox(CustomPopupComboBox::ItemType type, QWidget * parent)
: CComboBox(type, parent)
{
ConstructView();
m_ComboBox->view()->setFixedWidth(m_ItemWidth);
setStyleSheet(QString(
"QComboBox QAbstractItemView::item{height:45px;background:transparent;}" //下拉选项高度
"QComboBox QAbstractItemView::item:hover{height:145px;}" //下拉选项高度 //background-color:red;
"QComboBox::down-arrow{background:#34752;}" //下拉箭头image:url(:/combobox/Resources/icon1.jpg);
"QComboBox::drop-down{border:1 solid red;}"
"QWidget#PopupWidgetBottom{background:gray;}"
"QComboBox QAbstractItemView {selection-background-color: transparent;}"//background:transparent;
"QComboBox{min-height:20px;border:1 solid #BBBBBB;border-radius:5px;}"
"QRadioButton{ padding-left:10px;}"
"QCheckBox{ padding-left:10px;}"
"QLabel{ padding-left:10px;}"
"QPushButton{text-align: left;background:transparent;padding-left:10px;}"
"QListWidget{margin-top:0px;}")); //下拉按钮
}
CListPopupComboBox::~CListPopupComboBox()
{
}
bool CListPopupComboBox::eventFilter(QObject * watched, QEvent * event)
{
if (m_BottomWidget && m_BottomWidget == watched)
{
if (QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event))
{
if (mouseEvent->type() == QEvent::MouseButtonPress
|| mouseEvent->type() == QEvent::MouseButtonRelease)
{
return true;
}
}
}
return QWidget::eventFilter(watched, event);
}
void CListPopupComboBox::AddItem(const QString & text)
{
if (QListWidget * listWidget = dynamic_cast<QListWidget *>(m_PopupWidget))
{
QWidget * itemWidget = NewItem(text);
//itemWidget->setStyleSheet(QString("QCheckBox {background-color:lightgray;}"
// "QCheckBox:checked{background-color:white;}"));
itemWidget->setFixedSize(m_ItemWidth, m_ItemHeight);
int pos = listWidget->count() - 1 < 0 ? 0 : listWidget->count() - 1;
listWidget->insertItem(pos, new QListWidgetItem());
listWidget->setItemWidget(listWidget->item(pos), itemWidget);
}
}
void CListPopupComboBox::ResetItemWidth(int width)
{
if (QListWidget * listWidget = dynamic_cast<QListWidget *>(m_PopupWidget))
{
for (int i = 0; i < listWidget->count(); ++i)
{
if (QWidget * w = listWidget->itemWidget(listWidget->item(i)))
{
w->setFixedWidth(width);
}
}
}
}
void CListPopupComboBox::ResetItemHeight(int height)
{
if (QListWidget * listWidget = dynamic_cast<QListWidget *>(m_PopupWidget))
{
for (int i = 0; i < listWidget->count(); ++i)
{
if (QWidget * w = listWidget->itemWidget(listWidget->item(i)))
{
w->setFixedHeight(height);
}
}
}
}
void CListPopupComboBox::ConstructView()
{
m_PopupWidget = new QListWidget;
QListWidget * listWidget;
if ((listWidget = dynamic_cast<QListWidget *>(m_PopupWidget)) == nullptr)
{
return;
}
listWidget->setViewMode(QListView::ListMode);
#ifndef ImmediateShow
m_BottomWidget = new QWidget;
QHBoxLayout * bottomLayout = new QHBoxLayout;
QPushButton * okPushButton = new QPushButton;
m_BottomWidget->setObjectName(QStringLiteral("PopupWidgetBottom"));
connect(okPushButton, &QPushButton::clicked, th
评论1
最新资源