c++ - How to finish Qt programm from any place? -


my example:

main.cpp:     qapplication a(argc, argv);     mainwindow w;     w.start();     return a.exec(); 


w.start():     if (cf.exec()){         this->show();     } else {         qdebug()<<"need exit";         //here should exit     } 

at comment place, tried do: qapp->exit() , qapp->quit() , this->close() (but 'this' not shown , of cource close() not working). how can finish app place of code?


whole code:
main.cpp

#include "mainwindow.h" #include <qapplication>  int main(int argc, char *argv[]) {     qapplication a(argc, argv);     mainwindow w;     w.start();      return a.exec(); } 


mainwindow.h

#ifndef mainwindow_h #define mainwindow_h  #include <qmainwindow> #include "cadrform.h" #include "teacherform.h" #include "departmentform.h" #include "categoriesform.h" #include "postform.h" #include "ranksanddegreesform.h" #include "teachersrankanddegreeform.h" #include "connectionform.h"  namespace ui { class mainwindow; }  class mainwindow : public qmainwindow {     q_object  public:     explicit mainwindow(qwidget *parent = 0);     void start();     ~mainwindow(); signals:     void widgetchanged(qwidget*); private slots:     void on_actioncadr_triggered();     void resetmainlayout(qwidget* w);     void on_actionteacher_triggered();      void on_actiondepartment_triggered();      void on_actionpost_triggered();      void on_actioncategories_triggered();      void on_actionranksanddegrees_triggered();      void on_actionteachersrd_triggered();  private:     connectionform cf;     cadrform *cadrform;     teacherform *teacherform;     departmentform *departmentform;     categoriesform *categoriesform;     postform *postform;     ranksanddegreesform *ranksanddegreesform;     teachersrankanddegreeform *teachersrankanddegreeform;     qwidget *current;     ui::mainwindow *ui;      void addwidgettomainlayout(qwidget *w); };  #endif // mainwindow_h 


mainwindow.cpp

#include "mainwindow.h" #include "ui_mainwindow.h" #include <qdebug>  mainwindow::mainwindow(qwidget *parent) :     qmainwindow(parent),     ui(new ui::mainwindow) {     connect(this, signal(widgetchanged(qwidget*)), this, slot(resetmainlayout(qwidget*)));      ui->setupui(this);     cadrform = new cadrform(this);     teacherform = new teacherform(this);     departmentform = new departmentform(this);     categoriesform = new categoriesform(this);     postform = new postform(this);     ranksanddegreesform = new ranksanddegreesform(this);     teachersrankanddegreeform = new teachersrankanddegreeform(this);      addwidgettomainlayout(cadrform);     addwidgettomainlayout(teacherform);     addwidgettomainlayout(departmentform);     addwidgettomainlayout(categoriesform);     addwidgettomainlayout(postform);     addwidgettomainlayout(ranksanddegreesform);     addwidgettomainlayout(teachersrankanddegreeform); }  void mainwindow::start() {     if (cf.exec()){         this->show();     } else {         qdebug()<<"need exit";         qapp->quit();         qdebug()<<"still working";     } }  void mainwindow::addwidgettomainlayout(qwidget *w) {     ui->mainlayout->insertwidget(0, w);     ui->mainlayout->itemat(0)->widget()->hide(); }  mainwindow::~mainwindow() {     delete ui; }  void mainwindow::resetmainlayout(qwidget *w) {     int index;     if (current)     {         index = ui->mainlayout->indexof(current);         ui->mainlayout->itemat(index)->widget()->hide();     }     index = ui->mainlayout->indexof(w);     if (index != -1) ui->mainlayout->itemat(index)->widget()->show();     else {         addwidgettomainlayout(w);         resetmainlayout(w);     }     current = w;     setwindowtitle(current->windowtitle()); }  void mainwindow::on_actioncadr_triggered() {     emit widgetchanged(cadrform); }  void mainwindow::on_actionteacher_triggered() {     emit widgetchanged(teacherform); }  void mainwindow::on_actiondepartment_triggered() {     emit widgetchanged(departmentform); }  void mainwindow::on_actionpost_triggered() {     emit widgetchanged(postform); }  void mainwindow::on_actioncategories_triggered() {     emit widgetchanged(categoriesform); }  void mainwindow::on_actionranksanddegrees_triggered() {     emit widgetchanged(ranksanddegreesform); }  void mainwindow::on_actionteachersrd_triggered() {     emit widgetchanged(teachersrankanddegreeform); } 

and connectionform - it's qdialog gui , without additional code.

it looks problem you're not allowed call qapplication::quit() or qapplication::exit() until qapplication event loop has started. event loop gets started qapplication::exec(), you're calling quit()/exit() have effect.

you can fix either moving quit()/exit() call it's in event loop (e.g. moving mainwindow::start() code qmainwindow::showevent() slot), or changing mainwindow::start() return value, inspect value in main, , exit (without calling qapplication::exec()`) if it's value indicates process should exit early.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -