Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: how to close application

  1. #1
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default how to close application

    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
    Qt Code:
    1. QApplication a(argc, argv);
    2. Widget *w=new Widget();
    3. w->close();
    4. w->show();
    5. return a.exec();
    To copy to clipboard, switch view to plain text mode 
    but im unable to close the application from the widget.cpp like below
    Qt Code:
    1. void Widget::read_db_settings()
    2. {
    3. QFile file("ncssettingss.ini");
    4. if(file.open(QFile::ReadOnly))
    5. {
    6. this->close();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    ie if the file is not fount then close the application ...
    can any help me how to do this...
    thanks in advance...

  2. #2
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to close application

    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.
    Qt Code:
    1. app.setQuitOnLastWindowClosed(true);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to close application

    thanks gokul
    i ll check it now and tell u
    thankss...

  4. #4
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to close application

    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

  5. #5
    Join Date
    Oct 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to close application

    Quote Originally Posted by mohanakrishnan View Post
    but im unable to close the application from the widget.cpp like below
    Qt Code:
    1. if(file.open(QFile::ReadOnly))
    2. {
    3. this->close();
    4. }
    To copy to clipboard, switch view to plain text mode 
    ie if the file is not fount then close the application ...
    It should be:
    Qt Code:
    1. if(!file.open(QFile::ReadOnly)) this->close();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to close application

    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 ??

  7. #7
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to close application

    Are you sure this is being called.
    Qt Code:
    1. void Widget::read_db_settings();
    To copy to clipboard, switch view to plain text mode 
    Can you paste your code?

  8. #8
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to close application

    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



    Qt Code:
    1. main.cpp
    2. -----------------------
    3. #include <QtGui/QApplication>
    4. #include "widget.h"
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. Widget *w=new Widget();
    9. w->show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. widget.cpp
    2. ---------------
    3.  
    4. #include "widget.h"
    5. #include "ui_widget.h"
    6.  
    7. Widget::Widget(QWidget *parent)
    8. : QWidget(parent), ui(new Ui::WidgetClass)
    9. {
    10. ui->setupUi(this);
    11. qApp->setQuitOnLastWindowClosed(true);
    12. read_ncssettings();
    13. }
    14.  
    15. Widget::~Widget()
    16. {
    17. delete ui;
    18. }
    19. void Widget ::read_ncssettings()
    20. {
    21. QFile file("ncssettingss.ini");
    22. if(!file.open(QFile::ReadOnly))
    23. {
    24. QTextStream ts(&file);
    25. this->portno=ts.readLine(100);
    26. portno=portno.trimmed();
    27. this->baudrate=ts.readLine(100);
    28. baudrate=baudrate.trimmed();
    29. this->fmsname=ts.readLine(100);
    30. fmsname=fmsname.trimmed();
    31. qDebug() << baudrate;
    32. file.close();
    33. }
    34. else //if file is not found then give msg box and close the app
    35. {
    36. QMessageBox *mbox=new QMessageBox();
    37. mbox->setText("no file found");
    38. mbox->show();
    39. this->close();
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. widget.h
    2. -----------------
    3. #ifndef WIDGET_H
    4. #define WIDGET_H
    5. #include <QtGui/QWidget>
    6. #include <QFile>
    7. #include <QTextStream>
    8. #include <QMessageBox>
    9. #include <QDebug>
    10. namespace Ui
    11. {
    12. class WidgetClass;
    13. }
    14.  
    15. class Widget : public QWidget
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. Widget(QWidget *parent = 0);
    21. ~Widget();
    22. QString portno;
    23. QString baudrate;
    24. QString fmsname;
    25. void read_ncssettings();
    26. private:
    27. Ui::WidgetClass *ui;
    28. };
    29. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Oct 2009
    Posts
    22
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: how to close application

    Hi,if your mesagebox has worked currently,the close method must have done currently.
    because your code is currently!

  10. #10
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to close application

    no boss not working!! i have tried....
    wat to do ??? still im trying
    thsnks...

  11. #11
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to close application

    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.
    Qt Code:
    1. void Widget ::read_ncssettings()
    2. {
    3. QFile file("ncssettingss.ini");
    4. if(file.open(QFile::ReadOnly))
    5. {
    6. QMessageBox::information(this, "OK !", "file found, yeppie");
    7. QTextStream ts(&file);
    8. this->portno=ts.readLine(100);
    9. portno=portno.trimmed();
    10. this->baudrate=ts.readLine(100);
    11. baudrate=baudrate.trimmed();
    12. this->fmsname=ts.readLine(100);
    13. fmsname=fmsname.trimmed();
    14. qDebug() << baudrate;
    15. file.close();
    16. }
    17. else //if file is not found then give msg box and close the app
    18. {
    19. QMessageBox::critical(this, "Error !", "no file found");
    20. close();//No need to use this-> its obvious.;)
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to close application

    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

  13. #13
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to close application

    Quote Originally Posted by mohanakrishnan View Post
    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.
    Especially, please use comma and full stop at right places.

  14. #14
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to close application

    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

    Qt Code:
    1. void Widget ::read_ncssettings()
    2. {
    3. QFile file("ncssettingss.ini");
    4. if(file.open(QFile::ReadOnly))
    5. {
    6. QMessageBox::information(this, "OK !", "file found, yeppie");
    7. QTextStream ts(&file);
    8. this->portno=ts.readLine(100);
    9. portno=portno.trimmed();
    10. this->baudrate=ts.readLine(100);
    11. baudrate=baudrate.trimmed();
    12. this->fmsname=ts.readLine(100);
    13. fmsname=fmsname.trimmed();
    14. qDebug() << baudrate;
    15. file.close();
    16. }
    17. else //if file is not found then give msg box and close the app
    18. {
    19. QMessageBox::critical(this, "Error !", "no file found");
    20. close();//No need to use this-> its obvious.;)
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to close application

    In your main function please add this line:
    Qt Code:
    1. qApp->setQuitOnLastWindowClosed (true);
    To copy to clipboard, switch view to plain text mode 

    And change the function something like:
    Qt Code:
    1. void Widget ::read_ncssettings()
    2. {
    3. QFile file("ncssettingss.ini");
    4. if(file.open(QFile::ReadOnly))
    5. {
    6. QMessageBox::information(this, "OK !", "file found, yeppie");
    7. QTextStream ts(&file);
    8. this->portno=ts.readLine(100);
    9. portno=portno.trimmed();
    10. this->baudrate=ts.readLine(100);
    11. baudrate=baudrate.trimmed();
    12. this->fmsname=ts.readLine(100);
    13. fmsname=fmsname.trimmed();
    14. qDebug() << baudrate;
    15. file.close();
    16. }
    17. else //if file is not found then give msg box and close the app
    18. {
    19. //QMessageBox::critical(this, "Error !", "no file found");
    20. if(close())
    21. QMessageBox::critical(this, "YES", "Closed");
    22. else
    23. QMessageBox::critical(this, "NO", "Not Closed");
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    Then tell us the result.

  16. #16
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to close application

    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

  17. #17
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to close application

    Quote Originally Posted by mohanakrishnan View Post
    sorry for wasting ur time... if u want i ll attach the codes with zipped..
    thanks
    mohan
    yes, attach it.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  18. #18
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: how to close application

    hi
    pls find the attachemennt
    thanks
    mohan
    Attached Files Attached Files

  19. #19
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to close application

    you should close your widget after calling ctor.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  20. #20
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to close application

    Quote Originally Posted by mohanakrishnan View Post
    hi
    pls find the attachemennt
    thanks
    mohan
    I am not able to extract the contents
    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.

Similar Threads

  1. About QMessageBox close my tray application
    By Doles in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2008, 21:27
  2. Replies: 11
    Last Post: 15th July 2008, 13:11
  3. 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
  4. Close Event for DialogBased Application.
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 12th December 2007, 12:33
  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.