#include "stdafx.h"
#include "combobox.h"
CActionContentWidget::CActionContentWidget(const QString & text, QWidget * parent /*= nullptr*/) :QWidget(parent)
{
InitializeUI();
SetContentText(text);
}
CActionContentWidget::~CActionContentWidget()
{
}
void CActionContentWidget::SetContentText(const QString & text)
{
if (m_ActText)
{
m_ActText->setText(text);
}
}
void CActionContentWidget::SetItemIcon(const QString & icon, const QString & hover)
{
setStyleSheet(QString("QWidget#m_ContentWidget{background:#f0eeee;}"
"QWidget#m_ContentWidget[MouseHover=true]{background:#f2868b;height:30px;}"//f2868b
"QLabel{color:#777777;font-family:%1;font-size:12px;padding-left:8px;background:#f6f6f6;}"
"QLabel[MouseHover=true]{color:#ffffff;font-family:%1;font-size:12px;padding-left:8px;background:transparent;}"//f2868b
"QPushButton{border-image:url(%2); margin:2 2 2 2;}"
"QPushButton[MouseHover=true]{border-image:url(%3); margin:3 3 3 3;}"
).arg(QStringLiteral("微软雅黑")).arg(icon).arg(hover));
}
void CActionContentWidget::SetBackgroundRole(bool hover)
{
m_ContentWidget->setProperty("MouseHover", hover);
m_ActIcon->setProperty("MouseHover", hover);
m_ActText->setProperty("MouseHover", hover);
QString styleStr = styleSheet();
setStyleSheet("");
setStyleSheet(styleStr);
}
void CActionContentWidget::enterEvent(QEvent * event)
{
qDebug() << "CActionContentWidget::enterEvent";
QWidget::enterEvent(event);
}
void CActionContentWidget::leaveEvent(QEvent * event)
{
qDebug() << "CActionContentWidget::leaveEvent" << event->type();
QWidget::leaveEvent(event);
}
bool CActionContentWidget::eventFilter(QObject * obj, QEvent * event)
{
if (obj == m_ActIcon || obj == m_ActText)
{
if (QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event))
{
if (QEvent::MouseMove == event->type())
{
qDebug() << "event->type()" << event->type();
}
}
}
return QWidget::eventFilter(obj, event);
}
void CActionContentWidget::mousePressEvent(QMouseEvent * event)
{
if (event->button() == Qt::LeftButton)
{
m_Mouse_press = true;
}
}
void CActionContentWidget::mouseReleaseEvent(QMouseEvent * event)
{
if (m_Mouse_press)
{
emit showText(m_ActText->text());
m_Mouse_press = false;
}
}
void CActionContentWidget::InitializeUI()
{
m_ContentWidget = new QWidget();
m_ActIcon = new QPushButton();
m_ActText = new QLabel();
QHBoxLayout * mainLayout = new QHBoxLayout();
connect(m_ActIcon, &QPushButton::clicked, this, [this]{
emit showText(m_ActText->text());
});
m_ActIcon->installEventFilter(this);
m_ActText->installEventFilter(this);
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
m_ActIcon->setFixedSize(45, 45);
m_ContentWidget->setObjectName(QStringLiteral("m_ContentWidget"));
m_ActIcon->setObjectName(QStringLiteral("ActionIcon"));
m_ActText->setObjectName(QStringLiteral("m_ActText"));
mainLayout->addWidget(m_ActIcon);
mainLayout->addWidget(m_ActText);
m_ContentWidget->setLayout(mainLayout);
QVBoxLayout * fwLayout = new QVBoxLayout();
fwLayout->setMargin(0);
fwLayout->setSpacing(0);
fwLayout->addWidget(m_ContentWidget);
setLayout(fwLayout);
}
void CheckBox::checkStateSet()
{
QCheckBox::checkStateSet();
}
bool CheckBox::hitButton(const QPoint & pos) const
{
bool r = QCheckBox::hitButton(pos);
qDebug() << "r" << r;
return true;
}
void CheckBox::nextCheckState()
{
QCheckBox::nextCheckState();
}
combobox::combobox(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
ConstructList();
// ConstructTable();
}
combobox::~combobox()
{
}
bool combobox::eventFilter(QObject * watched, QEvent * event)
{
if (watched->inherits("QWidget") && event->type() == QEvent::Paint)
{
if (CActionContentWidget * actionItem = dynamic_cast<CActionContentWidget *>(watched))
{
if (actionItem->rect().contains(actionItem->mapFromGlobal(QCursor::pos())))
{
actionItem->SetBackgroundRole(true);
}
else
{
actionItem->SetBackgroundRole(false);
}
}
}
return QWidget::eventFilter(watched, event);
}
void combobox::ConstructList()
{
QListWidget * listWidget = new QListWidget;
listWidget->setViewMode(QListView::ListMode);
for (int i = 0; i < 5; i++)
{
CActionContentWidget * itemWidget = new CActionContentWidget("fawefawe");
connect(itemWidget, &CActionContentWidget::showText, this, [this, listWidget](const QString & text){
ui.comboBox->hidePopup();
if (ui.comboBox->isEditable())
{
ui.comboBox->setCurrentText(text);
}
else
{
for (int c = 0; c < listWidget->count(); ++c)
{
CActionContentWidget * itemWidget = dynamic_cast<CActionContentWidget *>(listWidget->itemWidget(listWidget->item(c)));
if (itemWidget == sender())
{
ui.comboBox->setCurrentIndex(c);
}
}
}
});
itemWidget->SetItemIcon(":/combobox/Resources/icon1.jpg", ":/combobox/Resources/icon1.jpg");
itemWidget->setFixedHeight(45);
itemWidget->setFixedWidth(150);
QListWidgetItem * item = new QListWidgetItem(listWidget);
item->setText(QStringLiteral("%1").arg(i));
// item->setCheckState(Qt::Checked);
listWidget->setItemWidget(item, itemWidget);
listWidget->addItem(item);
itemWidget->installEventFilter(this);
}
ui.comboBox->setModel(listWidget->model());
ui.comboBox->setView(listWidget);
ui.comboBox->setEditable(true);//是否支持下拉框可编辑
ui.comboBox->setMaxVisibleItems(3);
setStyleSheet("QComboBox{border:1px solid gray;}"
"QComboBox QAbstractItemView::item{height:45px;}" //下拉选项高度
"QComboBox::down-arrow{image:url(:/combobox/Resources/icon1.jpg);}" //下拉箭头
"QComboBox::drop-down{border:0px;}"); //下拉按钮
}
void combobox::ConstructTable()
{
setStyleSheet("QComboBox QAbstractItemView {selection-background-color: transparent;}");
QTableWidget * tableWidget = new QTableWidget(3, 3);
tableWidget->verticalHeader()->setVisible(false);
tableWidget->horizontalHeader()->setVisible(false);
tableWidget->setShowGrid(false);
for (int i = 0; i < 3; ++i)
{
tableWidget->setColumnWidth(i, 49);
tableWidget->setRowHeight(i, 50);
for (int j = 0; j < 3; ++j)
{
/* CActionContentWidget *itemWidget = new CActionContentWidget("fawefawe");
itemWidget->SetItemIcon(":/combobox/Resources/icon1.jpg", ":/combobox/Resources/icon1.jpg");
itemWidget->setFixedHeight(50);
itemWidget->setFixedWidth(50);*/
CheckBox * itemWidget = new CheckBox;
itemWidget->setFixedSize(50, 50);
itemWidget->setStyleSheet(QString("QCheckBox {background-color:lightgray;}"
"QCheckBox:checked{background-color:white;}"));
connect(itemWidget, &QCheckBox::clicked, this, [this]{
QObject * sender = this->sender();
if (QCheckBox * item = dynamic_cast<QCheckBox *>(sender))
{
QString text = ui.comboBox->currentText();
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());
}
}
ui.comboBox->setCurrentText(text);
}
});
itemWidget->setText(QStringLiteral("好%1").arg(i));
itemWidget->setCheckable(true);
// tableWidget->setItem(i, j, new QTableWidgetItem(QStringLiteral("热分%1").arg(i)));
tableWidget->setCellWidget(i, j, itemWidget);
itemWidget->installEventFilter(this);
}
}
ui.comboBox->setModel(tableWidget->model());
ui.comboBox->setView(tableWidge
- 1
- 2
前往页