c++ - 如何从任何地方完成一个程序?

标签 c++ qt qtgui qtimer qapplication

我的例子:

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 be exit
    }

在评论处,我尝试这样做:qApp->exit() 和 qApp->quit() 以及 this->close()(但“this”未显示,当然 close() 不起作用)。如何从任何代码位置完成应用程序?


完整代码:
main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.start();

    return a.exec();
}


主窗口.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


主窗口.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);
}

还有 ConnectionForm - 它只是一个带有一些 GUI 且没有任何额外代码的 QDialog。

最佳答案

看起来问题是您不允许调用 QApplication::quit()QApplication::exit() 直到 QApplication 事件循环实际上已经开始了。事件循环由 QApplication::exec() 启动,因此您调用 quit()/exit() 太早了产生影响。

您可以通过移动 quit()/exit() 调用来解决此问题,使其处于事件循环中(例如,通过移动您的 MainWindow: :start() 代码到 QMainWindow::showEvent() 槽),或者通过改变你的 MainWindow::start() 返回一个值,检查main 中的那个值,如果它是一个指示您的进程应该提前退出的值,则退出(不调用 QApplication::exec()`)。

关于c++ - 如何从任何地方完成一个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22902075/

相关文章:

c++ - 带隐藏和 WA_QuitOnClose 的 Qt QDialog

qt - 使用动画更改 QWidget 的背景颜色

c++ - 如何将使用 git 和 CMake 的外部库依赖项惯用地添加到 git 和 CMake 项目中?

c++ - 错误 : ‘method’ was not declared in this scope

c++ - Boost Spirit 如何将本地引用作为属性传递

python - 重写 QSpinBox.stepby 方法使 spinbox 仅增量

c++ - 如何使用 Qt 将两个 QComboBoxes 与 C++ 互连

c++ - 如何在没有持久索引损坏/重复的情况下使用 beginMoveRows 进行排序?

qt - 在PyQt中覆盖QPaintEvents

c++ - 如何在 C++/WinAPI 中通过网络适配器获取发送/接收的字节数