15. 菜单功能按钮实现

一、设置播放声音大小

1.类设计

#ifndef VOICESETWIDGET_H
#define VOICESETWIDGET_H
​
#include <QWidget>
​
namespace Ui {
class VoiceSetWidget;
}
​
class VoiceSetWidget : public QWidget
{
    Q_OBJECT
​
public:
    explicit VoiceSetWidget(QWidget *parent = nullptr);
    ~VoiceSetWidget();
​
signals:
    void voiceChanged(int value);
​
private slots:
    void on_voiceSlider_sliderMoved(int position);
​
private:
    Ui::VoiceSetWidget *ui;
};
​
#endif // VOICESETWIDGET_H

2.类实现

#include "voicesetwidget.h"
#include "ui_voicesetwidget.h"
​
VoiceSetWidget::VoiceSetWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VoiceSetWidget)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground,true);
    setWindowFlag(Qt::Window);
}
​
VoiceSetWidget::~VoiceSetWidget()
{
    delete ui;
}
​
void VoiceSetWidget::on_voiceSlider_sliderMoved(int position)
{
    ui->voiceLabel->setText(QString::number(position));
    emit voiceChanged(position);
}

3.类使用

class CloudMusicWindow : public QMainWindow
{
    Q_OBJECT
​
public:
    CloudMusicWindow(QWidget *parent = nullptr);
    ~CloudMusicWindow();
    void on_voiceSetAction_triggered();
    
private:
    VoiceSetWidget *voiceSet;
};
​
CloudMusicWindow::CloudMusicWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::CloudMusicWindow)
{
    ui->setupUi(this);
    
    voiceSet = new VoiceSetWidget(this);
    musicPlayer = new MusicPlayer(this);
    connect(voiceSet,&VoiceSetWidget::voiceChanged,
            musicPlayer,&MusicPlayer::setVolume);
}
​
void CloudMusicWindow::on_voiceSetAction_triggered()
{
    voiceSet->show();
}

二、设置歌曲下载路径

1.类设计

#ifndef MUSICDOWNLOADSETWIDGET_H
#define MUSICDOWNLOADSETWIDGET_H
​
#include <QWidget>
​
namespace Ui {
class MusicDownloadSetWidget;
}
​
class MusicDownloadSetWidget : public QWidget
{
    Q_OBJECT
​
public:
    explicit MusicDownloadSetWidget(QWidget *parent = nullptr);
    ~MusicDownloadSetWidget();
​
signals:
    void downloadPathChanged(const QString &downloadPath);
​
private slots:
    void on_pushButton_clicked();
​
private:
    Ui::MusicDownloadSetWidget *ui;
};
​
#endif // MUSICDOWNLOADSETWIDGET_H

2.类实现

#include "musicdownloadsetwidget.h"
#include "ui_musicdownloadsetwidget.h"
#include <QFileDialog>
#include <QDebug>
​
MusicDownloadSetWidget::MusicDownloadSetWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicDownloadSetWidget)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground,true);
    setWindowFlag(Qt::Window);
}
​
MusicDownloadSetWidget::~MusicDownloadSetWidget()
{
    delete ui;
}
​
void MusicDownloadSetWidget::on_pushButton_clicked()
{
    QString downloadPath = QFileDialog::getExistingDirectory(this, 
                                          tr("选择下载的路径"),
                                          ".",
                                          QFileDialog::ShowDirsOnly
                                          | QFileDialog::DontResolveSymlinks);
​
    if(downloadPath.isEmpty()){
        return;
    }
​
    ui->lineEdit->setText(downloadPath);
    emit downloadPathChanged(downloadPath);
}
​

3.类使用

class CloudMusicWindow : public QMainWindow
{
    Q_OBJECT
​
public:
    CloudMusicWindow(QWidget *parent = nullptr);
    ~CloudMusicWindow();
    void on_musicDownloadSetAction_triggered();
    
private:
    MusicDownloadSetWidget *musicDownloadSet;
};
​
CloudMusicWindow::CloudMusicWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::CloudMusicWindow)
{
    ui->setupUi(this);
    
    musicDownloadSet = new MusicDownloadSetWidget(this);
}
​
void CloudMusicWindow::on_musicDownloadSetAction_triggered()
{
    musicDownloadSet->show();
}

三、设置服务器端地址

1.类设计

#ifndef SEVERADDRESSSETWIDGET_H
#define SEVERADDRESSSETWIDGET_H
​
#include <QWidget>
​
namespace Ui {
class SeverAddressSetWidget;
}
​
class SeverAddressSetWidget : public QWidget
{
    Q_OBJECT
​
public:
    explicit SeverAddressSetWidget(QWidget *parent = nullptr);
    ~SeverAddressSetWidget();
​
signals:
    void serverAddressChanged(const QString &ip,const QString &port);
​
private slots:
    void on_clearButton_clicked();
​
    void on_saveButton_clicked();
​
private:
    Ui::SeverAddressSetWidget *ui;
};
​
#endif // SEVERADDRESSSETWIDGET_H
​

2.类实现

#include "severaddresssetwidget.h"
#include "ui_severaddresssetwidget.h"
#include <QHostAddress>
#include <QDebug>
#include <QMessageBox>
​
SeverAddressSetWidget::SeverAddressSetWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SeverAddressSetWidget)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground,true);
    setWindowFlag(Qt::Window);
}
​
SeverAddressSetWidget::~SeverAddressSetWidget()
{
    delete ui;
}
​
void SeverAddressSetWidget::on_clearButton_clicked()
{
    ui->ipLineEdit->clear();
    ui->portLineEdit->clear();
}
​
void SeverAddressSetWidget::on_saveButton_clicked()
{
    QString ip   = ui->ipLineEdit->text();
    QString port = ui->portLineEdit->text();
​
    if(ip.isEmpty() || port.isEmpty()){
        return;
    }
​
    QStringList ipDataList = ip.split('.');
    if(ipDataList.size() != 4){
        QMessageBox::warning(this,"IP地址错误","Invalid ip address:" + ip);
        return;
    }
​
    if(QHostAddress(ip).isNull()){
        QMessageBox::warning(this,"IP地址错误","Invalid ip : " + ip);
        return;
    }
​
​
    bool ok;
    port.toUShort(&ok);
    if(!ok){
        QMessageBox::warning(this,"端口错误","Invalid port : " + port);
        return;
    }
​
    emit serverAddressChanged(ip,port);
​
    return;
}
​

3.类使用

class CloudMusicWindow : public QMainWindow
{
    Q_OBJECT

public:
    CloudMusicWindow(QWidget *parent = nullptr);
    ~CloudMusicWindow();
   
private:
    SeverAddressSetWidget *serverAddressSet;
};
#endif // CLOUDMUSICWINDOW_H

CloudMusicWindow::CloudMusicWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::CloudMusicWindow)
{
    ui->setupUi(this);
    serverAddressSet = new SeverAddressSetWidget(this);
    connect(serverAddressSet,&SeverAddressSetWidget::serverAddressChanged,
         MusicHttpUrl::getMusicHttpUrlObject(),&MusicHttpUrl::setServerAddress);
}

三、设置是否下载歌曲标志

connect(ui->downloadNetworkMusicFlag,&QAction::toggled,this,[](bool checked){
       qDebug() << "checked:" << checked;
});

四、软件配置信息存储

将菜单功能按钮的所有操作结果以JSON数据格式保存在一个配置文件中,软件启动的时候自动读取配置文件,根据配置文件内容做相关的设置。

1.类的设计

#ifndef MUSICCONFIG_H
#define MUSICCONFIG_H

#include <QObject>

class MusicConfig : public QObject
{
    Q_OBJECT
public:
    explicit MusicConfig(const QString &filename = "config.json",
                         QObject *parent = nullptr);
    void save(void);
    void restore(void);

    QString getFilename() const;
    void setFilename(const QString &value);

    QString getDownloadPath() const;
    void setDownloadPath(const QString &value);

    qint32 getVoiceSize() const;
    void setVoiceSize(const qint32 &value);

    bool getDownloadMusicFlag() const;
    void setDownloadMusicFlag(bool value);

    QString getServerIP() const;
    void setServerIP(const QString &value);

    QString getServerPort() const;
    void setServerPort(const QString &value);

    void setServerAddress(const QString &ip,const QString &port);

signals:

private:
    QString filename;
    QString downloadPath;
    qint32 voiceSize;
    bool    downloadMusicFlag;
    QString serverIP;
    QString serverPort;
};

#endif // MUSICCONFIG_H

2.类的实现

#include "musicconfig.h"
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>

MusicConfig::MusicConfig(const QString &filename, QObject *parent) :
                                                                QObject(parent)
{
    this->filename = filename;

    if(!QFile::exists(filename)){
        this->downloadPath = QDir::currentPath();
        this->voiceSize    = 50;
        this->serverIP     = "127.0.0.1";
        this->serverPort   = "8080";
        this->downloadMusicFlag = false;
        save();
    }else{
        restore();
    }
}

void MusicConfig::save()
{
    QJsonObject configObject;

    configObject["downloadPath"] = downloadPath;
    configObject["voiceSize"]    = voiceSize;
    configObject["serverIP"]     = serverIP;
    configObject["serverPort"]   = serverPort;
    configObject["downloadMusicFlag"] = downloadMusicFlag;

    QJsonDocument configDoc(configObject);
    QByteArray configData = configDoc.toJson();

    QFile file(filename);
    bool ok = file.open(QFile::WriteOnly | QFile::Truncate);
    if(!ok){
        qDebug() << "Fail to open :" << filename 
                 << ",err:" << file.errorString();
        return;
    }

    file.write(configData);
    file.close();

    return;
}

void MusicConfig::restore()
{
    QFile file(filename);
    bool ok = file.open(QFile::ReadOnly);
    if(!ok){
        qDebug() << "Fail to open :" << filename 
                 << ",err:" << file.errorString();
        return;
    }

    QByteArray data = file.readAll();
    file.close();

    QJsonDocument configDoc = QJsonDocument::fromJson(data);
    if(configDoc.isEmpty()){
        qDebug() << "Fail to read json data";
        return;
    }

    QJsonObject configObject = configDoc.object();
    downloadPath = configObject["downloadPath"].toString() ;
    voiceSize = configObject["voiceSize"].toInt();
    serverIP  = configObject["serverIP"].toString();
    serverPort = configObject["serverPort"].toString();
    downloadMusicFlag = configObject["downloadMusicFlag"].toBool();

    return;
}

QString MusicConfig::getFilename() const
{
    return filename;
}

void MusicConfig::setFilename(const QString &value)
{
    filename = value;
    save();
}

QString MusicConfig::getDownloadPath() const
{
    return downloadPath;
}

void MusicConfig::setDownloadPath(const QString &value)
{
    downloadPath = value;
    save();
}

qint32 MusicConfig::getVoiceSize() const
{
    return voiceSize;
}

void MusicConfig::setVoiceSize(const qint32 &value)
{
    voiceSize = value;
    save();
}

bool MusicConfig::getDownloadMusicFlag() const
{
    return downloadMusicFlag;
}

void MusicConfig::setDownloadMusicFlag(bool value)
{
    downloadMusicFlag = value;
    save();
}

QString MusicConfig::getServerIP() const
{
    return serverIP;
}

void MusicConfig::setServerIP(const QString &value)
{
    serverIP = value;
    save();
}

QString MusicConfig::getServerPort() const
{
    return serverPort;
}

void MusicConfig::setServerPort(const QString &value)
{
    serverPort = value;
    save();
}

void MusicConfig::setServerAddress(const QString &ip, const QString &port)
{
    this->serverIP   = ip;
    this->serverPort = port;

    save();
}

3.类的应用

class CloudMusicWindow : public QMainWindow
{
    Q_OBJECT
​
public:
    CloudMusicWindow(QWidget *parent = nullptr);
    ~CloudMusicWindow();
    void initMusicConfig(MusicConfig *musicConfig);
​
private:
    MusicConfig *musicConfig;
};
​
CloudMusicWindow::CloudMusicWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::CloudMusicWindow)
{
    musicConfig = new MusicConfig("music.config",this);
    initMusicConfig(musicConfig);
}
​
void CloudMusicWindow::initMusicConfig(MusicConfig *musicConfig)
{
    voiceSet->setVoiceSize(musicConfig->getVoiceSize());
    musicDownloadSet->setDownloadPath(musicConfig->getDownloadPath());
    serverAddressSet->setServerAddress(musicConfig->getServerIP(),
                                       musicConfig->getServerPort());
    ui->downloadNetworkMusicFlag->setChecked(
                        musicConfig->getDownloadMusicFlag());
​
    connect(voiceSet,&VoiceSetWidget::voiceChanged,
            this,[musicConfig](int size){
        musicConfig->setVoiceSize(size);
    });
​
    connect(musicDownloadSet,&MusicDownloadSetWidget::downloadPathChanged,
            this,[musicConfig](const QString &downloadPath){
            musicConfig->setDownloadPath(downloadPath);
    });
​
    connect(serverAddressSet,&SeverAddressSetWidget::serverAddressChanged,
            this,[musicConfig](const QString &ip,const QString &port){
        musicConfig->setServerAddress(ip,port);
    });
​
    connect(ui->downloadNetworkMusicFlag,&QAction::toggled,
            this,[musicConfig](bool checked){
       musicConfig->setDownloadMusicFlag(checked);
    });
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值