QT添加托盘代码
代码
1 #include "vs2008smart.h"
2 #include "stdafx.h"
3 Vs2008Smart::Vs2008Smart(QWidget *parent, Qt::WFlags flags)
4 : QMainWindow(parent, flags)
5 {
6 ui.setupUi(this);
7
8 //this->setAttribute(Qt::WA_TranslucentBackground);//透明效果
9
10 //窗口居中
11 QDesktopWidget * deskTop = QApplication::desktop();
12 int screenIndex = deskTop->screenNumber ( this ); //得到this widget主要出于哪个屏幕中,返回屏幕index
13 QRect rect = deskTop->screenGeometry(screenIndex); //得到该屏幕大小
14 int x = (rect.width() - width())/2;
15 int y = (rect.height() - height())/2;
16 this->move(x,y);
17
18 //以可读写方式打开串口
19 /*
20 QFileSystemModel *model = new QFileSystemModel;
21 model->setRootPath(QDir::currentPath());
22 //QTreeView *tree = new QTreeView(splitter);
23 ui.treeView->setModel(model);
24 */
25
26 //添加listView Items
27 //ui.listWidget->insertItem(itemSize++,tr("通讯设置"));
28 //ui.listWidget->insertItem(itemSize++,QObject::tr("摄像头位置调整"));
29 //ui.listWidget->insertItem(itemSize++,tr("标定视频范围"));
30 itemSize=0;
31
32 //TODO:添加页面
33
34 //comPage=new com_Config();
35 camPage=new cam_Config();
36 demarcatePage=new demarcate_Config();
37 matrixPage=new matrix_Config();
38
39 /*
40 * 移除所有widgets
41 */
42 while(ui.stackedWidget->count()!=0)
43 ui.stackedWidget->removeWidget(ui.stackedWidget->widget(0));
44
45 //addPageItem(tr("通讯设置"),comPage);
46 addPageItem(tr("摄像机位置调整"),camPage);
47 addPageItem(tr("摄像机有效信息位置"),demarcatePage);
48 addPageItem(tr("摄像机内参数标定"),matrixPage);
49
50 ui.listWidget->setEnabled(false);
51
52 setPageItemIndex(0);
53 //ui.stackedWidget->addWidget(directPage);
54
55 //ui.btn_prepage->setEnabled(false);
56 setBtnEnable();
57 connect(ui.btn_prepage,SIGNAL(clicked()),this,SLOT(setPrePage()));//连接下一页
58 connect(ui.btn_nextpage,SIGNAL(clicked()),this,SLOT(setNextPage()));
59 connect(ui.listWidget,SIGNAL(currentRowChanged(int)),this,SLOT(setStackedWidget(int)));//连接左边listWidget
60 //connect(ui.stackedWidget,SIGNAL(currentChanged(int)),ui.listWidget,SLOT(setListWidget(int)));
61 connect(this,SIGNAL(updateListWidget(int)),this,SLOT(setListWidget(int)));
62
63 //connect(ui.btn_close,SIGNAL(clicked()),qApp,SLOT(quit()));
64
65 MoveMouseTimer=new QTimer(this);
66 connect(MoveMouseTimer,SIGNAL(timeout()),this,SLOT(moveMouse()));
67 connect(ui.btn_openV,SIGNAL(clicked()),this,SLOT(ControlmoveMouse()));
68 x=0;y=0;
69
70 /*
71 * 增加托盘的相关初始化代码
72 */
73 iTrayIcon=new QSystemTrayIcon();
74 iTrayIcon->setIcon( QIcon("IconTray.png"));
75 iTrayIcon->show();
76 iTrayIcon->setToolTip(QString(tr("One world, One dream!")));
77 iTrayIcon->showMessage("Message","Hi, I am here.");
78 connect( iTrayIcon, SIGNAL(messageClicked ()), this, SLOT(hiMessage()));
79 addMenu();
80 //end of the trayicon
81
82 }
83
84 void Vs2008Smart::moveMouse()
85 {
86 SystemInteraction::MoveMouseToXY(x,y);
87 x+=100;
88 //y++;
89 if(x>60000)
90 {
91 MoveMouseTimer->stop();
92 x=0;
93 }
94 }
95 /*
96 * 移动鼠标至x,y坐标处
97 */
98 void Vs2008Smart::ControlmoveMouse()
99 {
100 if(MoveMouseTimer->isActive())
101 {
102 MoveMouseTimer->stop();
103 }
104 else
105 {
106 MoveMouseTimer->start(50);//0.5s中断一次
107 }
108 }
109
110 Vs2008Smart::~Vs2008Smart()
111 {
112 //delete comPage;
113 delete camPage;
114 delete demarcatePage;
115 delete matrixPage;
116 qDebug( "Vs2008Smart is destruct!\n");
117 }
118
119 /*
120 * 设置上一页下一页的使能状态
121 */
122 void Vs2008Smart::setBtnEnable()
123 {
124
125 ui.Page_Oper->setText(QString("第")+QString::number(ui.stackedWidget->currentIndex()+1)+QString("步/共")+QString::number(ui.stackedWidget->count())+QString("步"));
126 if(ui.stackedWidget->count()==0)
127 return;
128 if(ui.stackedWidget->currentIndex()==0)
129 ui.btn_prepage->setEnabled(false);
130 else
131 ui.btn_prepage->setEnabled(true);
132
133 if(ui.stackedWidget->currentIndex()==ui.stackedWidget->count()-1)
134 {
135 ui.btn_nextpage->setText("完成");
136 //ui.btn_nextpage->setEnabled(false);
137 }
138 else
139 {
140 ui.btn_nextpage->setEnabled(true);
141 ui.btn_nextpage->setText(tr("下一页"));
142 }
143 }
144
145 void Vs2008Smart::setPrePage()
146 {
147 ui.stackedWidget->setCurrentIndex(ui.stackedWidget->currentIndex()-1);//这里直接用slot,不会再次发送singal,或许这样理解比较好
148
149 setBtnEnable();
150 emit updateListWidget(ui.stackedWidget->currentIndex());
151 }
152
153 void Vs2008Smart::setNextPage()
154 {
155 ui.stackedWidget->setCurrentIndex(ui.stackedWidget->currentIndex()+1);
156 setBtnEnable();
157 emit updateListWidget(ui.stackedWidget->currentIndex());
158 }
159
160 void Vs2008Smart::setListWidget(int curpage)
161 {
162 ui.listWidget->setCurrentRow(curpage);
163 //setVision(curpage);//设置vision
164 }
165
166 void Vs2008Smart::setStackedWidget(int page)
167 {
168 ui.stackedWidget->setCurrentIndex(page);
169 //setVision(page);//设置vision
170 setBtnEnable();
171 }
172
173 //////////////////////////////////////////////////////////////////////////
174 /*
175 * 添加一个页面,包括item和widget页面
176 */
177 void Vs2008Smart::addPageItem(QString itemName,QWidget* pageWidget)
178 {
179 //
180 ui.listWidget->insertItem(itemSize++,itemName);
181 ui.stackedWidget->addWidget(pageWidget);
182 }
183
184 void Vs2008Smart::setPageItemIndex(int Index)
185 {
186 ui.listWidget->setCurrentRow(Index);
187 ui.stackedWidget->setCurrentIndex(Index);
188 }
189
190 /*
191 * 设置每个页面是否是当前显示页面的函数
192 */
193 /*
194 void Vs2008Smart::setVision(int Index)
195 {
196 switch(Index)
197 {
198 case 0:
199 camPage->isVision=true;
200 demarcatePage->isVision=false;
201 matrixPage->isVision=false;
202 break;
203 case 1:
204 camPage->isVision=false;
205 demarcatePage->isVision=true;
206 matrixPage->isVision=false;
207 break;
208 case 2:
209 camPage->isVision=false;
210 demarcatePage->isVision=false;
211 matrixPage->isVision=true;
212 break;
213 }
214 }
215 */
216
217 /*
218 * 点击托盘消息slot
219 */
220 void Vs2008Smart::hiMessage()
221 {
222 iTrayIcon->setIcon( QIcon("IconTray.png"));
223 }
224
225 /*
226 * 推出函数
227 */
228 void Vs2008Smart::exitProgram()
229 {
230 exit(-1);
231 }
232
233 /*
234 * 添加菜单项
235 */
236 void Vs2008Smart::addMenu()
237 {
238 diaplayAction = new QAction(tr("&配置参数"), this);
239 connect(diaplayAction, SIGNAL(triggered()), this, SLOT(displayVary()));
240 normalModeAction = new QAction(tr("&普通模式"), this);
241 //connect(normalModeAction, SIGNAL(triggered()), this, SLOT(show()));
242 restoreAction = new QAction(tr("&Restore"), this);
243 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
244 quitAction = new QAction(tr("&Quit"), this);
245 connect(quitAction, SIGNAL(triggered()), this, SLOT(exitProgram()));
246 iTraymenu=new QMenu();
247 iTraymenu->addAction(diaplayAction);
248 iTraymenu->addAction(normalModeAction);
249 iTraymenu->addAction(restoreAction);
250 iTraymenu->addSeparator();
251 iTraymenu->addAction(quitAction);
252 iTrayIcon->setContextMenu(iTraymenu);
253 }
254
255 /*
256 * 切换显示还是隐藏
257 */
258 void Vs2008Smart::displayVary()
259 {
260 //
261 if(this->isHidden())
262 {
263 this->setVisible(TRUE);
264 }
265 else
266 {
267 this->setVisible(FALSE);
268 }
269 }
270
2 #include "stdafx.h"
3 Vs2008Smart::Vs2008Smart(QWidget *parent, Qt::WFlags flags)
4 : QMainWindow(parent, flags)
5 {
6 ui.setupUi(this);
7
8 //this->setAttribute(Qt::WA_TranslucentBackground);//透明效果
9
10 //窗口居中
11 QDesktopWidget * deskTop = QApplication::desktop();
12 int screenIndex = deskTop->screenNumber ( this ); //得到this widget主要出于哪个屏幕中,返回屏幕index
13 QRect rect = deskTop->screenGeometry(screenIndex); //得到该屏幕大小
14 int x = (rect.width() - width())/2;
15 int y = (rect.height() - height())/2;
16 this->move(x,y);
17
18 //以可读写方式打开串口
19 /*
20 QFileSystemModel *model = new QFileSystemModel;
21 model->setRootPath(QDir::currentPath());
22 //QTreeView *tree = new QTreeView(splitter);
23 ui.treeView->setModel(model);
24 */
25
26 //添加listView Items
27 //ui.listWidget->insertItem(itemSize++,tr("通讯设置"));
28 //ui.listWidget->insertItem(itemSize++,QObject::tr("摄像头位置调整"));
29 //ui.listWidget->insertItem(itemSize++,tr("标定视频范围"));
30 itemSize=0;
31
32 //TODO:添加页面
33
34 //comPage=new com_Config();
35 camPage=new cam_Config();
36 demarcatePage=new demarcate_Config();
37 matrixPage=new matrix_Config();
38
39 /*
40 * 移除所有widgets
41 */
42 while(ui.stackedWidget->count()!=0)
43 ui.stackedWidget->removeWidget(ui.stackedWidget->widget(0));
44
45 //addPageItem(tr("通讯设置"),comPage);
46 addPageItem(tr("摄像机位置调整"),camPage);
47 addPageItem(tr("摄像机有效信息位置"),demarcatePage);
48 addPageItem(tr("摄像机内参数标定"),matrixPage);
49
50 ui.listWidget->setEnabled(false);
51
52 setPageItemIndex(0);
53 //ui.stackedWidget->addWidget(directPage);
54
55 //ui.btn_prepage->setEnabled(false);
56 setBtnEnable();
57 connect(ui.btn_prepage,SIGNAL(clicked()),this,SLOT(setPrePage()));//连接下一页
58 connect(ui.btn_nextpage,SIGNAL(clicked()),this,SLOT(setNextPage()));
59 connect(ui.listWidget,SIGNAL(currentRowChanged(int)),this,SLOT(setStackedWidget(int)));//连接左边listWidget
60 //connect(ui.stackedWidget,SIGNAL(currentChanged(int)),ui.listWidget,SLOT(setListWidget(int)));
61 connect(this,SIGNAL(updateListWidget(int)),this,SLOT(setListWidget(int)));
62
63 //connect(ui.btn_close,SIGNAL(clicked()),qApp,SLOT(quit()));
64
65 MoveMouseTimer=new QTimer(this);
66 connect(MoveMouseTimer,SIGNAL(timeout()),this,SLOT(moveMouse()));
67 connect(ui.btn_openV,SIGNAL(clicked()),this,SLOT(ControlmoveMouse()));
68 x=0;y=0;
69
70 /*
71 * 增加托盘的相关初始化代码
72 */
73 iTrayIcon=new QSystemTrayIcon();
74 iTrayIcon->setIcon( QIcon("IconTray.png"));
75 iTrayIcon->show();
76 iTrayIcon->setToolTip(QString(tr("One world, One dream!")));
77 iTrayIcon->showMessage("Message","Hi, I am here.");
78 connect( iTrayIcon, SIGNAL(messageClicked ()), this, SLOT(hiMessage()));
79 addMenu();
80 //end of the trayicon
81
82 }
83
84 void Vs2008Smart::moveMouse()
85 {
86 SystemInteraction::MoveMouseToXY(x,y);
87 x+=100;
88 //y++;
89 if(x>60000)
90 {
91 MoveMouseTimer->stop();
92 x=0;
93 }
94 }
95 /*
96 * 移动鼠标至x,y坐标处
97 */
98 void Vs2008Smart::ControlmoveMouse()
99 {
100 if(MoveMouseTimer->isActive())
101 {
102 MoveMouseTimer->stop();
103 }
104 else
105 {
106 MoveMouseTimer->start(50);//0.5s中断一次
107 }
108 }
109
110 Vs2008Smart::~Vs2008Smart()
111 {
112 //delete comPage;
113 delete camPage;
114 delete demarcatePage;
115 delete matrixPage;
116 qDebug( "Vs2008Smart is destruct!\n");
117 }
118
119 /*
120 * 设置上一页下一页的使能状态
121 */
122 void Vs2008Smart::setBtnEnable()
123 {
124
125 ui.Page_Oper->setText(QString("第")+QString::number(ui.stackedWidget->currentIndex()+1)+QString("步/共")+QString::number(ui.stackedWidget->count())+QString("步"));
126 if(ui.stackedWidget->count()==0)
127 return;
128 if(ui.stackedWidget->currentIndex()==0)
129 ui.btn_prepage->setEnabled(false);
130 else
131 ui.btn_prepage->setEnabled(true);
132
133 if(ui.stackedWidget->currentIndex()==ui.stackedWidget->count()-1)
134 {
135 ui.btn_nextpage->setText("完成");
136 //ui.btn_nextpage->setEnabled(false);
137 }
138 else
139 {
140 ui.btn_nextpage->setEnabled(true);
141 ui.btn_nextpage->setText(tr("下一页"));
142 }
143 }
144
145 void Vs2008Smart::setPrePage()
146 {
147 ui.stackedWidget->setCurrentIndex(ui.stackedWidget->currentIndex()-1);//这里直接用slot,不会再次发送singal,或许这样理解比较好
148
149 setBtnEnable();
150 emit updateListWidget(ui.stackedWidget->currentIndex());
151 }
152
153 void Vs2008Smart::setNextPage()
154 {
155 ui.stackedWidget->setCurrentIndex(ui.stackedWidget->currentIndex()+1);
156 setBtnEnable();
157 emit updateListWidget(ui.stackedWidget->currentIndex());
158 }
159
160 void Vs2008Smart::setListWidget(int curpage)
161 {
162 ui.listWidget->setCurrentRow(curpage);
163 //setVision(curpage);//设置vision
164 }
165
166 void Vs2008Smart::setStackedWidget(int page)
167 {
168 ui.stackedWidget->setCurrentIndex(page);
169 //setVision(page);//设置vision
170 setBtnEnable();
171 }
172
173 //////////////////////////////////////////////////////////////////////////
174 /*
175 * 添加一个页面,包括item和widget页面
176 */
177 void Vs2008Smart::addPageItem(QString itemName,QWidget* pageWidget)
178 {
179 //
180 ui.listWidget->insertItem(itemSize++,itemName);
181 ui.stackedWidget->addWidget(pageWidget);
182 }
183
184 void Vs2008Smart::setPageItemIndex(int Index)
185 {
186 ui.listWidget->setCurrentRow(Index);
187 ui.stackedWidget->setCurrentIndex(Index);
188 }
189
190 /*
191 * 设置每个页面是否是当前显示页面的函数
192 */
193 /*
194 void Vs2008Smart::setVision(int Index)
195 {
196 switch(Index)
197 {
198 case 0:
199 camPage->isVision=true;
200 demarcatePage->isVision=false;
201 matrixPage->isVision=false;
202 break;
203 case 1:
204 camPage->isVision=false;
205 demarcatePage->isVision=true;
206 matrixPage->isVision=false;
207 break;
208 case 2:
209 camPage->isVision=false;
210 demarcatePage->isVision=false;
211 matrixPage->isVision=true;
212 break;
213 }
214 }
215 */
216
217 /*
218 * 点击托盘消息slot
219 */
220 void Vs2008Smart::hiMessage()
221 {
222 iTrayIcon->setIcon( QIcon("IconTray.png"));
223 }
224
225 /*
226 * 推出函数
227 */
228 void Vs2008Smart::exitProgram()
229 {
230 exit(-1);
231 }
232
233 /*
234 * 添加菜单项
235 */
236 void Vs2008Smart::addMenu()
237 {
238 diaplayAction = new QAction(tr("&配置参数"), this);
239 connect(diaplayAction, SIGNAL(triggered()), this, SLOT(displayVary()));
240 normalModeAction = new QAction(tr("&普通模式"), this);
241 //connect(normalModeAction, SIGNAL(triggered()), this, SLOT(show()));
242 restoreAction = new QAction(tr("&Restore"), this);
243 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
244 quitAction = new QAction(tr("&Quit"), this);
245 connect(quitAction, SIGNAL(triggered()), this, SLOT(exitProgram()));
246 iTraymenu=new QMenu();
247 iTraymenu->addAction(diaplayAction);
248 iTraymenu->addAction(normalModeAction);
249 iTraymenu->addAction(restoreAction);
250 iTraymenu->addSeparator();
251 iTraymenu->addAction(quitAction);
252 iTrayIcon->setContextMenu(iTraymenu);
253 }
254
255 /*
256 * 切换显示还是隐藏
257 */
258 void Vs2008Smart::displayVary()
259 {
260 //
261 if(this->isHidden())
262 {
263 this->setVisible(TRUE);
264 }
265 else
266 {
267 this->setVisible(FALSE);
268 }
269 }
270
作者:W.M.steve
出处:http://www.cnblogs.com/weisteve/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/weisteve/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。