本文主要是介绍仿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();
}
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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!