改进代码

master
feiyangqingyun 2021-09-20 12:45:58 +08:00
parent f0abf6588e
commit 047b7959ac
36 changed files with 1220 additions and 3657 deletions

View File

@ -59,3 +59,8 @@ SUBDIRS += designer #QtDesigner4源码
lessThan(QT_MAJOR_VERSION, 6) { lessThan(QT_MAJOR_VERSION, 6) {
SUBDIRS += qwtdemo #qwt的源码版本无需插件直接源码集成到你的项目即可 SUBDIRS += qwtdemo #qwt的源码版本无需插件直接源码集成到你的项目即可
} }
#6.0到6.2之间的版本还有点问题等待正式发布的时候再试试
greaterThan(QT_MAJOR_VERSION, 5) {
SUBDIRS -= emailtool
}

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

View File

@ -8,11 +8,18 @@
/** /**
* :feiyangqingyun(QQ:517216493) 2016-11-23 * :feiyangqingyun(QQ:517216493) 2016-11-23
* 1. * 1.
* 2. QLabel+QAbstractButton * 2. FontAliBaBaFontAwesomeFontWeather
* 3. * 3. QLabelQAbstractButton
* 4. * 4. QAbstractButton
* 5. * 5. getPixmap
* 6.
* 7.
* 8.
* 9. leftrighttopbottom
* 10.
* 11.
* 12. 使便
*/ */
#ifdef quc #ifdef quc

View File

@ -61,7 +61,7 @@ private:
//存储窗体默认的属性 //存储窗体默认的属性
Qt::WindowFlags flags; Qt::WindowFlags flags;
public: public slots:
//设置边距+可拖动+可拉伸 //设置边距+可拖动+可拉伸
void setPadding(int padding); void setPadding(int padding);
void setMoveEnable(bool moveEnable); void setMoveEnable(bool moveEnable);

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

View File

@ -7,6 +7,7 @@
frmIPAddress::frmIPAddress(QWidget *parent) : QWidget(parent), ui(new Ui::frmIPAddress) frmIPAddress::frmIPAddress(QWidget *parent) : QWidget(parent), ui(new Ui::frmIPAddress)
{ {
ui->setupUi(this); ui->setupUi(this);
on_btnSetIP_clicked();
} }
frmIPAddress::~frmIPAddress() frmIPAddress::~frmIPAddress()

View File

@ -6,19 +6,19 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>500</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Form</string> <string>Form</string>
</property> </property>
<widget class="QWidget" name=""> <widget class="QWidget" name="layoutWidget">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>9</x> <x>10</x>
<y>9</y> <y>10</y>
<width>481</width> <width>281</width>
<height>71</height> <height>71</height>
</rect> </rect>
</property> </property>
@ -29,7 +29,7 @@
<item row="1" column="0"> <item row="1" column="0">
<widget class="QPushButton" name="btnSetIP"> <widget class="QPushButton" name="btnSetIP">
<property name="text"> <property name="text">
<string>设置IP</string> <string>填入IP</string>
</property> </property>
</widget> </widget>
</item> </item>

View File

@ -7,7 +7,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -16,7 +16,6 @@ frmMaskWidget::frmMaskWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmM
frmMaskWidget::~frmMaskWidget() frmMaskWidget::~frmMaskWidget()
{ {
delete ui; delete ui;
exit(0);
} }
void frmMaskWidget::initForm() void frmMaskWidget::initForm()

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>600</width> <width>800</width>
<height>480</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>500</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

View File

@ -10,6 +10,13 @@ frmNavButton::frmNavButton(QWidget *parent) : QWidget(parent), ui(new Ui::frmNav
{ {
ui->setupUi(this); ui->setupUi(this);
this->initForm(); this->initForm();
this->initBtn1();
this->initBtn2();
this->initBtn3();
this->initBtn4();
this->initBtn5();
this->initBtn6();
this->initBtn7();
} }
frmNavButton::~frmNavButton() frmNavButton::~frmNavButton()
@ -18,6 +25,22 @@ frmNavButton::~frmNavButton()
} }
void frmNavButton::initForm() void frmNavButton::initForm()
{
icons << 0xf17b << 0xf002 << 0xf013 << 0xf021 << 0xf0e0 << 0xf135;
ui->navButton11->setChecked(true);
ui->navButton23->setChecked(true);
ui->navButton31->setChecked(true);
ui->navButton44->setChecked(true);
ui->navButton53->setChecked(true);
ui->navButton61->setChecked(true);
ui->navButton75->setChecked(true);
//设置整体圆角
ui->widgetNav5->setStyleSheet(".QWidget{background:#292929;border:1px solid #292929;border-radius:20px;}");
}
void frmNavButton::initBtn1()
{ {
quint32 size = 15; quint32 size = 15;
quint32 pixWidth = 15; quint32 pixWidth = 15;
@ -44,13 +67,14 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick1())); connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick1()));
} }
}
size = 15; void frmNavButton::initBtn2()
pixWidth = 20; {
pixHeight = 20; quint32 size = 15;
quint32 pixWidth = 20;
quint32 pixHeight = 20;
QList<int> icons;
icons << 0xf17b << 0xf002 << 0xf013 << 0xf021 << 0xf0e0 << 0xf135;
QColor normalBgColor = QColor("#2D9191"); QColor normalBgColor = QColor("#2D9191");
QColor hoverBgColor = QColor("#187294"); QColor hoverBgColor = QColor("#187294");
QColor checkBgColor = QColor("#145C75"); QColor checkBgColor = QColor("#145C75");
@ -90,13 +114,20 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick2())); connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick2()));
} }
}
normalBgColor = QColor("#292F38"); void frmNavButton::initBtn3()
hoverBgColor = QColor("#1D2025"); {
checkBgColor = QColor("#1D2025"); quint32 size = 15;
normalTextColor = QColor("#54626F"); quint32 pixWidth = 20;
hoverTextColor = QColor("#FDFDFD"); quint32 pixHeight = 20;
checkTextColor = QColor("#FDFDFD");
QColor normalBgColor = QColor("#292F38");
QColor hoverBgColor = QColor("#1D2025");
QColor checkBgColor = QColor("#1D2025");
QColor normalTextColor = QColor("#54626F");
QColor hoverTextColor = QColor("#FDFDFD");
QColor checkTextColor = QColor("#FDFDFD");
btns3 << ui->navButton31 << ui->navButton32 << ui->navButton33 << ui->navButton34; btns3 << ui->navButton31 << ui->navButton32 << ui->navButton33 << ui->navButton34;
for (int i = 0; i < btns3.count(); i++) { for (int i = 0; i < btns3.count(); i++) {
@ -132,15 +163,18 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick3())); connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick3()));
} }
}
size = 15; void frmNavButton::initBtn4()
pixWidth = 15; {
pixHeight = 15; quint32 size = 15;
quint32 pixWidth = 15;
quint32 pixHeight = 15;
icon = 0xf105; int icon = 0xf105;
iconNormal = IconHelper::getPixmap(QColor(100, 100, 100).name(), icon, size, pixWidth, pixHeight); QPixmap iconNormal = IconHelper::getPixmap(QColor(100, 100, 100).name(), icon, size, pixWidth, pixHeight);
iconHover = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight); QPixmap iconHover = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
iconCheck = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight); QPixmap iconCheck = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
btns4 << ui->navButton41 << ui->navButton42 << ui->navButton43 << ui->navButton44; btns4 << ui->navButton41 << ui->navButton42 << ui->navButton43 << ui->navButton44;
for (int i = 0; i < btns4.count(); i++) { for (int i = 0; i < btns4.count(); i++) {
@ -154,7 +188,7 @@ void frmNavButton::initForm()
btn->setLinePosition(NavButton::LinePosition_Right); btn->setLinePosition(NavButton::LinePosition_Right);
btn->setShowIcon(true); btn->setShowIcon(true);
btn->setIconSpace(25); btn->setIconSpace(10);
btn->setIconSize(QSize(15, 15)); btn->setIconSize(QSize(15, 15));
btn->setIconNormal(iconNormal); btn->setIconNormal(iconNormal);
btn->setIconHover(iconHover); btn->setIconHover(iconHover);
@ -162,21 +196,24 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick4())); connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick4()));
} }
}
size = 15; void frmNavButton::initBtn5()
pixWidth = 20; {
pixHeight = 20;
QFont font; QFont font;
font.setPixelSize(15); font.setPixelSize(15);
font.setBold(true); font.setBold(true);
normalBgColor = QColor("#292929"); quint32 size = 15;
hoverBgColor = QColor("#064077"); quint32 pixWidth = 20;
checkBgColor = QColor("#10689A"); quint32 pixHeight = 20;
normalTextColor = QColor("#FFFFFF");
hoverTextColor = Qt::yellow; QColor normalBgColor = QColor("#292929");
checkTextColor = QColor("#FFFFFF"); QColor hoverBgColor = QColor("#064077");
QColor checkBgColor = QColor("#10689A");
QColor normalTextColor = QColor("#FFFFFF");
QColor hoverTextColor = Qt::yellow;
QColor checkTextColor = QColor("#FFFFFF");
btns5 << ui->navButton51 << ui->navButton52 << ui->navButton53 << ui->navButton54 << ui->navButton55; btns5 << ui->navButton51 << ui->navButton52 << ui->navButton53 << ui->navButton54 << ui->navButton55;
for (int i = 0; i < btns5.count(); i++) { for (int i = 0; i < btns5.count(); i++) {
@ -210,13 +247,24 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick5())); connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick5()));
} }
}
normalBgColor = QColor("#E6393D"); void frmNavButton::initBtn6()
hoverBgColor = QColor("#EE0000"); {
checkBgColor = QColor("#A40001"); QFont font;
normalTextColor = QColor("#FFFFFF"); font.setPixelSize(15);
hoverTextColor = QColor("#FFFFFF"); font.setBold(true);
checkTextColor = QColor("#FFFFFF");
quint32 size = 15;
quint32 pixWidth = 20;
quint32 pixHeight = 20;
QColor normalBgColor = QColor("#E6393D");
QColor hoverBgColor = QColor("#EE0000");
QColor checkBgColor = QColor("#A40001");
QColor normalTextColor = QColor("#FFFFFF");
QColor hoverTextColor = QColor("#FFFFFF");
QColor checkTextColor = QColor("#FFFFFF");
btns6 << ui->navButton61 << ui->navButton62 << ui->navButton63 << ui->navButton64 << ui->navButton65; btns6 << ui->navButton61 << ui->navButton62 << ui->navButton63 << ui->navButton64 << ui->navButton65;
for (int i = 0; i < btns6.count(); i++) { for (int i = 0; i < btns6.count(); i++) {
@ -250,14 +298,25 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick6())); connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick6()));
} }
}
void frmNavButton::initBtn7()
{
QFont font;
font.setPixelSize(15);
font.setBold(true);
QColor normalTextColor = QColor("#FFFFFF");
QColor hoverTextColor = QColor("#FFFFFF");
QColor checkTextColor = QColor("#FFFFFF");
//设置背景色为画刷 //设置背景色为画刷
QLinearGradient normalBgBrush(0, 0, 0, ui->navButton61->height()); QLinearGradient normalBgBrush(0, 0, 0, ui->navButton71->height());
normalBgBrush.setColorAt(0.0, QColor("#3985BF")); normalBgBrush.setColorAt(0.0, QColor("#3985BF"));
normalBgBrush.setColorAt(0.5, QColor("#2972A9")); normalBgBrush.setColorAt(0.5, QColor("#2972A9"));
normalBgBrush.setColorAt(1.0, QColor("#1C6496")); normalBgBrush.setColorAt(1.0, QColor("#1C6496"));
QLinearGradient hoverBgBrush(0, 0, 0, ui->navButton61->height()); QLinearGradient hoverBgBrush(0, 0, 0, ui->navButton71->height());
hoverBgBrush.setColorAt(0.0, QColor("#4897D1")); hoverBgBrush.setColorAt(0.0, QColor("#4897D1"));
hoverBgBrush.setColorAt(0.5, QColor("#3283BC")); hoverBgBrush.setColorAt(0.5, QColor("#3283BC"));
hoverBgBrush.setColorAt(1.0, QColor("#3088C3")); hoverBgBrush.setColorAt(1.0, QColor("#3088C3"));
@ -283,17 +342,6 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick7())); connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick7()));
} }
ui->navButton11->setChecked(true);
ui->navButton23->setChecked(true);
ui->navButton31->setChecked(true);
ui->navButton44->setChecked(true);
ui->navButton53->setChecked(true);
ui->navButton61->setChecked(true);
ui->navButton75->setChecked(true);
//设置整体圆角
ui->widgetNav5->setStyleSheet(".QWidget{background:#292929;border:1px solid #292929;border-radius:20px;}");
} }
void frmNavButton::buttonClick1() void frmNavButton::buttonClick1()

View File

@ -19,6 +19,7 @@ public:
private: private:
Ui::frmNavButton *ui; Ui::frmNavButton *ui;
QList<int> icons;
QList<NavButton *> btns1; QList<NavButton *> btns1;
QList<NavButton *> btns2; QList<NavButton *> btns2;
QList<NavButton *> btns3; QList<NavButton *> btns3;
@ -29,6 +30,14 @@ private:
private slots: private slots:
void initForm(); void initForm();
void initBtn1();
void initBtn2();
void initBtn3();
void initBtn4();
void initBtn5();
void initBtn6();
void initBtn7();
void buttonClick1(); void buttonClick1();
void buttonClick2(); void buttonClick2();
void buttonClick3(); void buttonClick3();

View File

@ -6,28 +6,22 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>500</width> <width>800</width>
<height>300</height> <height>605</height>
</rect> </rect>
</property> </property>
<property name="minimumSize">
<size>
<width>500</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>500</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Form</string> <string>Form</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="0" colspan="4">
<widget class="QWidget" name="widgetNav7" native="true"> <widget class="QWidget" name="widgetNav7" native="true">
<property name="geometry">
<rect>
<x>11</x>
<y>245</y>
<width>611</width>
<height>40</height>
</rect>
</property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>0</width> <width>0</width>
@ -136,9 +130,15 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</item>
<item row="0" column="0">
<widget class="QWidget" name="widgetNav1" native="true"> <widget class="QWidget" name="widgetNav1" native="true">
<property name="geometry">
<rect>
<x>11</x>
<y>11</y>
<width>120</width>
<height>133</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>
@ -182,9 +182,15 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</item>
<item row="0" column="1">
<widget class="QWidget" name="widgetNav2" native="true"> <widget class="QWidget" name="widgetNav2" native="true">
<property name="geometry">
<rect>
<x>140</x>
<y>11</y>
<width>120</width>
<height>133</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>
@ -228,9 +234,15 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</item>
<item row="1" column="0" colspan="4">
<widget class="QWidget" name="widgetNav5" native="true"> <widget class="QWidget" name="widgetNav5" native="true">
<property name="geometry">
<rect>
<x>11</x>
<y>151</y>
<width>511</width>
<height>40</height>
</rect>
</property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>0</width> <width>0</width>
@ -326,9 +338,15 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</item>
<item row="2" column="0" colspan="4">
<widget class="QWidget" name="widgetNav6" native="true"> <widget class="QWidget" name="widgetNav6" native="true">
<property name="geometry">
<rect>
<x>11</x>
<y>198</y>
<width>511</width>
<height>40</height>
</rect>
</property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>0</width> <width>0</width>
@ -424,9 +442,15 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</item>
<item row="0" column="2">
<widget class="QWidget" name="widgetNav3" native="true"> <widget class="QWidget" name="widgetNav3" native="true">
<property name="geometry">
<rect>
<x>270</x>
<y>11</y>
<width>120</width>
<height>133</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing"> <property name="spacing">
<number>6</number> <number>6</number>
@ -473,9 +497,15 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</item>
<item row="0" column="3">
<widget class="QWidget" name="widgetNav4" native="true"> <widget class="QWidget" name="widgetNav4" native="true">
<property name="geometry">
<rect>
<x>400</x>
<y>11</y>
<width>120</width>
<height>133</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4"> <layout class="QVBoxLayout" name="verticalLayout_4">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>
@ -519,21 +549,6 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</item>
<item row="4" column="0" colspan="4">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>

View File

@ -2,6 +2,9 @@
IconHelper *IconHelper::iconFontAliBaBa = 0; IconHelper *IconHelper::iconFontAliBaBa = 0;
IconHelper *IconHelper::iconFontAwesome = 0; IconHelper *IconHelper::iconFontAwesome = 0;
IconHelper *IconHelper::iconFontWeather = 0;
int IconHelper::iconFontIndex = -1;
void IconHelper::initFont() void IconHelper::initFont()
{ {
static bool isInit = false; static bool isInit = false;
@ -13,103 +16,101 @@ void IconHelper::initFont()
if (iconFontAwesome == 0) { if (iconFontAwesome == 0) {
iconFontAwesome = new IconHelper(":/image/fontawesome-webfont.ttf", "FontAwesome"); iconFontAwesome = new IconHelper(":/image/fontawesome-webfont.ttf", "FontAwesome");
} }
if (iconFontWeather == 0) {
iconFontWeather = new IconHelper(":/image/pe-icon-set-weather.ttf", "pe-icon-set-weather");
} }
}
}
QFont IconHelper::getIconFontAliBaBa()
{
initFont();
return iconFontAliBaBa->getIconFont();
}
QFont IconHelper::getIconFontAwesome()
{
initFont();
return iconFontAwesome->getIconFont();
}
QFont IconHelper::getIconFontWeather()
{
initFont();
return iconFontWeather->getIconFont();
}
IconHelper *IconHelper::getIconHelper(int icon)
{
initFont();
//指定了字体索引则取对应索引的字体类
//没指定则自动根据不同的字体的值选择对应的类
//由于部分值范围冲突所以可以指定索引来取
//fontawesome 0xf000-0xf2e0
//iconfont 0xe501-0xe793 0xe8d5-0xea5d
//weather 0xe900-0xe9cf
IconHelper *iconHelper = iconFontAwesome;
if (iconFontIndex < 0) {
if ((icon > 0xe501 && icon < 0xe793) || (icon > 0xe8d5 && icon < 0xea5d)) {
iconHelper = iconFontAliBaBa;
}
} else if (iconFontIndex == 0) {
iconHelper = iconFontAliBaBa;
} else if (iconFontIndex == 1) {
iconHelper = iconFontAwesome;
} else if (iconFontIndex == 2) {
iconHelper = iconFontWeather;
}
return iconHelper;
} }
void IconHelper::setIcon(QLabel *lab, int icon, quint32 size) void IconHelper::setIcon(QLabel *lab, int icon, quint32 size)
{ {
initFont(); getIconHelper(icon)->setIcon1(lab, icon, size);
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setIcon1(lab, icon, size);
} else if (icon > 0xf000) {
iconFontAwesome->setIcon1(lab, icon, size);
}
} }
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size) void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
{ {
initFont(); getIconHelper(icon)->setIcon1(btn, icon, size);
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setIcon1(btn, icon, size);
} else if (icon > 0xf000) {
iconFontAwesome->setIcon1(btn, icon, size);
}
} }
void IconHelper::setPixmap(QAbstractButton *btn, const QColor &color, int icon, quint32 size, void IconHelper::setPixmap(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags) quint32 width, quint32 height, int flags)
{ {
initFont(); getIconHelper(icon)->setPixmap1(btn, color, icon, size, width, height, flags);
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setPixmap1(btn, color, icon, size, width, height, flags);
} else if (icon > 0xf000) {
iconFontAwesome->setPixmap1(btn, color, icon, size, width, height, flags);
}
} }
QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size, QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags) quint32 width, quint32 height, int flags)
{ {
initFont(); return getIconHelper(icon)->getPixmap1(color, icon, size, width, height, flags);
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
QPixmap pix;
if (icon > 0xe000 && icon < 0xf000) {
pix = iconFontAliBaBa->getPixmap1(color, icon, size, width, height, flags);
} else if (icon > 0xf000) {
pix = iconFontAwesome->getPixmap1(color, icon, size, width, height, flags);
}
return pix;
} }
void IconHelper::setStyle(QWidget *widget, QList<QPushButton *> btns, void IconHelper::setStyle(QWidget *widget, QList<QPushButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor) QList<int> icons, const IconHelper::StyleColor &styleColor)
{ {
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
int icon = icons.first(); int icon = icons.first();
if (icon > 0xe000 && icon < 0xf000) { getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
} else if (icon > 0xf000) {
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
}
} }
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns, void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor) QList<int> icons, const IconHelper::StyleColor &styleColor)
{ {
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
int icon = icons.first(); int icon = icons.first();
if (icon > 0xe000 && icon < 0xf000) { getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
} else if (icon > 0xf000) {
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
}
} }
void IconHelper::setStyle(QWidget *widget, QList<QAbstractButton *> btns, void IconHelper::setStyle(QWidget *widget, QList<QAbstractButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor) QList<int> icons, const IconHelper::StyleColor &styleColor)
{ {
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
int icon = icons.first(); int icon = icons.first();
if (icon > 0xe000 && icon < 0xf000) { getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
} else if (icon > 0xf000) {
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
}
} }
IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject *parent) : QObject(parent) IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject *parent) : QObject(parent)
{ {
//判断图形字体是否存在,不存在则加入 //判断图形字体是否存在,不存在则加入
@ -122,6 +123,7 @@ IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject
} }
} }
//再次判断是否包含字体名称防止加载失败
if (fontDb.families().contains(fontName)) { if (fontDb.families().contains(fontName)) {
iconFont = QFont(fontName); iconFont = QFont(fontName);
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0)) #if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
@ -179,6 +181,11 @@ void IconHelper::toggled(bool checked)
} }
} }
QFont IconHelper::getIconFont()
{
return this->iconFont;
}
void IconHelper::setIcon1(QLabel *lab, int icon, quint32 size) void IconHelper::setIcon1(QLabel *lab, int icon, quint32 size)
{ {
iconFont.setPixelSize(size); iconFont.setPixelSize(size);
@ -269,8 +276,9 @@ void IconHelper::setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList
} }
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色 //如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
//如果图标在文字上面而设置的边框是 top bottom 也需要启用加深边框
QStringList qss; QStringList qss;
if (styleColor.textBesideIcon) { if (styleColor.defaultBorder) {
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}") qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.normalBgColor).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor); .arg(position).arg(strBorder).arg(styleColor.normalBgColor).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
} else { } else {

View File

@ -8,11 +8,18 @@
/** /**
* :feiyangqingyun(QQ:517216493) 2016-11-23 * :feiyangqingyun(QQ:517216493) 2016-11-23
* 1. * 1.
* 2. QLabel+QAbstractButton * 2. FontAliBaBaFontAwesomeFontWeather
* 3. * 3. QLabelQAbstractButton
* 4. * 4. QAbstractButton
* 5. * 5. getPixmap
* 6.
* 7.
* 8.
* 9. leftrighttopbottom
* 10.
* 11.
* 12. 使便
*/ */
#ifdef quc #ifdef quc
@ -28,7 +35,7 @@ public:
//样式颜色结构体 //样式颜色结构体
struct StyleColor { struct StyleColor {
QString position; //位置 left right top bottom QString position; //位置 left right top bottom
bool textBesideIcon; //文字在图标左侧 bool defaultBorder; //默认有边框
quint32 iconSize; //图标字体尺寸 quint32 iconSize; //图标字体尺寸
quint32 iconWidth; //图标图片宽度 quint32 iconWidth; //图标图片宽度
@ -48,7 +55,7 @@ public:
StyleColor() { StyleColor() {
position = "left"; position = "left";
textBesideIcon = false; defaultBorder = false;
iconSize = 12; iconSize = 12;
iconWidth = 15; iconWidth = 15;
@ -87,20 +94,38 @@ public:
static IconHelper *iconFontAliBaBa; static IconHelper *iconFontAliBaBa;
//FontAwesome图形字体类 //FontAwesome图形字体类
static IconHelper *iconFontAwesome; static IconHelper *iconFontAwesome;
//天气图形字体类
static IconHelper *iconFontWeather;
//图形字体索引
static int iconFontIndex;
//初始化图形字体 //初始化图形字体
static void initFont(); static void initFont();
//获取图形字体
static QFont getIconFontAliBaBa();
static QFont getIconFontAwesome();
static QFont getIconFontWeather();
//根据值获取图形字体类
static IconHelper *getIconHelper(int icon);
//设置图形字体到标签
static void setIcon(QLabel *lab, int icon, quint32 size = 12); static void setIcon(QLabel *lab, int icon, quint32 size = 12);
//设置图形字体到按钮
static void setIcon(QAbstractButton *btn, int icon, quint32 size = 12); static void setIcon(QAbstractButton *btn, int icon, quint32 size = 12);
//设置图形字体到图标
static void setPixmap(QAbstractButton *btn, const QColor &color, static void setPixmap(QAbstractButton *btn, const QColor &color,
int icon, quint32 size = 12, int icon, quint32 size = 12,
quint32 width = 15, quint32 height = 15, quint32 width = 15, quint32 height = 15,
int flags = Qt::AlignCenter); int flags = Qt::AlignCenter);
//获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
static QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12, static QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12,
quint32 width = 15, quint32 height = 15, quint32 width = 15, quint32 height = 15,
int flags = Qt::AlignCenter); int flags = Qt::AlignCenter);
//指定导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
static void setStyle(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor); static void setStyle(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
static void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor); static void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
static void setStyle(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor); static void setStyle(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
@ -124,6 +149,9 @@ private slots:
void toggled(bool checked); void toggled(bool checked);
public: public:
//获取图形字体
QFont getIconFont();
//设置图形字体到标签 //设置图形字体到标签
void setIcon1(QLabel *lab, int icon, quint32 size = 12); void setIcon1(QLabel *lab, int icon, quint32 size = 12);
//设置图形字体到按钮 //设置图形字体到按钮

View File

@ -3,6 +3,7 @@
#include "frmntpclient.h" #include "frmntpclient.h"
#include "ui_frmntpclient.h" #include "ui_frmntpclient.h"
#include "ntpclient.h" #include "ntpclient.h"
#include "qdebug.h"
frmNtpClient::frmNtpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmNtpClient) frmNtpClient::frmNtpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmNtpClient)
{ {

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -26,7 +26,6 @@ private slots:
void on_ckTimer_stateChanged(int arg1); void on_ckTimer_stateChanged(int arg1);
void on_ckNet_stateChanged(int arg1); void on_ckNet_stateChanged(int arg1);
void on_ckSave_stateChanged(int arg1); void on_ckSave_stateChanged(int arg1);
}; };
#endif // FRMSAVELOG_H #endif // FRMSAVELOG_H

View File

@ -6,51 +6,68 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Form</string> <string>Form</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item row="1" column="1"> <item>
<widget class="QCheckBox" name="ckTimer"> <widget class="QTextEdit" name="txtMain"/>
<property name="text">
<string>定时器打印消息</string>
</property>
</widget>
</item> </item>
<item row="1" column="3"> <item>
<widget class="QCheckBox" name="ckSave"> <layout class="QHBoxLayout" name="horizontalLayout">
<property name="text"> <item>
<string>保存日志</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="btnDebug"> <widget class="QPushButton" name="btnDebug">
<property name="sizePolicy"> <property name="minimumSize">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed"> <size>
<horstretch>0</horstretch> <width>130</width>
<verstretch>0</verstretch> <height>0</height>
</sizepolicy> </size>
</property> </property>
<property name="text"> <property name="text">
<string>手动插入消息</string> <string>手动插入消息</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="0" colspan="4"> <item>
<widget class="QTextEdit" name="txtMain"/> <widget class="QCheckBox" name="ckTimer">
<property name="text">
<string>定时器打印消息</string>
</property>
</widget>
</item> </item>
<item row="1" column="2"> <item>
<widget class="QCheckBox" name="ckNet"> <widget class="QCheckBox" name="ckNet">
<property name="text"> <property name="text">
<string>重定向到网络</string> <string>重定向到网络</string>
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="ckSave">
<property name="text">
<string>保存日志</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

View File

@ -2,6 +2,7 @@
#include "frmsaveruntime.h" #include "frmsaveruntime.h"
#include "ui_frmsaveruntime.h" #include "ui_frmsaveruntime.h"
#include "qfile.h"
#include "saveruntime.h" #include "saveruntime.h"
frmSaveRunTime::frmSaveRunTime(QWidget *parent) : QWidget(parent), ui(new Ui::frmSaveRunTime) frmSaveRunTime::frmSaveRunTime(QWidget *parent) : QWidget(parent), ui(new Ui::frmSaveRunTime)
@ -21,4 +22,27 @@ void frmSaveRunTime::on_checkBox_stateChanged(int arg1)
} else { } else {
SaveRunTime::Instance()->start(); SaveRunTime::Instance()->start();
} }
on_btnOpen_clicked();
}
void frmSaveRunTime::on_btnAppend_clicked()
{
SaveRunTime::Instance()->initLog();
SaveRunTime::Instance()->appendLog();
on_btnOpen_clicked();
}
void frmSaveRunTime::on_btnUpdate_clicked()
{
SaveRunTime::Instance()->saveLog();
on_btnOpen_clicked();
}
void frmSaveRunTime::on_btnOpen_clicked()
{
QString fileName = QString("%1/examples_runtime_%2.txt").arg(qApp->applicationDirPath()).arg(QDate::currentDate().year());
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text)) {
ui->txtMain->setText(file.readAll());
}
} }

View File

@ -20,6 +20,9 @@ private:
private slots: private slots:
void on_checkBox_stateChanged(int arg1); void on_checkBox_stateChanged(int arg1);
void on_btnAppend_clicked();
void on_btnUpdate_clicked();
void on_btnOpen_clicked();
}; };
#endif // FRMSAVERUNTIME_H #endif // FRMSAVERUNTIME_H

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -15,18 +15,10 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<spacer name="verticalSpacer"> <widget class="QTextEdit" name="txtMain"/>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item> </item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
<widget class="QCheckBox" name="checkBox"> <widget class="QCheckBox" name="checkBox">
<property name="text"> <property name="text">
@ -34,6 +26,60 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="btnAppend">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>插入一条记录</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnUpdate">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>更新一条记录</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnOpen">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>打开记录文件</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

27
trayicon/frmtrayicon.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "frmtrayicon.h"
#include "ui_frmtrayicon.h"
#include "trayicon.h"
frmTrayIcon::frmTrayIcon(QWidget *parent) : QWidget(parent), ui(new Ui::frmTrayIcon)
{
ui->setupUi(this);
TrayIcon::Instance()->setIcon(":/main.ico");
TrayIcon::Instance()->setMainWidget(this);
}
frmTrayIcon::~frmTrayIcon()
{
TrayIcon::Instance()->setVisible(false);
delete ui;
}
void frmTrayIcon::on_btnShow_clicked()
{
TrayIcon::Instance()->setVisible(true);
TrayIcon::Instance()->showMessage("自定义控件大全", "已经最小化到托盘,双击打开!");
}
void frmTrayIcon::on_btnHide_clicked()
{
TrayIcon::Instance()->setVisible(false);
}

26
trayicon/frmtrayicon.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef FRMTRAYICON_H
#define FRMTRAYICON_H
#include <QWidget>
namespace Ui {
class frmTrayIcon;
}
class frmTrayIcon : public QWidget
{
Q_OBJECT
public:
explicit frmTrayIcon(QWidget *parent = 0);
~frmTrayIcon();
private:
Ui::frmTrayIcon *ui;
private slots:
void on_btnShow_clicked();
void on_btnHide_clicked();
};
#endif // FRMTRAYICON_H

45
trayicon/frmtrayicon.ui Normal file
View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmTrayIcon</class>
<widget class="QWidget" name="frmTrayIcon">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QPushButton" name="btnShow">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>92</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>显示托盘</string>
</property>
</widget>
<widget class="QPushButton" name="btnHide">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>92</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>隐藏托盘</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

31
trayicon/main.cpp Normal file
View File

@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmtrayicon.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
frmTrayIcon w;
w.setWindowTitle("托盘图标");
w.show();
return a.exec();
}

BIN
trayicon/main.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

5
trayicon/main.qrc Normal file
View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>main.ico</file>
</qresource>
</RCC>

98
trayicon/trayicon.cpp Normal file
View File

@ -0,0 +1,98 @@
#pragma execution_character_set("utf-8")
#include "trayicon.h"
#include "qmutex.h"
#include "qmenu.h"
#include "qapplication.h"
#include "qdebug.h"
QScopedPointer<TrayIcon> TrayIcon::self;
TrayIcon *TrayIcon::Instance()
{
if (self.isNull()) {
static QMutex mutex;
QMutexLocker locker(&mutex);
if (self.isNull()) {
self.reset(new TrayIcon);
}
}
return self.data();
}
TrayIcon::TrayIcon(QObject *parent) : QObject(parent)
{
mainWidget = 0;
trayIcon = new QSystemTrayIcon(this);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
menu = new QMenu;
exitDirect = true;
}
void TrayIcon::iconIsActived(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick: {
mainWidget->showNormal();
break;
}
default:
break;
}
}
bool TrayIcon::getVisible() const
{
return trayIcon->isVisible();
}
void TrayIcon::setExitDirect(bool exitDirect)
{
if (this->exitDirect != exitDirect) {
this->exitDirect = exitDirect;
}
}
void TrayIcon::setMainWidget(QWidget *mainWidget)
{
this->mainWidget = mainWidget;
menu->addAction("主界面", mainWidget, SLOT(showNormal()));
if (exitDirect) {
menu->addAction("退出", this, SLOT(closeAll()));
} else {
menu->addAction("退出", this, SIGNAL(trayIconExit()));
}
trayIcon->setContextMenu(menu);
}
void TrayIcon::showMessage(const QString &title, const QString &msg, QSystemTrayIcon::MessageIcon icon, int msecs)
{
trayIcon->showMessage(title, msg, icon, msecs);
}
void TrayIcon::setIcon(const QString &strIcon)
{
trayIcon->setIcon(QIcon(strIcon));
}
void TrayIcon::setToolTip(const QString &tip)
{
trayIcon->setToolTip(tip);
}
void TrayIcon::setVisible(bool visible)
{
trayIcon->setVisible(visible);
}
void TrayIcon::closeAll()
{
trayIcon->hide();
trayIcon->deleteLater();
qApp->exit();
}

66
trayicon/trayicon.h Normal file
View File

@ -0,0 +1,66 @@
#ifndef TRAYICON_H
#define TRAYICON_H
/**
* :feiyangqingyun(QQ:517216493) 2017-1-8
* 1.
* 2.
* 3.
* 4.
*/
#include <QObject>
#include <QSystemTrayIcon>
class QMenu;
#ifdef quc
class Q_DECL_EXPORT TrayIcon : public QObject
#else
class TrayIcon : public QObject
#endif
{
Q_OBJECT
public:
static TrayIcon *Instance();
explicit TrayIcon(QObject *parent = 0);
private:
static QScopedPointer<TrayIcon> self;
QWidget *mainWidget; //对应所属主窗体
QSystemTrayIcon *trayIcon; //托盘对象
QMenu *menu; //右键菜单
bool exitDirect; //是否直接退出
private slots:
void iconIsActived(QSystemTrayIcon::ActivationReason reason);
public:
bool getVisible() const;
public Q_SLOTS:
//设置是否直接退出,如果不是直接退出则发送信号给主界面
void setExitDirect(bool exitDirect);
//设置所属主窗体
void setMainWidget(QWidget *mainWidget);
//显示消息
void showMessage(const QString &title, const QString &msg,
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, int msecs = 5000);
//设置图标
void setIcon(const QString &strIcon);
//设置提示信息
void setToolTip(const QString &tip);
//设置是否可见
void setVisible(bool visible);
//退出所有
void closeAll();
Q_SIGNALS:
void trayIconExit();
};
#endif // TRAYICON_H

19
trayicon/trayicon.pro Normal file
View File

@ -0,0 +1,19 @@
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = trayicon
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmtrayicon.cpp
SOURCES += trayicon.cpp
HEADERS += frmtrayicon.h
HEADERS += trayicon.h
FORMS += frmtrayicon.ui
RESOURCES += main.qrc

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>800</width>
<height>300</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">