在這裡首先感謝劉大師的作品:Qt編寫通用主界面導航(開源)
貼上示範:

本部落客要是研究了劉大師的作品然後自己總結,做點筆記。。不喜勿噴~~~
廢話不多說,先貼出代碼解析一下:
AppInit::Instance()->start();
單例模式 Instance()
首先說說這個這個Instance(),本人由于是C++新手,對此有好多C++知識不懂。是以在這裡記錄一下:
這個Instance() 介紹說用于單例模式 : 用來保證系統中隻有一個執行個體。
.h 檔案中 class類的定義:
static AppInit *Instance();
private:
static AppInit *self;
.c檔案中
AppInit *AppInit::self = ;//靜态成員變量需要在類體外面進行初始化。
AppInit *AppInit::Instance()
{
if (!self) {
QMutex mutex;//保護一個對象,同一時間隻能由一個線程進行通路。
QMutexLocker locker(&mutex);//加鎖
if (!self) {
self = new AppInit;//建立一個AppInit對象
}
}
return self;
}
使用時,采用:
AppInit::Instance()->satrt();
通過這種方式進行類執行個體的調用,保證單例模式的進行。
(2). 自定義窗體的移動:
在main 函數中,首先調用這句話,
AppInit::Instance()->start();
執行單例模式,并且加載事件過濾器。
然後在建立的窗體Widget構造函數中,調用以下函數,這裡設定窗體屬性(property)是為了對應qApp中加載的事件過濾器。使其能夠實作窗體的移動。
this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);//去除标題欄
this->setProperty("canMove", true);//将對象的名稱屬性的設定為canMove。 可以移動
在AppInit .c檔案中:
//加載事件過濾器
void AppInit::start()
{
qApp->installEventFilter(this);
}
//重寫事件過濾器
bool AppInit::eventFilter(QObject *obj, QEvent *evt)//參數:對象,事件
{
QWidget *w = (QWidget *)obj;//強制轉換為QWidget
if (!w->property("canMove").toBool())//得到QWidget的屬性是canMove(可以移動)。
{
return QObject::eventFilter(obj, evt);
}
static QPoint mousePoint;//靜态變量 --> 滑鼠坐标
static bool mousePressed = false;
QMouseEvent *event = static_cast<QMouseEvent *>(evt);
if (event->type() == QEvent::MouseButtonPress)//事件類型 滑鼠按鈕按下
{
if (event->button() == Qt::LeftButton) //左鍵
{
mousePressed = true;
mousePoint = event->globalPos() - w->pos();
return true;
}
}
else if (event->type() == QEvent::MouseButtonRelease) //事件類型 滑鼠按鈕釋放
{
mousePressed = false;
return true;
}
else if (event->type() == QEvent::MouseMove)//事件類型 滑鼠移動
{
if (mousePressed && (event->buttons() && Qt::LeftButton))
{
w->move(event->globalPos() - mousePoint);//窗體移動坐标
return true;
}
}
return QObject::eventFilter(obj, evt);
}
(3)控件圖示:
這裡的控件圖示實作,主要是采用:fontawesome圖示
fontawesome是一個圖示的集合,裡面有好多的圖示,使用起來也還是非常友善的。
圖示資訊可以到官網去查:http://fontawesome.io/cheatsheet/
fontawesome-webfont.ttf 下載下傳位址:http://pan.baidu.com/s/1sjyvp3v
具體的實作重要是通過調用以下函數:
QPushButton * Btn = new QPushButton(widget);
IconHelper::Instance()->SetIcon(Btn, QChar(), );
其中的QChar(0xf192) 是fontawesome是圖示集合中一個圖示對應的uncode編碼。使用不同的uncode編碼即對應了不同的圖示。
這裡還可以的根據指定的繪制得到需要的pixmap
//依據 寬度,高度,大小,圖示uncode十六進制編碼,顔色 繪制圖檔
QPixmap IconHelper::getPixmap(const QString &color, QChar c, quint32 size,
quint32 pixWidth, quint32 pixHeight)
{
QPixmap pix(pixWidth, pixHeight);//定義一個對象
pix.fill(Qt::transparent);//透明的黑色值(即,QColor(0,0,0,0))填充
QPainter painter;//定義繪圖對象
painter.begin(&pix);//調用begin時,所有的Pen Brush 重置為預設值。
//設定給定的絢染提示: 抗鋸齒+ 抗鋸齒文本
painter.setRenderHints(QPainter::Antialiasing |QPainter::TextAntialiasing);
painter.setPen(QColor(color));
painter.setBrush(QColor(color));
iconFont.setPointSize(size);
painter.setFont(iconFont);//設定字型
painter.drawText(pix.rect(), Qt::AlignCenter, c);//畫圖示 設定文本中央對齊,
painter.end();//結束繪畫。 繪畫時使用的任何資源都被釋放。
return pix;
}
通過調用:
QPixmap pix = IconHelper::Instance()->getPixmap(listColorText.at(i), listChar.at(i), iconSize, iconWidth, iconHeight);//根據指定繪制圖示樣式
btn->setIcon(QIcon(pix));//設定圖示
項目代碼在我的github倉庫:
https://github.com/xiedonghuilove/Package/tree/master/QFreamWork/uidemo18