网盘——分享文件——界面设计

本文主要讲解网盘中文件操作的分享文件部分,主要包含两方面的设计:分享文件界面设计和逻辑设计。

1、界面设计

1.1、添加一个类

1.2、引入头文件

#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QButtonGroup>
#include <QScrollArea>

1.3、添加对象

private:
    //全选
    QPushButton *m_pSelectAllPB;
    //取消
    QPushButton *m_pCancelSelectPB;
    //确定
    QPushButton *m_pOKPB;
    //取消分享
    QPushButton *m_pCancelPB;
    //中间显示好友的区域
    QScrollArea *m_pSA;
    QWidget *m_pFriendW;
    QButtonGroup *m_pButtonGroup;

1.4、添加控件,定义布局

ShareFile::ShareFile(QWidget *parent) : QWidget(parent)
{
    //全选
    m_pSelectAllPB = new QPushButton("全选");
    //取消
    m_pCancelSelectPB = new QPushButton("取消选择");
    //确定
    m_pOKPB = new QPushButton("确定");
    //取消分享
    m_pCancelPB = new QPushButton("取消");
    //中间显示好友的区域
    m_pSA = new QScrollArea;
    m_pFriendW = new QWidget;
    m_pButtonGroup = new QButtonGroup(m_pFriendW);
    
    QHBoxLayout *pTopHBL = new QHBoxLayout;
    pTopHBL->addWidget(m_pSelectAllPB);
    pTopHBL->addWidget(m_pCancelSelectPB);
    pTopHBL->addStretch();//弹簧
    
    QHBoxLayout *pDownHBL = new QHBoxLayout;
    pDownHBL->addWidget(m_pOKPB);
    pDownHBL->addWidget(m_pCancelPB);
    
    QVBoxLayout *pMainVBL = new QVBoxLayout;
    pMainVBL->addLayout(pTopHBL);
    pMainVBL->addWidget(m_pSA);
    pMainVBL->addLayout(pDownHBL);
    
    setLayout(pMainVBL);
}

1.5、展示效果

1.6、将好友放进来

void ShareFile::test()
{
    QVBoxLayout *p = new QVBoxLayout(m_pFriendW);
    QCheckBox *pCB = NULL;
    for(int i=0;i<15;i++)
    {
        pCB = new QCheckBox("jack");
        p->addWidget(pCB);
        m_pButtonGroup->addButton(pCB);
    }
    m_pSA->setWidget(m_pFriendW);
}

#include "sharefile.h"

ShareFile::ShareFile(QWidget *parent) : QWidget(parent)
{
    //全选
    m_pSelectAllPB = new QPushButton("全选");
    //取消
    m_pCancelSelectPB = new QPushButton("取消选择");
    //确定
    m_pOKPB = new QPushButton("确定");
    //取消分享
    m_pCancelPB = new QPushButton("取消");
    //中间显示好友的区域
    m_pSA = new QScrollArea;
    m_pFriendW = new QWidget;
    m_FriendWVBL = new QVBoxLayout(m_pFriendW);
    m_pButtonGroup = new QButtonGroup(m_pFriendW);
    m_pButtonGroup->setExclusive(false);

    QHBoxLayout *pTopHBL = new QHBoxLayout;
    pTopHBL->addWidget(m_pSelectAllPB);
    pTopHBL->addWidget(m_pCancelSelectPB);
    pTopHBL->addStretch();//弹簧

    QHBoxLayout *pDownHBL = new QHBoxLayout;
    pDownHBL->addWidget(m_pOKPB);
    pDownHBL->addWidget(m_pCancelPB);

    QVBoxLayout *pMainVBL = new QVBoxLayout;
    pMainVBL->addLayout(pTopHBL);
    pMainVBL->addWidget(m_pSA);
    pMainVBL->addLayout(pDownHBL);

    setLayout(pMainVBL);
}

ShareFile &ShareFile::getinstance()
{
    static ShareFile instance;
    return instance;
}

void ShareFile::test()
{
    QVBoxLayout *p = new QVBoxLayout(m_pFriendW);
    QCheckBox *pCB = NULL;
    for(int i=0;i<15;i++)
    {
        pCB = new QCheckBox("jack");
        p->addWidget(pCB);
        m_pButtonGroup->addButton(pCB);
    }
    m_pSA->setWidget(m_pFriendW);
}

void ShareFile::updateFriend(QListWidget *pFriendList)
{
    if(NULL == pFriendList)
    {
        return;
    }
    QAbstractButton *tmp = NULL;
    //刷新掉以前的
    QList<QAbstractButton*>preFriendList = m_pButtonGroup->buttons();
    for(int i=0;i<preFriendList.size();i++)
    {
        tmp = preFriendList[i];
        m_FriendWVBL->removeWidget(tmp);
        m_pButtonGroup->removeButton(tmp);
        preFriendList.removeOne(tmp);
        delete tmp;
        tmp = NULL;
    }
    //将新的放进来
    QAbstractButton *pCB = NULL;
    for(int i=0;i<pFriendList->count();i++)
    {
        pCB = new QCheckBox(pFriendList->item(i)->text());
        m_FriendWVBL->addWidget(pCB);
        m_pButtonGroup->addButton(pCB);
    }
    m_pSA->setWidget(m_pFriendW);
}

现在简单的界面设计就做好了

1.7、将在线好友添加到界面上显示

    //分享文件
    void shareFile();

1.8、关联槽函数

    //关联分享文件槽函数
    connect(m_pShareFilePB, SIGNAL(clicked(bool)),
            this, SLOT(shareFile()));

1.9、添加定义

void Book::shareFile()
{
    //获得好友列表
    Friend *pFriend = OpeWidget::getInstance().getFriend();
    QListWidget *pFriendList = pFriend->getFriendlist();
    //将内容依次遍历出来
    for(int i=0;i<pFriendList->count();i++)
    {
        qDebug()<<pFriendList->item(i)->text();
    }
}

测试一下

获得在线好友列表是没有问题的

1.10、将sharefile写成单例模式

    static ShareFile &getinstance();
ShareFile &ShareFile::getinstance()
{
    static ShareFile instance;
    return instance;
}

1.11、更新好友函数

    //更新friend
    void updateFriend(QListWidget *pFriendList);
void ShareFile::updateFriend(QListWidget *pFriendList)
{
    if(NULL == pFriendList)
    {
        return;
    }
    QAbstractButton *tmp = NULL;
    //刷新掉以前的
    QList<QAbstractButton*>preFriendList = m_pButtonGroup->buttons();
    for(int i=0;i<preFriendList.size();i++)
    {
        tmp = preFriendList[i];
        m_FriendWVBL->removeWidget(tmp);
        m_pButtonGroup->removeButton(tmp);
        preFriendList.removeOne(tmp);
        delete tmp;
        tmp = NULL;
    }
    //将新的放进来
    QAbstractButton *pCB = NULL;
    for(int i=0;i<pFriendList->count();i++)
    {
        pCB = new QCheckBox(pFriendList->item(i)->text());
        m_FriendWVBL->addWidget(pCB);
        m_pButtonGroup->addButton(pCB);
    }
    m_pSA->setWidget(m_pFriendW);
}

1.12、共享文件函数

void Book::shareFile()
{
    //获得好友列表
    Friend *pFriend = OpeWidget::getInstance().getFriend();
    QListWidget *pFriendList = pFriend->getFriendlist();
    ShareFile::getinstance().updateFriend(pFriendList);
}

测试一下

 

上一篇:日期类的实现,const成员


下一篇:Docker基本命令