Results 1 to 3 of 3

Thread: About QMessageBox close my tray application

  1. #1
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default About QMessageBox close my tray application

    Hi everyone,
    I have a trouble with QMessageBox::about(). My application works in tray icon. I created it using example source code from documentation. I have a QWidget, and when I click the "X" button my program still works in tray. And that`s cool. When this QWidget window is visible and I click the "About Application" item from TrayIcon menu my program doesn`t quit. BUT when the window is invisible (I mean just closed) and I click the about action item, whole program exits. It doesn`t matter if i use "X" button or PushButton. I was trying to change this invocation:

    Qt Code:
    1. void QIP::about()
    2. {
    3. QMessageBox::about(this ,tr("About Application"),tr("<b> QIP v 0.2 </b><br/>"
    4. "------------------<br/>"
    5. "<b>Q</b>uick <b>I</b>p or <b>Q</b>t <b>I</b>p <br/>"
    6. "2008 by Bartosz Dolewski"));
    7. }
    To copy to clipboard, switch view to plain text mode 
    to this one:
    Qt Code:
    1. void QIP::about()
    2. {
    3. QMessageBox::about(NULL ,tr("About Application"),tr("<b> QIP v 0.2 </b><br/>"
    4. "------------------<br/>"
    5. "<b>Q</b>uick <b>I</b>p or <b>Q</b>t <b>I</b>p <br/>"
    6. "2008 by Bartosz Dolewski"));
    7. }
    To copy to clipboard, switch view to plain text mode 
    It wasn`t change anything, maybe this about messagebox had not my icon from main window. Does anybody have some ideas ?
    There are source code of whole *.cpp file:

    Qt Code:
    1. /***************************************************************************
    2.  * Copyright (C) 2008 by Bartosz Dolewski
    3.  * E-mail: [email]doles2@gmail.com[/email]
    4.  * *
    5.  * This program is free software; you can redistribute it and/or modify *
    6.  * it under the terms of the GNU General Public License as published by *
    7.  * the Free Software Foundation; either version 2 of the License, or *
    8.  * (at your option) any later version. *
    9.  * *
    10.  * This program is distributed in the hope that it will be useful, *
    11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
    12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
    13.  * GNU General Public License for more details. *
    14.  * *
    15.  * You should have received a copy of the GNU General Public License *
    16.  * along with this program; if not, write to the *
    17.  * Free Software Foundation, Inc., *
    18.  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
    19.  ***************************************************************************/
    20. #include "qip.h"
    21.  
    22. QIP::QIP(QWidget *parent, Qt::WFlags f) : QWidget(parent,f)
    23. {
    24. ui.setupUi(this);
    25.  
    26. QTextCodec::setCodecForTr (QTextCodec::codecForName ("UTF-8"));
    27. setWindowTitle("QIP");
    28. qDebug()<<"Program has started...\n";
    29.  
    30. createNetworkList();
    31. createTrayIcon();
    32. createConnections();
    33. //this->hide();
    34. }
    35.  
    36. void QIP::createNetworkList()
    37. {
    38. //first of all we have to fill up our QList with interfaces which are found in this machine
    39. qDebug()<<"Creating list of founded networks...\n";
    40. networkList = QNetworkInterface::allInterfaces ();
    41. foreach(QNetworkInterface NetInter, networkList)
    42. {
    43. if(NetInter.isValid())
    44. {
    45. ui.comboBox1->addItem(NetInter.name());
    46. }
    47. }
    48. qDebug()<<"\t[+] NetworkList is done\n";
    49. }
    50.  
    51. void QIP::closeEvent(QCloseEvent *event)
    52. {
    53. //this function is a reimpletementation of closeEvent from QWidget.
    54. //We would like to not quit from program if the user will click the X button of our window :)
    55. //Just ignore this event, when X button is clicked, close the window not whole application
    56. if (trayIcon->isVisible())
    57. {
    58. hide();
    59. event->ignore();
    60. isHidden = true;
    61. showHideAction->setText( tr("Show"));
    62. }
    63. }
    64.  
    65. void QIP::createTrayIcon()
    66. {
    67. qDebug()<<"Building tray icon....\n";
    68. icon = QIcon (QString(":images/tray_icon.xpm"));
    69. trayIcon = new QSystemTrayIcon;
    70. contextMenu = new QMenu;
    71. showHideAction = new QAction(tr("Show"), this);
    72. aboutApplication = new QAction(tr("About Application"),this);
    73. quitApplication = new QAction(tr("Quit Application"),this);
    74. isHidden = false;
    75. trayIcon->setIcon(icon);
    76.  
    77. contextMenu->addAction(showHideAction);
    78. contextMenu->addSeparator();
    79. contextMenu->addAction(aboutApplication);
    80. contextMenu->addSeparator();
    81. contextMenu->addAction(quitApplication);
    82. contextMenu->setDefaultAction(showHideAction);
    83. qDebug()<<"\t[+] Tray icon has built\n";
    84.  
    85.  
    86. showHideAction->setText( tr("Hide")); //default action :) We have just started and the window is visible
    87. trayIcon->setContextMenu(contextMenu);
    88. trayIcon->show();
    89. }
    90.  
    91. void QIP::createConnections()
    92. {
    93. qDebug()<<"Building connections...\n";
    94. connect(ui.pushButton1, SIGNAL(clicked()), this, SLOT(showIP()));
    95. connect(ui.pushButton2, SIGNAL(clicked()), this, SLOT(showMAC()));
    96. connect(ui.pushButton3, SIGNAL(clicked()), this, SLOT(showBroadcast()));
    97. connect(ui.pushButton4, SIGNAL(clicked()), this, SLOT(showNetmask()));
    98. connect(ui.pushButton5, SIGNAL(clicked()), this, SLOT(refreshEverything()));
    99. connect(ui.comboBox1, SIGNAL(activated(int)), this, SLOT(setInterface(int)));
    100. //connections from TrayIcon
    101. connect(showHideAction, SIGNAL(triggered()), this, SLOT(showHide()));
    102. connect(quitApplication, SIGNAL(triggered()), qApp, SLOT(quit()));
    103. connect(aboutApplication, SIGNAL(triggered()), this, SLOT(about()));
    104. //this line are also from TrayIcon :)
    105. connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
    106. qDebug()<<"\t[+] Connections have already created....\n";
    107. }
    108.  
    109. void QIP::showIP()
    110. {
    111. qDebug()<<"showIP()...\n";
    112. trayIcon->setToolTip("IP: " + ip);
    113. if ( networkInterface.isValid() )
    114. {
    115. QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
    116. ip = entry.at(0).ip().toString();
    117. }
    118. else
    119. {
    120. ip = "unknown";
    121. }
    122.  
    123. if (ui.lineEdit1->text() != ip)
    124. {
    125. qDebug()<<"Setting IP adres to lineEdit1...\n";
    126. ui.lineEdit1->setText(ip);
    127. qDebug()<<"\t[+] Done...\n";
    128. }
    129. }
    130. void QIP::showMAC()
    131. {
    132. qDebug()<<"showMAC()...\n";
    133. if(networkInterface.isValid() )
    134. {
    135. mac = networkInterface.hardwareAddress();
    136. }
    137. else
    138. {
    139. mac = "unknown";
    140. }
    141.  
    142. if (ui.lineEdit2->text() != mac)
    143. {
    144. qDebug()<<"Setting up hardware address...\n";
    145. ui.lineEdit2->setText(mac);
    146. qDebug()<<"\t[+] Done...\n";
    147. }
    148.  
    149. }
    150.  
    151. void QIP::showBroadcast()
    152. {
    153. qDebug()<<"showBroadcast...\n";
    154. if(networkInterface.isValid())
    155. {
    156. //broadcast = networkInterface.broadcast();
    157. QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
    158. broadcast = entry.at(0).broadcast().toString();
    159. }
    160. else
    161. {
    162. broadcast = "unknown";
    163. }
    164. if(ui.lineEdit3->text() != broadcast)
    165. {
    166. qDebug()<<"Setting up broadcast address...\n";
    167. ui.lineEdit3->setText(broadcast);
    168. qDebug()<<"\t[+] Done...\n";
    169. }
    170. }
    171. void QIP::showNetmask()
    172. {
    173. qDebug()<<"showNetmask()...\n";
    174. if(networkInterface.isValid())
    175. {
    176. //netmask = networkInterface.netmask();
    177. QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
    178. netmask = entry.at(0).netmask().toString();
    179. }
    180. else
    181. {
    182. netmask = "unknown";
    183. }
    184. if(ui.lineEdit4->text() != netmask)
    185. {
    186. qDebug()<<"Setting up netmask address...\n";
    187. ui.lineEdit4->setText(netmask);
    188. qDebug()<<"\t[+] Done...\n";
    189. }
    190. }
    191.  
    192. void QIP::refreshEverything()
    193. {
    194. qDebug()<<"Refreshing all fields...\n";
    195. emit showIP();
    196. emit showMAC();
    197. emit showBroadcast();
    198. emit showNetmask();
    199. qDebug()<<"\t[+] Done...\n";
    200. }
    201.  
    202. void QIP::showHide()
    203. {
    204. if ( isHidden )
    205. {
    206. this->show();
    207. showHideAction->setText( tr("Hide"));
    208. isHidden = false;
    209. }
    210.  
    211. else
    212. {
    213. this->hide();
    214. showHideAction->setText( tr("Show"));
    215. isHidden = true;
    216. }
    217. }
    218.  
    219.  
    220. void QIP::trayIconClicked( QSystemTrayIcon::ActivationReason reason )
    221. {
    222. if ( reason == QSystemTrayIcon::Trigger )
    223. {
    224. emit showHide();
    225. }
    226. }
    227.  
    228. void QIP::about()
    229. {
    230. QMessageBox::about(this ,tr("About Application"),tr("<b> QIP v 0.2 </b><br/>"
    231. "------------------<br/>"
    232. "<b>Q</b>uick <b>I</b>p or <b>Q</b>t <b>I</b>p <br/>"
    233. "2008 by Bartosz Dolewski"));
    234. }
    235.  
    236. void QIP::setInterface(int index)
    237. {
    238. qDebug()<<"Setting up new interface...\n";
    239. networkInterface = networkList.at(index);
    240. qDebug()<<"Network name: "<<networkInterface.name();
    241. qDebug()<<"\t[+] Done...\n";
    242. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help,
    Regards Bartek

    PS: Sorry for my english (maybe "engrish" )

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: About QMessageBox close my tray application


  3. The following 3 users say thank you to caduel for this useful post:

    Doles (19th July 2008), Frostorator (15th August 2011), RenatoFerreira (4th September 2016)

  4. #3
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: About QMessageBox close my tray application

    No I haven`t unset it. Even more I didn`t know about this function at all I`ve added this code to the main.cpp file because there is included <QApplication> and my program works great. Thank you caduel

Similar Threads

  1. Replies: 11
    Last Post: 15th July 2008, 13:11
  2. Why when I close QDialog my main application gets killed?
    By alex chpenst in forum Qt Programming
    Replies: 2
    Last Post: 10th July 2008, 22:57
  3. Close Event for DialogBased Application.
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 12th December 2007, 12:33
  4. cleanup handler on application close
    By err23 in forum Qt Programming
    Replies: 3
    Last Post: 16th August 2006, 10:56
  5. How to close application?
    By beerkg in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2006, 19:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.