仿QQ登录界面的QComboBox

2023-10-10 01:48
文章标签 登录 界面 qcombobox qq

本文主要是介绍仿QQ登录界面的QComboBox,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自: http://blog.sina.com.cn/s/blog_a6fb6cc90101ed6n.html



1.  列表项, 每一个列表项都是一个小的Widget

AccountItem.h

#ifndef ACCOUNTITEM_H
#define ACCOUNTITEM_H#include <QLabel>
#include <QWidget>
#include <QPushButton>
#include <QMouseEvent>
#include "ui_AccountItem.h"class CAccountItem : public QWidget
{Q_OBJECTpublic:CAccountItem(QWidget *parent = 0);CAccountItem(const QString& name, const QString& number, QWidget *parent = 0);~CAccountItem();void SetAccountNumber(const QString& number);QString GetAccountNumber() const;void SetNickName(const QString& name);QString GetNickName() const;signals:void sigShowAccount(QString);void sigRemoveAccount(QString);private slots:void OnRemoveAccount();protected:virtual void mousePressEvent(QMouseEvent *event);virtual void mouseReleaseEvent(QMouseEvent *event);private:Ui::AccountItem ui;bool mouse_press;
};#endif // ACCOUNTITEM_H

AccountItem.cpp

#include "AccountItem.h"
#include <QHBoxLayout>CAccountItem::CAccountItem(QWidget *parent): QWidget(parent)
{ui.setupUi(this);mouse_press = false;ui.labImage->setPixmap(QPixmap(":/Resources/Avatar.png").scaled(50,50));QPixmap pixmap(":/Resources/delete.png");ui.btnDel->setIcon(pixmap);ui.btnDel->setIconSize(QSize(20,20));ui.btnDel->setStyleSheet("background:transparent;");connect(ui.btnDel, SIGNAL(clicked()), this, SLOT(OnRemoveAccount()));
}CAccountItem::CAccountItem( const QString& name, const QString& number, QWidget *parent /*= 0*/ ): QWidget(parent)
{ui.setupUi(this);mouse_press = false;ui.labNickName->setText(name);ui.labNumber->setText(number);ui.labImage->setPixmap(QPixmap(":/Resources/Avatar.png").scaled(50,50));QPixmap pixmap(":/Resources/delete.png");ui.btnDel->setIcon(pixmap);ui.btnDel->setIconSize(QSize(20,20));ui.btnDel->setStyleSheet("background:transparent;");connect(ui.btnDel, SIGNAL(clicked()), this, SLOT(OnRemoveAccount()));
}CAccountItem::~CAccountItem()
{
}void CAccountItem::SetAccountNumber(const QString& number)
{ui.labNumber->setText(number);
}QString CAccountItem::GetAccountNumber() const
{return ui.labNumber->text();
}void CAccountItem::OnRemoveAccount()
{emit sigRemoveAccount(ui.labNumber->text());
}void CAccountItem::mousePressEvent(QMouseEvent *event)
{if(event->button() == Qt::LeftButton) {mouse_press = true;}
}void CAccountItem::mouseReleaseEvent(QMouseEvent *event)
{if(mouse_press) {emit sigShowAccount(ui.labNumber->text());mouse_press = false;}
}void CAccountItem::SetNickName( const QString& name )
{ui.labNickName->setText(name);
}QString CAccountItem::GetNickName() const
{return ui.labNickName->text();
}


2. 重载ComboBox, 用QListWidget做为ComboBox的model, 

QListWidget中加载AccountItem


AccountComboBox.h

#ifndef TESTCOMBOBOX_H
#define TESTCOMBOBOX_H#include <QComboBox>
#include <QListWidget>class CAccountItem;class CAccountComboBox : public QComboBox
{Q_OBJECTpublic:CAccountComboBox(QWidget *parent);~CAccountComboBox();void AddAccount(CAccountItem* pAccountItem);private slots:void OnShowAccount(QString account);void OnRemoveAccount(QString account);private:QListWidget* mpListWidget;
};#endif // TESTCOMBOBOX_H

AccountComboBox.cpp

#include "AccountComboBox.h"
#include "AccountItem.h"CAccountComboBox::CAccountComboBox(QWidget *parent): QComboBox(parent)
{setEditable(true);setFixedSize(220,30);setStyleSheet("QComboBox{border:1px solid gray;}""QComboBox QAbstractItemView::item{height:50px;}" //下拉选项高度);mpListWidget = new QListWidget();setModel(mpListWidget->model());setView(mpListWidget);
}CAccountComboBox::~CAccountComboBox()
{}void CAccountComboBox::AddAccount( CAccountItem* pAccountItem )
{connect(pAccountItem, SIGNAL(sigShowAccount(QString)), this, SLOT(OnShowAccount(QString)));connect(pAccountItem, SIGNAL(sigRemoveAccount(QString)), this, SLOT(OnRemoveAccount(QString)));QListWidgetItem* item = new QListWidgetItem(mpListWidget);mpListWidget->setItemWidget(item, pAccountItem);
}void CAccountComboBox::OnShowAccount(QString account)
{setEditText(account);hidePopup();
}void CAccountComboBox::OnRemoveAccount(QString account)
{hidePopup();//msg_box->setInfo(tr("remove account"), tr("are you sure to remove account?"), QPixmap(":/loginDialog/attention"), false);//if(msg_box->exec() == QDialog::Accepted){int list_count = mpListWidget->count();for(int i=0; i<3; i++){QListWidgetItem *item = mpListWidget->item(i);CAccountItem *account_item = (CAccountItem *)(mpListWidget->itemWidget(item));QString account_number = account_item->GetAccountNumber();if(account == account_number){mpListWidget->takeItem(i);delete item;break;}}}
}

调用方法:

testWid.cpp

testWid::testWid(QWidget *parent): QDialog(parent)
{ui.setupUi(this);account_combo_box = new CAccountComboBox(this);for(int i=0; i<3; i++){QString _nickName = QString::fromLocal8Bit("贷款国");QString _number = QString("safe_") + QString::number(i, 10) + QString("@sina.com");CAccountItem *account_item = new CAccountItem(_nickName, _number);account_combo_box->AddAccount(account_item);}
}

大概就是这样的了. 这个方法挺方便的. 赞一个.

这篇关于仿QQ登录界面的QComboBox的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/177064

相关文章

Spring Boot基于 JWT 优化 Spring Security 无状态登录实战指南

《SpringBoot基于JWT优化SpringSecurity无状态登录实战指南》本文介绍如何使用JWT优化SpringSecurity实现无状态登录,提高接口安全性,并通过实际操作步骤... 目录Spring Boot 实战:基于 JWT 优化 Spring Security 无状态登录一、先搞懂:为什

在C#中调用Windows防火墙界面的常见方式

《在C#中调用Windows防火墙界面的常见方式》在C#中调用Windows防火墙界面(基础设置或高级安全设置),可以使用进程启动(Process.Start)或Win32API来实现,所以本文给大家... 目录引言1. 直接启动防火墙界面(1) 打开基本防火墙设置(firewall.cpl)(2) 打开高

在DataGrip中操作MySQL完整流程步骤(从登录到数据查询)

《在DataGrip中操作MySQL完整流程步骤(从登录到数据查询)》DataGrip是JetBrains公司出品的一款现代化数据库管理工具,支持多种数据库系统,包括MySQL,:本文主要介绍在D... 目录前言一、登录 mysql 服务器1.1 打开 DataGrip 并添加数据源1.2 配置 MySQL

Springboot中JWT登录校验及其拦截器实现方法

《Springboot中JWT登录校验及其拦截器实现方法》:本文主要介绍Springboot中JWT登录校验及其拦截器实现方法的相关资料,包括引入Maven坐标、获取Token、JWT拦截器的实现... 目录前言一、JWT是什么?二、实现步骤1.引入Maven坐标2.获取Token3.JWT拦截器的实现4.

90%的人第一步就错了! 顺利登录wifi路由器后台的技巧

《90%的人第一步就错了!顺利登录wifi路由器后台的技巧》登录Wi-Fi路由器,其实就是进入它的后台管理页面,很多朋友不知道该怎么进入路由器后台设置,感兴趣的朋友可以花3分钟了解一下... 你是不是也遇到过这种情况:家里网速突然变慢、想改WiFi密码却不知道从哪进路由器、新装宽带后完全不知道怎么设置?别慌

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

Spring Security重写AuthenticationManager实现账号密码登录或者手机号码登录

《SpringSecurity重写AuthenticationManager实现账号密码登录或者手机号码登录》本文主要介绍了SpringSecurity重写AuthenticationManage... 目录一、创建自定义认证提供者CustomAuthenticationProvider二、创建认证业务Us

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

使用Redis快速实现共享Session登录的详细步骤

《使用Redis快速实现共享Session登录的详细步骤》在Web开发中,Session通常用于存储用户的会话信息,允许用户在多个页面之间保持登录状态,Redis是一个开源的高性能键值数据库,广泛用于... 目录前言实现原理:步骤:使用Redis实现共享Session登录1. 引入Redis依赖2. 配置R

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、