Qt实现QQ好友下拉列表

这篇博客介绍了如何使用Qt实现类似QQ面板的下拉列表功能,包括自定义数据模型来实现删除列表中图标、转移分组下图标以及添加分组等操作。通过示例代码展示了将黑名单中的图标5移动到我的好友分组的过程。虽然目前的程序相对简单,尚无法完全模拟QQ面板的所有功能,但作者表示会继续研究完善。
摘要由CSDN通过智能技术生成

    偶然发现Qt有个控件可以实现下拉列表,所以就试着实现一下类似QQ面板的下拉列表,这里主要实现几个功能:

   1.可以删除列表中图标

   2.可以像qq一样的,把某个分组下的图标转移到另外的分组

  3.添加分组

代码里写了注释了,这里就不重复了,下面直接看代码吧。

自定义的数据模型

ListModel继承了QAbstractListModel,主要是实现要显示的数据结构。用的是model/view的三层结构,这样好拆分


struct ListItemData
{
	QString  iconPath;
	QString  Name;

};

class ListModel:public QAbstractListModel
{
	Q_OBJECT
public:
	ListModel(QObject *parent = NULL);
	~ListModel();

	void init();
	void addItem(ListItemData *pItem);
	QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const ;
	int	rowCount ( const QModelIndex & parent = QModelIndex() ) const;
	void deleteItem(int index);
	ListItemData* getItem(int index );
protected:
private:
	vector<ListItemData*> m_ItemDataVec;
};
ListModel::ListModel( QObject *parent /*= NULL*/ ):QAbstractListModel(parent)
{
	init();
}

ListModel::~ListModel()
{

}

QVariant ListModel::data( const QModelIndex & index, int role /*= Qt::DisplayRole */ ) const
{
	if (index.row() > m_ItemDataVec.size())
	{
		return QVariant();
	} 
	else
	{
	   switch (role)
	   {
	   case Qt::DisplayRole:
		   {
			   return m_ItemDataVec[index.row()]->Name;
		   }
	   	break;
	   case Qt::DecorationRole:
		   {
			   return QIcon(m_ItemDataVec[index.row()]->iconPath);
		   }
		   break;
	 case Qt::SizeHintRole:
		   {
			   return QSize(10,50);

		   }
	   }
	}
	return QVariant();
}



int ListModel::rowCount( const QModelIndex & parent /*= QModelIndex() */ ) const
{
	return m_ItemDataVec.size();
}

void ListModel::init()
{
	for (int i = 1; i < 26; ++i)
	{ 
		ListItemData *pItem = new ListItemData;
		pItem->Name = QString::number(i);
		pItem->iconPath = QString(":/QQPanel/Resources/%1.jpg").arg(i);
		QFile Iconfile(pItem->iconPath);
		if (Iconfile.exists())
		{
			m_ItemDataVec.push_back(pItem);
		}
		
	}
}

void ListM
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个不错的问题,让我来简单介绍一下如何使用 Qt 实现 QQ 好友下拉列表。 首先,我们需要使用 QListView 控件来实现下拉列表,然后我们需要定义一个自定义的 Model 来提供数据。这个 Model 可以继承自 QAbstractListModel 类,然后重写一些虚函数来实现自定义 Model 的功能。 下面是一个简单的自定义 Model 的示例代码: ```cpp class FriendListModel : public QAbstractListModel { public: FriendListModel(QObject *parent = nullptr); void setFriendList(const QList<QString>& list); int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; private: QList<QString> m_friendList; }; FriendListModel::FriendListModel(QObject *parent) : QAbstractListModel(parent) { } void FriendListModel::setFriendList(const QList<QString>& list) { m_friendList = list; emit dataChanged(index(0), index(m_friendList.size()-1)); } int FriendListModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return m_friendList.size(); } QVariant FriendListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= m_friendList.size() || index.row() < 0) return QVariant(); if (role == Qt::DisplayRole) return m_friendList.at(index.row()); else return QVariant(); } ``` 在上面的代码中,我们定义了一个 FriendListModel 类来提供好友列表数据。其中,我们重写了 rowCount() 和 data() 函数来实现自定义 Model 的功能。rowCount() 函数返回列表的行数,data() 函数返回每个单元格的数据。 接着,我们需要在界面中使用 QListView 控件,并设置上述自定义 Model 为其数据源。示例代码如下: ```cpp QListView* friendListView = new QListView(this); FriendListModel* friendListModel = new FriendListModel(this); friendListView->setModel(friendListModel); friendListModel->setFriendList({"Friend1", "Friend2", "Friend3"}); ``` 上述代码中,我们使用 QListView 控件来显示好友列表,然后将 FriendListModel 设置为其数据源,并通过 setFriendList() 函数设置好友列表数据。 最后,你还可以根据需要自定义 QListView 的样式,比如设置下拉列表的最大高度、单元格的字体大小、颜色等等。 希望这些信息能够对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值