#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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
说起下拉框,想必大家都比较熟悉,在我们注册一些网站的时候,会出现大量的地区数据供我们选择,这个时候出现的就是下拉框列表,再比如字体选择的时候也是使用的下拉框。下拉框到处可见,作为一个图形库,qt也提供了QtComboBox类来供我们使用,但是有些时候简单的下拉列表已经满足不了我们的需求,如58上选择岗位是一个下拉表格,这个时候就需要我们自己定制一下QComboBox。
资源推荐
资源详情
资源评论
收起资源包目录
combobox.zip (14个子文件)
combobox
Resources
icon1.jpg 7KB
combobox.ico 3KB
combobox.qrc 106B
FlowLayout.h 1KB
FlowLayout.cpp 3KB
combobox.vcxproj 11KB
stdafx.h 22B
main.cpp 196B
combobox.rc 47B
stdafx.cpp 21B
combobox.ui 801B
combobox.vcxproj.filters 3KB
combobox.cpp 8KB
combobox.h 2KB
共 14 条
- 1
一窝蜂117
- 粉丝: 580
- 资源: 44
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页