c++ - 出错时停止代码执行 (C++)

标签 c++ qt qwidget qtgui qmainwindow

我有一个用 C++ 编写的库。该库有一个函数,它接受命令作为字符串并执行它们。如果遇到错误(在命令中或在运行命令时),将调用“错误函数”进行一些清理并最终调用 exit(1)。我现在正在尝试为该库实现一个图形用户界面(使用 Qt)。问题是当遇到错误时,exit 被调用并且我的应用程序崩溃了。我可以访问库源代码,但我想将源代码修改到最少。

我正在考虑重写错误函数,使其停止执行代码并保持空闲状态,直到另一个命令从用户界面传递到库。问题是我不确定该怎么做。我基本上是在寻找一个相当于退出系统调用的函数调用(这样错误函数永远不会返回到产生错误的代码)除了我不希望应用程序退出而只是进入空闲状态并等待从用户界面调用。

如果有其他方法可以实现,请告诉我。如果您需要更多详细信息,也请告诉我。

提前致谢

这是一些代码,显示了我的问题所在

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void error_func(string error); 
void create_sphere(float radius); 
void create_rect(float length, float width); 

int main()
{
  string command; 
  while(1)  {
    cout << "Enter command: "; 
    cin >> command; 

    if(command.compare("create_sphere") == 0)  {
      float radius; 
      cout << "Enter radius: "; 
      cin >> radius;
      create_sphere(radius); 
    }
    else if(command.compare("create_rect") == 0)  {
      float l, w; 
      cout << "Enter length and width: "; 
      cin >> l >> w; 
      create_rect(l, w); 
    }
    else if(command.compare("quit") == 0)
      break; 
  } 
}

void create_sphere(float  radius)
{
  if(radius < 0)
    error_func(string("Radius must be positive")); 

  cout << "Created sphere" << endl; 
}

void create_rect(float length, float width)
{
  if(length < 0)
    error_func(string("Length must be positive")); 

  if(width < 0)
    error_func(string("Width must be positive")); 

  cout << "Created rectangle" << endl;
} 

void error_func(string error)
{
  // do some cleanup
  cout << "ERROR: " << error << endl; 
  exit(1); 
}

假设create_spherecreate_recterror_func 由库提供。我可以根据需要修改 error_func 但不能修改其他函数(因为有很多这样的函数)。

现在遇到错误时,我想回到main中的while循环,以便继续接受其他命令。

最佳答案

I am basically looking for a function call equivalent to exit system call (so that the error function never returns to the code which generated the error) except that I do not want the application to exit but instead just go to an idle state and wait for calls from the user interface.

基本上,您正在寻找一个事件循环。典型的最小Qt程序如下:

#include <QApplication>
#include <QMainWindow>

int main(int argc, char **argv)
{
    QApplication(argc, argv);
    QMainWindow w;
    w.show();
    return application.exec(); // What you want instead of exit
}

现在,您可以将 QMainWindow 替换为您自己的类,并在其中声明一个插槽,当您尝试处理来自用户界面的命令时将调用该插槽。

#include <QWidget>

...

class MyWidget : public QWidget
{
    Q_OBJECT
    public:
        explicit MyWidget(QWidget *parent) : QWidget(parent)
        {
            connect(sender, SIGNAL(mySignal()), SLOT(handleCommand()));
        }
    public slots:
        void handleCommand()
        {
            // Handle your command here.
            // Print the error code.
            qDebug() << error_func(string("Radius must be positive"));
            // or simply:
            qDebug() << "Radius must be positive";
        } // Leaving the scope, and getting back to the event loop
}

至于假库,好吧,如果它存在,它就会存在。如果不修复库,您对此无能为力。对于大多数图书馆来说,这是一种非常糟糕的行为。

修改不是退出,而是返回一个错误代码 - 这是 Qt 软件中的一般做法 - 如果他们愿意,则由应用程序决定何时退出。

在您的情况下,应用程序不会退出。同样,退出库函数是一个非常糟糕的主意。甚至 Qt 也不会在几百万 LOC 中做 1-2 次。

我建议不要抛出异常。它在 Qt 软件中通常不常见,您可以像 Qt 的其余部分一样为您使用错误代码来使您的软件保持一致。

关于c++ - 出错时停止代码执行 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23452755/

相关文章:

c++ - unsigned short 和 signed short 比较奇怪的行为

c++ - 在 iOS 上使用来自 brew 的 gsl/gsl 有问题吗?

c++ - 如何最小化调用列表构造函数(复制构造函数)的次数?

c++ - meta/cmd 键可以显示在快捷方式中吗?

qt - 如何销毁 QML 中的上下文指针?

qt - QML 控件重叠

c++ - Q3DBars 在垂直墙上有网格,怎么样?

c++ - 向自定义 QWidget 添加滚动条

c++ - 如何为 QWidget 设置用户数据?

c++ - QGraphicsItem 在场景中的位置始终为 NULL