仿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

相关文章

Ubuntu 24.04启用root图形登录的操作流程

《Ubuntu24.04启用root图形登录的操作流程》Ubuntu默认禁用root账户的图形与SSH登录,这是为了安全,但在某些场景你可能需要直接用root登录GNOME桌面,本文以Ubuntu2... 目录一、前言二、准备工作三、设置 root 密码四、启用图形界面 root 登录1. 修改 GDM 配

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

Java中的登录技术保姆级详细教程

《Java中的登录技术保姆级详细教程》:本文主要介绍Java中登录技术保姆级详细教程的相关资料,在Java中我们可以使用各种技术和框架来实现这些功能,文中通过代码介绍的非常详细,需要的朋友可以参考... 目录1.登录思路2.登录标记1.会话技术2.会话跟踪1.Cookie技术2.Session技术3.令牌技

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

springboot security验证码的登录实例

《springbootsecurity验证码的登录实例》:本文主要介绍springbootsecurity验证码的登录实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录前言代码示例引入依赖定义验证码生成器定义获取验证码及认证接口测试获取验证码登录总结前言在spring

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现