PDA

View Full Version : how to close application



mohanakrishnan
9th November 2009, 10:51
hi
i can close the application from the main function using widget object
like below; my application having 3 files main,widget.cpp,widget.h below is main.cpp


QApplication a(argc, argv);
Widget *w=new Widget();
w->close();
w->show();
return a.exec();

but im unable to close the application from the widget.cpp like below


void Widget::read_db_settings()
{
QFile file("ncssettingss.ini");
if(file.open(QFile::ReadOnly))
{
this->close();
}
}

ie if the file is not fount then close the application ...
can any help me how to do this...
thanks in advance...:o

yogeshgokul
9th November 2009, 11:05
In your case, you dont need to manually close the application, you just close the widget you are showing out there and your application will be closed automatically. For that you need to set this flag of your qApp object.

app.setQuitOnLastWindowClosed(true);

mohanakrishnan
9th November 2009, 11:10
thanks gokul
i ll check it now and tell u
thankss...

mohanakrishnan
9th November 2009, 11:34
hi gokul
i have tried it as u said ,but no use..i tried qapp destroy also but all in vain..
because this closing of the exe is essentially needed for me..can u guide me??
thanks
:confused:

Kronen
9th November 2009, 14:19
but im unable to close the application from the widget.cpp like below

if(file.open(QFile::ReadOnly))
{
this->close();
}

ie if the file is not fount then close the application ...

It should be:

if(!file.open(QFile::ReadOnly)) this->close();

mohanakrishnan
10th November 2009, 05:10
i tried it but the problem is this->close() does not work here .if the file is not found or found it is not working ??:confused:

yogeshgokul
10th November 2009, 05:14
Are you sure this is being called.

void Widget::read_db_settings();
Can you paste your code?

mohanakrishnan
10th November 2009, 05:39
ya sure gokul pls see..jus look all the code pls see the widget.cpp i tried to read an ini file where serial port setting will be present ill read it and open serial port my problem is if the ini file is not present then i need to show msgbox and close the exe ...
any help is highly respected
thanks in advance
mohan





main.cpp
-----------------------
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w=new Widget();
w->show();
return a.exec();
}






widget.cpp
---------------

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
: QWidget(parent), ui(new Ui::WidgetClass)
{
ui->setupUi(this);
qApp->setQuitOnLastWindowClosed(true);
read_ncssettings();
}

Widget::~Widget()
{
delete ui;
}
void Widget ::read_ncssettings()
{
QFile file("ncssettingss.ini");
if(!file.open(QFile::ReadOnly))
{
QTextStream ts(&file);
this->portno=ts.readLine(100);
portno=portno.trimmed();
this->baudrate=ts.readLine(100);
baudrate=baudrate.trimmed();
this->fmsname=ts.readLine(100);
fmsname=fmsname.trimmed();
qDebug() << baudrate;
file.close();
}
else //if file is not found then give msg box and close the app
{
QMessageBox *mbox=new QMessageBox();
mbox->setText("no file found");
mbox->show();
this->close();
}
}



widget.h
-----------------
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QDebug>
namespace Ui
{
class WidgetClass;
}

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = 0);
~Widget();
QString portno;
QString baudrate;
QString fmsname;
void read_ncssettings();
private:
Ui::WidgetClass *ui;
};
#endif // WIDGET_H

rainspider
10th November 2009, 05:55
Hi,if your mesagebox has worked currently,the close method must have done currently.
because your code is currently!

mohanakrishnan
10th November 2009, 06:17
no boss not working!! i have tried....
wat to do ??? still im trying
thsnks...
:confused:

yogeshgokul
10th November 2009, 06:20
First of all, you are checking the condition in reverse way.
It should be like. Please replace this function with code below and tell us the results.

void Widget ::read_ncssettings()
{
QFile file("ncssettingss.ini");
if(file.open(QFile::ReadOnly))
{
QMessageBox::information(this, "OK !", "file found, yeppie");
QTextStream ts(&file);
this->portno=ts.readLine(100);
portno=portno.trimmed();
this->baudrate=ts.readLine(100);
baudrate=baudrate.trimmed();
this->fmsname=ts.readLine(100);
fmsname=fmsname.trimmed();
qDebug() << baudrate;
file.close();
}
else //if file is not found then give msg box and close the app
{
QMessageBox::critical(this, "Error !", "no file found");
close();//No need to use this-> its obvious.;)
}
}

mohanakrishnan
10th November 2009, 06:43
hi gokul
i tried as u said now if the file is not found an error box comes then if i give ok to error box it closes ,then only the main widget form opens.but actually the widget box should not open am i right???
any further idea im also trying this another way!!if possible can u do this in ur pc
jus create on qt gui widget application just close the application using close () from widget.cpp constructor or using any function but not by any buttons
thanks...
mohan

yogeshgokul
10th November 2009, 07:02
hi gokul
i tried as u said now if the file is not found an error box comes then if i give ok to error box it closes ,then only the main widget form opens.but actually the widget box should not open am i right???
any further idea im also trying this another way!!if possible can u do this in ur pc
jus create on qt gui widget application just close the application using close () from widget.cpp constructor or using any function but not by any buttons
thanks...
mohan
Can you please use proper grammer, I am not able to understand your problem.
Notice your first sentance of this post, running like a train.:crying::crying::crying:
Especially, please use comma and full stop at right places.

mohanakrishnan
10th November 2009, 07:26
hi gokul
sorry for the continuous writing.. as you said i changed the code ,but it does not work
as expected.In my case the ncssettingss.ini file is not present so it goes to the else {}
condition ,after that it displays the messagebox .If i give ok ,then messagebox is closed after that the mainwindow is opened.but actually it should not open the window..
the application should be closed!!
thanks
mohan



void Widget ::read_ncssettings()
{
QFile file("ncssettingss.ini");
if(file.open(QFile::ReadOnly))
{
QMessageBox::information(this, "OK !", "file found, yeppie");
QTextStream ts(&file);
this->portno=ts.readLine(100);
portno=portno.trimmed();
this->baudrate=ts.readLine(100);
baudrate=baudrate.trimmed();
this->fmsname=ts.readLine(100);
fmsname=fmsname.trimmed();
qDebug() << baudrate;
file.close();
}
else //if file is not found then give msg box and close the app
{
QMessageBox::critical(this, "Error !", "no file found");
close();//No need to use this-> its obvious.;)
}
}

yogeshgokul
10th November 2009, 08:51
In your main function please add this line:


qApp->setQuitOnLastWindowClosed (true);

And change the function something like:


void Widget ::read_ncssettings()
{
QFile file("ncssettingss.ini");
if(file.open(QFile::ReadOnly))
{
QMessageBox::information(this, "OK !", "file found, yeppie");
QTextStream ts(&file);
this->portno=ts.readLine(100);
portno=portno.trimmed();
this->baudrate=ts.readLine(100);
baudrate=baudrate.trimmed();
this->fmsname=ts.readLine(100);
fmsname=fmsname.trimmed();
qDebug() << baudrate;
file.close();
}
else //if file is not found then give msg box and close the app
{
//QMessageBox::critical(this, "Error !", "no file found");
if(close())
QMessageBox::critical(this, "YES", "Closed");
else
QMessageBox::critical(this, "NO", "Not Closed");
}
}


Then tell us the result.

mohanakrishnan
10th November 2009, 09:28
hi gokul
i have made the changes as u said.If i run ,it showed "closed " in messagebox .If i give ok then the main widget form opens.It means,we close the form which is not created that time!!!
ie.,befor the widget is opened we try to close !!!! am i right??
sorry for wasting ur time... if u want i ll attach the codes with zipped..
thanks
mohan

spirit
10th November 2009, 09:29
sorry for wasting ur time... if u want i ll attach the codes with zipped..
thanks
mohan
yes, attach it.

mohanakrishnan
10th November 2009, 09:52
hi
pls find the attachemennt
thanks
mohan

spirit
10th November 2009, 10:11
you should close your widget after calling ctor.

yogeshgokul
10th November 2009, 10:14
hi
pls find the attachemennt
thanks
mohan
I am not able to extract the contents :crying::crying:
I am on SUSE
This is my untarring log

[/home/csc/_work/ys/source.zip]
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
zipinfo: cannot find zipfile directory in one of /home/csc/_work/ys/source.zip or
/home/csc/_work/ys/source.zip.zip, and cannot find /home/csc/_work/ys/source.zip.ZIP, period.

spirit
10th November 2009, 10:19
another way it is to do like this


void Widget ::read_ncssettings()
{
QFile file("ncssettingss.ini");
if(file.open(QFile::ReadOnly))
{
QMessageBox::information(this, "OK !", "file found, yeppie");
QTextStream ts(&file);
this->portno=ts.readLine(100);
portno=portno.trimmed();
this->baudrate=ts.readLine(100);
baudrate=baudrate.trimmed();
this->fmsname=ts.readLine(100);
fmsname=fmsname.trimmed();
qDebug() << baudrate;
file.close();
}
else //if file is not found then give msg box and close the app
{
QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);//important line
}

}

btw, in this case you don't need to use setQuitOnLastWindowClosed at all.
so, the main should look like.


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

mohanakrishnan
10th November 2009, 10:20
hi
sorry i am using windows xp and winrar now i have attached them as seperate files
pls look at them sorry for the inconvenience.
thanks
mohan

mohanakrishnan
10th November 2009, 10:30
hi yogesh/spirit
thanks a lot at last it worked as per ur idea i included the
below line
QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);//important line

yes it clicked
thanks a lot... for ur helps
:D:D:D

mohanakrishnan
10th November 2009, 10:33
hi spirit
thanks for ur great help
as u said i modified
QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);//important line

it works fine now ,
thanks.....