Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

The inferior stopped because it received a signal from the operating system



  • Everytime I run debug mode it shows these two error and stop at Py_Initialize();
    image.JPG
    and error: An exception was triggered:Exception at 0x7ff92da3db9e, code: 0xc0000409: ,flags=0x0.

    here is my code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "qmessagebox.h"
    #include <math.h>
    #include <Python.h>
    #include <QDebug>
    #include <QApplication>
    #include <QWidget>
    #include <QLabel>
    #include <QFileDialog>
    #include <QString>
    
    MainWindow::MainWindow(QWidget *parent):
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    
    void MainWindow::on_pushButton_clicked()
    {
        Py_Initialize();
        if( !Py_IsInitialized() ){
            return;
        }
        PyRun_SimpleString("import sys");
        PyRun_SimpleString("sys.path.append('./')");
    
        PyObject* pModule = PyImport_ImportModule("QTpic_function");
        if (! pModule){
           qDebug()<<QObject::tr("Can't open python file\n");
        }
    
        PyObject* pHue = PyImport_ImportModule("hue");
        if (! pHue){
           qDebug()<<QObject::tr("Can't open python file\n");
        }
        QString pic_path = ui->P_path->text();
        QDir path;
    
        if(ui->rotate->isChecked()){
            QString save_path = pic_path + "rotate" + "\\";
            QString tLow = ui->low->text();
            QString tHigh = ui->high->text();
            int low = tLow.toInt();
            int high = tHigh.toInt();
            PyObject *args = PyTuple_New(4);
            PyTuple_SetItem(args,0,Py_BuildValue("s",pic_path.toStdString().c_str()));
            PyTuple_SetItem(args,1,Py_BuildValue("s",save_path.toStdString().c_str()));
            PyTuple_SetItem(args,2,Py_BuildValue("i",low));
            PyTuple_SetItem(args,3,Py_BuildValue("i",high));
            path.setCurrent(pic_path);
            path.mkdir("rotate");
    
            PyObject* pFunHandler = PyObject_GetAttrString(pModule,"rotate");
            if (!pFunHandler){
               qDebug()<<QObject::tr("Get function failed\n");
            }
            PyEval_CallObject(pFunHandler,args);
        }
    
        if(ui->bright->isChecked()){
            QString save_path = pic_path + "bright" + "\\";
            QString tbright = ui->brightness->text();
            QString tcontrast = ui->contrast->text();
            int bright = tbright.toInt();
            float contrast = tcontrast.toFloat();
            PyObject *args = PyTuple_New(4);
            PyTuple_SetItem(args,0,Py_BuildValue("s",pic_path.toStdString().c_str()));
            PyTuple_SetItem(args,1,Py_BuildValue("s",save_path.toStdString().c_str()));
            PyTuple_SetItem(args,2,Py_BuildValue("i",bright));
            PyTuple_SetItem(args,3,Py_BuildValue("f",contrast));
            path.setCurrent(pic_path);
            path.mkdir("bright");
    
            PyObject* pFunHandler = PyObject_GetAttrString(pModule,"bright");
            if (!pFunHandler){
               qDebug()<<QObject::tr("Get function failed\n");
            }
            PyEval_CallObject(pFunHandler,args);
        }
    
        if(ui->resize->isChecked()){
            QString save_path = pic_path + "resize" + "\\";
            QString twidth = ui->width->text();
            QString theight = ui->height->text();
            int width = twidth.toInt();
            int height = theight.toInt();
    
            PyObject *args = PyTuple_New(4);
            PyTuple_SetItem(args,0,Py_BuildValue("s",pic_path.toStdString().c_str()));
            PyTuple_SetItem(args,1,Py_BuildValue("s",save_path.toStdString().c_str()));
            PyTuple_SetItem(args,2,Py_BuildValue("i",width));
            PyTuple_SetItem(args,3,Py_BuildValue("i",height));
    
            path.setCurrent(pic_path);
            path.mkdir("resize");
    
            PyObject* pFunHandler = PyObject_GetAttrString(pModule,"resize");
            if (!pFunHandler){
               qDebug()<<QObject::tr("Get function failed\n");
            }
            PyEval_CallObject(pFunHandler,args);
        }
    
        if(ui->shift->isChecked()){
            QString save_path = pic_path + "shift" + "\\";
            QString tshift_right = ui->shift_right->text();
            QString tshift_down = ui->shift_down->text();
            int shift_right = tshift_right.toInt();
            int shift_down = tshift_down.toInt();
    
            PyObject *args = PyTuple_New(4);
            PyTuple_SetItem(args,0,Py_BuildValue("s",pic_path.toStdString().c_str()));
            PyTuple_SetItem(args,1,Py_BuildValue("s",save_path.toStdString().c_str()));
            PyTuple_SetItem(args,2,Py_BuildValue("i",shift_right));
            PyTuple_SetItem(args,3,Py_BuildValue("i",shift_down));
    
            path.setCurrent(pic_path);
            path.mkdir("shift");
    
            PyObject* pFunHandler = PyObject_GetAttrString(pModule,"shift");
            if (!pFunHandler){
               qDebug()<<QObject::tr("Get function failed\n");
            }
            PyEval_CallObject(pFunHandler,args);
        }
    
        if(ui->flip->isChecked()){
            QString save_path = pic_path + "flip" + "\\";
            QString tflipValue = ui->flipValue->text();
            int flipValue = tflipValue.toInt();
    
            PyObject *args = PyTuple_New(3);
            PyTuple_SetItem(args,0,Py_BuildValue("s",pic_path.toStdString().c_str()));
            PyTuple_SetItem(args,1,Py_BuildValue("s",save_path.toStdString().c_str()));
            PyTuple_SetItem(args,2,Py_BuildValue("i",flipValue));
    
            path.setCurrent(pic_path);
            path.mkdir("flip");
    
            PyObject* pFunHandler = PyObject_GetAttrString(pModule,"flip");
            if (!pFunHandler){
               qDebug()<<QObject::tr("Get function failed\n");
            }
            PyEval_CallObject(pFunHandler,args);
        }
    
        if(ui->hue->isChecked()){
            QString save_path = pic_path + "hue" + "\\";
    
            QString tpic_count = ui->pic_count->text();
            QString thue_low = ui->hue_low->text();
            QString thue_high = ui->hue_high->text();
    
            int pic_count = tpic_count.toInt();
            float hue_low = thue_low.toFloat();
            float hue_high = thue_high.toFloat();
    
            PyObject *args = PyTuple_New(5);
            PyTuple_SetItem(args,0,Py_BuildValue("s",pic_path.toStdString().c_str()));
            PyTuple_SetItem(args,1,Py_BuildValue("s",save_path.toStdString().c_str()));
            PyTuple_SetItem(args,2,Py_BuildValue("i",pic_count));
            PyTuple_SetItem(args,3,Py_BuildValue("f",hue_low));
            PyTuple_SetItem(args,4,Py_BuildValue("f",hue_high));
    
            path.setCurrent(pic_path);
            path.mkdir("hue");
    
            PyObject* pFunHandler = PyObject_GetAttrString(pHue,"hue");
            if (!pFunHandler){
               qDebug()<<QObject::tr("Get function failed\n");
            }
            PyEval_CallObject(pFunHandler,args);
        }
        Py_Finalize();
    
    }
    
    void MainWindow::on_browse_clicked()
    {
        QFileDialog myFileDialog (this);
    
        QString fileName = myFileDialog.getExistingDirectory (this, tr ("Select Folder"), QDir::currentPath ());
        fileName = QDir::toNativeSeparators((fileName));
    
        fileName += "\\";
        if(fileName != NULL) {
            ui->P_path->setText(fileName);
        }
    }
    
    void MainWindow::on_browseImg_clicked()
    {
    
        QFileDialog myFileDialog (this);
    
        QString filename = myFileDialog.getOpenFileName(this,"select picture","/home","image(*.jpg *.png)");
        filename = QDir::toNativeSeparators((filename));
    
        QPixmap pix(filename);
        QPixmap dest=pix.scaled(ui->Img->size(),Qt::KeepAspectRatio);
        ui->Img->setScaledContents(true);
        ui->Img->setPixmap(dest);
    }
    
    void MainWindow::on_browseResult_clicked()
    {
        QFileDialog myFileDialog (this);
    
        QString filename = myFileDialog.getOpenFileName(this,"select picture","/home","image(*.jpg *.png)");
        filename = QDir::toNativeSeparators((filename));
    
    
        QPixmap pix(filename);
        QPixmap dest=pix.scaled(ui->resultImg->size(),Qt::KeepAspectRatio);
        ui->resultImg->setPixmap(dest);
    }
    
    

    Every time I run the code it won't show any error,but when I click PushButton second time the program will stop and show these two error:
    The program has unexpectedly finished.
    The process was ended forcefully.
    Some people say it's because Py_Initialize(); and Py_Finalize(); only can run one time,so I but it in main.cpp,but the program stop when I click PushButton.



  • @terrysweqwe said in The inferior stopped because it received a signal from the operating system:

    when I click PushButton second time the program will stop and show these two error:
    The program has unexpectedly finished.
    The process was ended forcefully.
    Some people say it's because Py_Initialize(); and Py_Finalize(); only can run one time

    Are you sure, that finalize() is done, when clicking your button a second time? Because it will definitely crash, if some stuff is still running and you press that button a second time.

    Why do you use Qt / C++, when you pass your C++ controls to Python? Why don't you use PyQt directly, so you can create your GUI and everything else with Python code?
    In addition, you can use every additional Python module. So you don't have to worry about any handlers or script calls that might fail.


Log in to reply