Qt工作笔记-仿QQ登录界面(换肤,切换语言)

本程序含有资源文件,和QSS,在此博文不在展示。

源码下载地址:

https://download.csdn.net/download/qq78442761/10389816



程序运行截图如下:



本程序关键代码如下:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    bool eventFilter(QObject *watched, QEvent *event);
    ~Widget();

public slots:
    void btnClicked();


private:
    void paintEvent(QPaintEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);


private:
    Ui::Widget *ui;
    QPixmap *m_bgImage;
    QFile *m_file;

    bool m_dragging;
    QPoint m_startPosition;
    QPoint m_framePosition;

    bool currentSkin;

    QTranslator *m_translator;
    bool zh_CN;
};

#endif // WIDGET_H


widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include 
#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    zh_CN(false),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_dragging=false;
    setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    m_bgImage=new QPixmap();
    m_bgImage->load(":/image/qq.bmp");

    currentSkin=false;

    ui->skinLabel->installEventFilter(this);

    m_file=new QFile(":/qq.qss",this);
    m_file->open(QFile::ReadOnly);
    QString styleSheet=QString(m_file->readAll());
    qApp->setStyleSheet(styleSheet);
    m_file->close();
    delete m_file;

    connect(ui->colseToolButton,SIGNAL(clicked(bool)),this,SLOT(close()));
    connect(ui->toolButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
}

void Widget::btnClicked(){
    m_translator=new QTranslator;
    if(!zh_CN){
        if(!m_translator->load(":/myI18N_zh_CN.qm")){
            qDebug()<<"加载失败";
            return;
        }
        qApp->installTranslator(m_translator);
        ui->retranslateUi(this);
    }
    else{
        m_translator->load(":/myI18N_US.qm");
        qApp->installTranslator(m_translator);
        ui->retranslateUi(this);
    }
    zh_CN=!zh_CN;
    delete m_translator;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event){
    Q_UNUSED(event)
    QPainter painter(this);
    QRect frameRect = rect();
    painter.drawPixmap(frameRect, *m_bgImage);
    this->update();
}

void Widget::mousePressEvent(QMouseEvent *event){
    if(event->button()==Qt::LeftButton){
        m_dragging=true;
        m_startPosition=event->globalPos();
        m_framePosition=frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
}

void Widget::mouseMoveEvent(QMouseEvent *event){
    if(event->buttons()&Qt::LeftButton){
        if(m_dragging){
            QPoint delta=event->globalPos()-m_startPosition;
            move(m_framePosition+delta);
        }
    }
    QWidget::mouseMoveEvent(event);
}

void Widget::mouseReleaseEvent(QMouseEvent *event){
    m_dragging=false;
    QWidget::mouseReleaseEvent(event);
}


bool Widget::eventFilter(QObject *watched, QEvent *event){
    if(watched==ui->skinLabel){
        QMouseEvent *mouseEvent=static_cast(event);
        if(mouseEvent->button()==Qt::RightButton){
            currentSkin=!currentSkin;
            if(!currentSkin){
                delete m_bgImage;
                m_bgImage=new QPixmap();
                m_bgImage->load(":/image/qq.bmp");
                m_file=new QFile(":/qq.qss",this);
                m_file->open(QFile::ReadOnly);
                QString styleSheet=QString(m_file->readAll());
                qApp->setStyleSheet(styleSheet);
                m_file->close();
                delete m_file;

            }
            else{
                delete m_bgImage;
                m_bgImage=new QPixmap();
                m_bgImage->load(":/image/qq2.bmp");
                m_file=new QFile(":/qq2.qss",this);
                m_file->open(QFile::ReadOnly);
                QString styleSheet=QString(m_file->readAll());
                qApp->setStyleSheet(styleSheet);
                m_file->close();
                delete m_file;
            }
        }
    }
    return false;
}


main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Widget w;
    w.show();

    return a.exec();
}

QSS以及其他资源在文章开头有链接下载

你可能感兴趣的:(C/C++,Qt,工作笔记,Qt,QQ)