首页 > 其他分享 >自定义QComboBox解决QCombobox设置样式并与QDateTime一起使用时候弹出选项乱跳

自定义QComboBox解决QCombobox设置样式并与QDateTime一起使用时候弹出选项乱跳

时间:2024-02-23 15:58:32浏览次数:19  
标签:index QListView 自定义 CustomComBox int QComboBox QCombobox color listView

头文件:

#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);//更新样式,主要右边箭头位置
}

标签:index,QListView,自定义,CustomComBox,int,QComboBox,QCombobox,color,listView
From: https://www.cnblogs.com/flygreen/p/17657322.html

相关文章

  • k8s prometheus监控自定义exporter接口
    案例1:我有的k8s中所有pod应用资源监控接口是/actuator/prometheus,但是默认prometheus监控的是/metrics,这是需要修改prometheus-server的configmap,修改抓取资源监控的api接口需要找到kubernetes-service-endpoints这一项,然后找到action:replace,然后添加replacement案例2:如果......
  • 如何自定义配置
    IConfigurationSource  build()方法IConfigurationProvider1usingCSRedis;2usingMicrosoft.Extensions.Configuration;3usingMicrosoft.Extensions.Primitives;4usingSystem;5usingSystem.Collections.Generic;6usingSystem.Text;78namespac......
  • Angular Material 17+ 高级教程 – Custom Themes (自定义主题) for Material Design
    前言AngularMaterialv17.2.0发布了MaterialDesign3Theme   上一篇 AngularMaterial17+高级教程–GetStarted下一篇TODO想查看目录,请移步 Angular17+高级教程–目录......
  • 引入JavaScript自定义提示信息(账户密码不为空,必须勾选用户协议)
     当不输入用户或密码就登录时不勾选用户协议就登录时form表单的修改,引入了onsubmit控件,提交表单时,调用validateForm函数<formaction="/home"method="post"onsubmit="returnvalidateForm()">validateForm函数内容functionvalidateForm(){//验证用户名和密......
  • FastReport调用自定义方法
     在FastReport内部写一个方法,如果直接写入数值,可以被调用成功。 这种情况可以被正常调用。 但是在使用传递进来的变量,就会说不正确。类似:[SecondToString([Table1.Error_TimeTotal])],这种,就是不行。解决办法,使用事件处理,注意,要使用AfterData。方法如上图。 ......
  • el-form表单使用pattern自定义校验规则
    //正则校验的正则表达式,这里注意正则表达式中的‘\’要使用‘\\’转义constpatterns={"name":"^[a-zA-Z_][0-9a-zA-Z_]{0,}$","tel":"^1[2-9]\\d{0,}$","email":"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-......
  • 自定义控件 creator 2.4
     这个分页栏中可以收集用户自己建立的 预制资源(Prefab),方便重复多次创建和使用。要添加自定义的预制控件,只需要从 资源管理器 中拖拽相应的预制资源(Prefab)到自定义控件分页,即可完成创建。右键点击自定义控件中的元素,可以选择重命名、从控件库中删除该控件以及更换控件图标......
  • Android里使用AspectJ实现双击自定义注解
    创建注解首先创建一个双击注解。importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;​/***<pre>*desc:双击*author:刘金*......
  • 1 Spring5 自定义标签开发
    spring5 自定义脚本开发步骤1 定义bean,publicclassUser{privateStringid;privateStringuserName;privateStringemail;privateStringpassword;publicStringgetId(){returnid;}publicvoidsetId(St......
  • 一文了解广州自定义表单开源的诸多优势特点
    广州自定义表单开源可以助力企业实现提质增效、降低人工成本的发展目的,因而在通信业、电力、高校、制造业、医疗等很多行业中获得了大家的点赞与支持。广州流辰信息是一家专业性强的低代码技术平台服务商,潜心研发广州自定义表单开源工具,与客户朋友一起创造更具价值的辉煌前程。那......