PDA

View Full Version : About QMessageBox close my tray application



Doles
19th July 2008, 12:20
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:



void QIP::about()
{
QMessageBox::about(this ,tr("About Application"),tr("<b> QIP v 0.2 </b><br/>"
"------------------<br/>"
"<b>Q</b>uick <b>I</b>p or <b>Q</b>t <b>I</b>p <br/>"
"2008 by Bartosz Dolewski"));
}


to this one:


void QIP::about()
{
QMessageBox::about(NULL ,tr("About Application"),tr("<b> QIP v 0.2 </b><br/>"
"------------------<br/>"
"<b>Q</b>uick <b>I</b>p or <b>Q</b>t <b>I</b>p <br/>"
"2008 by Bartosz Dolewski"));
}


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:



/************************************************** *************************
* Copyright (C) 2008 by Bartosz Dolewski
* E-mail: doles2@gmail.com
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
************************************************** *************************/
#include "qip.h"

QIP::QIP(QWidget *parent, Qt::WFlags f) : QWidget(parent,f)
{
ui.setupUi(this);

QTextCodec::setCodecForTr (QTextCodec::codecForName ("UTF-8"));
setWindowTitle("QIP");
qDebug()<<"Program has started...\n";

createNetworkList();
createTrayIcon();
createConnections();
//this->hide();
}

void QIP::createNetworkList()
{
//first of all we have to fill up our QList with interfaces which are found in this machine
qDebug()<<"Creating list of founded networks...\n";
networkList = QNetworkInterface::allInterfaces ();
foreach(QNetworkInterface NetInter, networkList)
{
if(NetInter.isValid())
{
ui.comboBox1->addItem(NetInter.name());
}
}
qDebug()<<"\t[+] NetworkList is done\n";
}

void QIP::closeEvent(QCloseEvent *event)
{
//this function is a reimpletementation of closeEvent from QWidget.
//We would like to not quit from program if the user will click the X button of our window :)
//Just ignore this event, when X button is clicked, close the window not whole application
if (trayIcon->isVisible())
{
hide();
event->ignore();
isHidden = true;
showHideAction->setText( tr("Show"));
}
}

void QIP::createTrayIcon()
{
qDebug()<<"Building tray icon....\n";
icon = QIcon (QString(":images/tray_icon.xpm"));
trayIcon = new QSystemTrayIcon;
contextMenu = new QMenu;
showHideAction = new QAction(tr("Show"), this);
aboutApplication = new QAction(tr("About Application"),this);
quitApplication = new QAction(tr("Quit Application"),this);
isHidden = false;
trayIcon->setIcon(icon);

contextMenu->addAction(showHideAction);
contextMenu->addSeparator();
contextMenu->addAction(aboutApplication);
contextMenu->addSeparator();
contextMenu->addAction(quitApplication);
contextMenu->setDefaultAction(showHideAction);
qDebug()<<"\t[+] Tray icon has built\n";


showHideAction->setText( tr("Hide")); //default action :) We have just started and the window is visible
trayIcon->setContextMenu(contextMenu);
trayIcon->show();
}

void QIP::createConnections()
{
qDebug()<<"Building connections...\n";
connect(ui.pushButton1, SIGNAL(clicked()), this, SLOT(showIP()));
connect(ui.pushButton2, SIGNAL(clicked()), this, SLOT(showMAC()));
connect(ui.pushButton3, SIGNAL(clicked()), this, SLOT(showBroadcast()));
connect(ui.pushButton4, SIGNAL(clicked()), this, SLOT(showNetmask()));
connect(ui.pushButton5, SIGNAL(clicked()), this, SLOT(refreshEverything()));
connect(ui.comboBox1, SIGNAL(activated(int)), this, SLOT(setInterface(int)));
//connections from TrayIcon
connect(showHideAction, SIGNAL(triggered()), this, SLOT(showHide()));
connect(quitApplication, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(aboutApplication, SIGNAL(triggered()), this, SLOT(about()));
//this line are also from TrayIcon :)
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason )), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationRe ason)));
qDebug()<<"\t[+] Connections have already created....\n";
}

void QIP::showIP()
{
qDebug()<<"showIP()...\n";
trayIcon->setToolTip("IP: " + ip);
if ( networkInterface.isValid() )
{
QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
ip = entry.at(0).ip().toString();
}
else
{
ip = "unknown";
}

if (ui.lineEdit1->text() != ip)
{
qDebug()<<"Setting IP adres to lineEdit1...\n";
ui.lineEdit1->setText(ip);
qDebug()<<"\t[+] Done...\n";
}
}
void QIP::showMAC()
{
qDebug()<<"showMAC()...\n";
if(networkInterface.isValid() )
{
mac = networkInterface.hardwareAddress();
}
else
{
mac = "unknown";
}

if (ui.lineEdit2->text() != mac)
{
qDebug()<<"Setting up hardware address...\n";
ui.lineEdit2->setText(mac);
qDebug()<<"\t[+] Done...\n";
}

}

void QIP::showBroadcast()
{
qDebug()<<"showBroadcast...\n";
if(networkInterface.isValid())
{
//broadcast = networkInterface.broadcast();
QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
broadcast = entry.at(0).broadcast().toString();
}
else
{
broadcast = "unknown";
}
if(ui.lineEdit3->text() != broadcast)
{
qDebug()<<"Setting up broadcast address...\n";
ui.lineEdit3->setText(broadcast);
qDebug()<<"\t[+] Done...\n";
}
}
void QIP::showNetmask()
{
qDebug()<<"showNetmask()...\n";
if(networkInterface.isValid())
{
//netmask = networkInterface.netmask();
QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
netmask = entry.at(0).netmask().toString();
}
else
{
netmask = "unknown";
}
if(ui.lineEdit4->text() != netmask)
{
qDebug()<<"Setting up netmask address...\n";
ui.lineEdit4->setText(netmask);
qDebug()<<"\t[+] Done...\n";
}
}

void QIP::refreshEverything()
{
qDebug()<<"Refreshing all fields...\n";
emit showIP();
emit showMAC();
emit showBroadcast();
emit showNetmask();
qDebug()<<"\t[+] Done...\n";
}

void QIP::showHide()
{
if ( isHidden )
{
this->show();
showHideAction->setText( tr("Hide"));
isHidden = false;
}

else
{
this->hide();
showHideAction->setText( tr("Show"));
isHidden = true;
}
}


void QIP::trayIconClicked( QSystemTrayIcon::ActivationReason reason )
{
if ( reason == QSystemTrayIcon::Trigger )
{
emit showHide();
}
}

void QIP::about()
{
QMessageBox::about(this ,tr("About Application"),tr("<b> QIP v 0.2 </b><br/>"
"------------------<br/>"
"<b>Q</b>uick <b>I</b>p or <b>Q</b>t <b>I</b>p <br/>"
"2008 by Bartosz Dolewski"));
}

void QIP::setInterface(int index)
{
qDebug()<<"Setting up new interface...\n";
networkInterface = networkList.at(index);
qDebug()<<"Network name: "<<networkInterface.name();
qDebug()<<"\t[+] Done...\n";
}


Thanks for any help,
Regards Bartek

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

caduel
19th July 2008, 18:39
have you unset QApplication::setQuitOnLastWindowClosed()?

Doles
19th July 2008, 21:27
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 :D