源码下载地址:
https://download.csdn.net/download/qq78442761/10534055
程序运行截图:
程序版权
程序结构图如下:
主要就几点:
1.QSS
2.自定义控件
3.获取屏幕分辨率
代码如下:
mylabel.h
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QObject>
#include <QLabel>
#define CloseLabel 0
#define MinLabel 1
class MyLabel : public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *parent=0);
void setStatus(const int status);
~MyLabel();
protected:
void mouseReleaseEvent(QMouseEvent *event);
signals:
void prepareToClose();
void prepareToMin();
private:
int m_status;
};
#endif // MYLABEL_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPoint>
class QTimer;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
protected slots:
void showDynamicTime();
void windowsClose();
void windowsShowMin();
private:
Ui::Widget *ui;
QPoint z;
QTimer *m_pTimer;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
mylabel.cpp
#include "mylabel.h"
#include <QDebug>
#include <QMouseEvent>
MyLabel::MyLabel(QWidget *parent):
QLabel(parent)
{
}
void MyLabel::setStatus(const int status)
{
m_status=status;
}
MyLabel::~MyLabel()
{
}
void MyLabel::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton){
if(m_status==CloseLabel){
emit prepareToClose();
}
else if(m_status==MinLabel){
emit prepareToMin();
}
}
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
#include <QFile>
#include <QEvent>
#include <QDateTime>
#include <QTimer>
#include <QRect>
#include <QDesktopWidget>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->timeLabel->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
this->setWindowFlags(Qt::FramelessWindowHint);
ui->titleLabel->setText("博客:CSDN IT1995");
QFile myFile;
myFile.setFileName(":/img/myQSS.txt");
myFile.open(QFile::ReadOnly);
QString styleSheet=QString(myFile.readAll());
qApp->setStyleSheet(styleSheet);
myFile.close();
QRect sceenRect=QApplication::desktop()->screenGeometry();
setFixedSize(sceenRect.width()/6,sceenRect.height()/2);
m_pTimer=new QTimer(this);
connect(m_pTimer,SIGNAL(timeout()),this,SLOT(showDynamicTime()));
m_pTimer->start(1000);
ui->closeLabel->setStatus(CloseLabel);
ui->minLabel->setStatus(MinLabel);
connect(ui->closeLabel,SIGNAL(prepareToClose()),this,SLOT(windowsClose()));
connect(ui->minLabel,SIGNAL(prepareToMin()),this,SLOT(windowsShowMin()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
QWidget::mouseMoveEvent(event);
QPoint y=event->globalPos();
QPoint x=y-z;
this->move(x);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
QPoint y=event->globalPos();
QPoint x=this->geometry().topLeft();
z=y-x;
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
QWidget::mouseReleaseEvent(event);
z=QPoint();
}
void Widget::showDynamicTime()
{
ui->timeLabel->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}
void Widget::windowsClose()
{
this->close();
}
void Widget::windowsShowMin()
{
this->showMinimized();
}
QSS代码如下:
MyLabel#minLabel{
image: url(:/img/min.png);
}
MyLabel#minLabel:hover{
image: url(:/img/min_hover.png);
}
MyLabel#closeLabel{
image: url(:/img/close.png);
}
MyLabel#closeLabel:hover{
image: url(:/img/close_hover.png);
}
QLabel#iconLabel{
image: url(:/img/icon.png);
}
QLabel#iconLabel:hover{
image: url(:/img/icon_hover.png);
}
QLabel#titleLabel{
color: rgb(200, 39, 11);
font: 14pt "华文琥珀";
}
QLabel#titleLabel:hover{
color: rgb(0, 85, 255);
font: 14pt "华文琥珀";
}
QLabel#timeLabel{
color: rgb(200, 39, 11);
}
QLabel#timeLabel:hover{
color: rgb(0, 85, 255);
}
QWidget#Widget{
background-color: rgb(0, 0, 0);
}
QWidget#mainWidget{
background-color: rgb(255, 255, 255);
}