最近工作需要,用到Qt,现在先写个小程序和大家分享下几个我觉比较重要的知识点,希望大家指正,以后再陆续更新
该例子主要功能有:
1,隐藏窗口标题栏,然后自己设计一个
2,新建close按钮
3,按钮菜单制作
void MyTest::Init()
{
//隐藏窗口的标题栏
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
//添加窗口Close按钮
IconHelper::Instance()->SetIcon(ui->Btn_close, QChar(0xf00d), 10);
connect(ui->Btn_close, SIGNAL(clicked()), this, SLOT(close()));
//添加菜单按钮
IconHelper::Instance()->SetIcon(ui->Btn_Menu, QChar(0xf0c9), 10);
}
void MyTest::on_Btn_Menu_clicked()
{
QMenu *menu = new QMenu();
QAction *menu_new = new QAction(menu);
QAction *menu_open = new QAction(menu);
QAction *menu_close = new QAction(menu);
menu_new->setText(tr("登陆"));
menu_open->setText(tr("注册"));
menu_close->setText(tr("注销"));
menu->addAction(menu_new);
menu->addAction(menu_open);
menu->addAction(menu_close);
ui->Btn_Menu->setMenu(menu);
}
加载qss文件
QString qss;
QFile qssFile(":/ui.qss");
qssFile.open(QFile::ReadOnly);
if(qssFile.isOpen())
{
qss = QLatin1String(qssFile.readAll());
qApp->setStyleSheet(qss);
qssFile.close();
}
iconhelper.cpp 和 iconhelper.h来源于网络
iconhelper.cpp
#include "iconhelper.h"
IconHelper* IconHelper::_instance = 0;
IconHelper::IconHelper(QObject*):
QObject(qApp)
{
QString runPath = QCoreApplication::applicationDirPath() + "./image/fontawesome-webfont.ttf";
int fontId = QFontDatabase::addApplicationFont(runPath);
QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);
iconFont = QFont(fontName);
}
void IconHelper::SetIcon(QLabel* lab, QChar c, int size)
{
iconFont.setPointSize(size);
lab->setFont(iconFont);
lab->setText(c);
}
void IconHelper::SetIcon(QPushButton* btn, QChar c, int size)
{
iconFont.setPointSize(size);
btn->setFont(iconFont);
btn->setText(c);
}
iconhelper.h
#ifndef ICONHELPER_H
#define ICONHELPER_H
#include
#include
#include
#include
#include
#include
#include
class IconHelper : public QObject
{
private:
explicit IconHelper(QObject *parent = 0);
QFont iconFont;
static IconHelper* _instance;
public:
static IconHelper* Instance()
{
static QMutex mutex;
if (!_instance) {
QMutexLocker locker(&mutex);
if (!_instance) {
_instance = new IconHelper;
}
}
return _instance;
}
void SetIcon(QLabel* lab, QChar c, int size = 10);
void SetIcon(QPushButton* btn, QChar c, int size = 10);
};
#endif // ICONHELPER_H
qss
.QPushButton#Btn_close, .QPushButton#Btn_Menu{
border-radius:0px;
color: #F0F0F0;
background-color:rgba(0,0,0,0);
border-style:none;
}
.QPushButton#Btn_close:hover{
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(238, 0, 0, 128), stop:1 rgba(238, 44, 44, 255));
}
.QPushButton#Btn_Menu:hover{
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(25, 134, 199, 0), stop:1 #636363);
}
.QPushButton#Btn_Menu:menu-indicator{
image: none;
}
.QWidget#widget{
background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #696969, stop:1 #808080);
}
.QMenu{
background-color:white;
padding: 1px;
}
.QMenu::item{
background-color:transparent;
}
.QMenu::item:selected{
background-color:rgb(234, 243, 253);
color: black;
}
.QLineEdit#lineEdit, .QLineEdit#lineEdit_2{
border: 1px solid #B8B8B8;
border-radius: 5px;
padding: 2px;
background: none;
selection-background-color: #454648;
}
.QLineEdit#lineEdit, .QLineEdit#lineEdit_2{
lineedit-password-character: 9679;
}
运行效果: