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

QtFileDialog stocks after until subprocess finished



  • Hi there,

    in my application i use

    QString fileName = QFileDialog::getOpenFileName(this,
                                                        tr("Open Input Image"),
                                                        desktopPath,
                                                        tr("Open file") + " (*.jpg *.jpeg *.png *.bmp *.tif *.tiff *.pdf)");
    
    
        if(fileName.isEmpty()) {
             QMessageBox::warning(this, tr("Error"), tr("No input file selcted"));        
             return;
        }
        
       QApplication::processEvents();
        processFile(fileName);
    
        doSomething();
    

    Inside the doSomething() function I use a system call (I tried this two):

    int err = system(cmd.toLatin1().constData());
    QProcess::execute(cmd);
    

    The "FileDialog get stuck - until the subprocess has finished.
    Is there any option or trick to close the "open file dialog" after selecting a file and "ok click" ?

    thx to all



  • @ademmler

    int err = system(cmd.toLatin1().constData());
    QProcess::execute(cmd);
    

    You are running your OS cmd twice! First, get rid of the system() call, and don't use that again!

    It blocks because you use QProcess::execute(). Assuming as you say (I don't know any different) that the file dialog does not disappear, you need to allow the code to return into the main Qt event loop for it to get removed. You have a couple of possibly approaches:

    • Change to using QProcess::start(), and use the finished etc. signals if you need to know when it finishes. This is probably the best idea.

    • If you want to stick with QProcess::exec() (or QProcess::waitForFinished()), either emit a signal or your own which you have connect()ed with Qt::QueuedConnection as the connection type, or use a QTimer() to put in a tiny delay, and have the slot on either of these do the QProcess::exec().



  • thx, this was not the real code. just the commands I was trying out ;-)
    Here is a better writing.

    Inside the doSomething() function I use a system call (I tried this two):
    
    this "int err = system(cmd.toLatin1().constData());"
    or  this "QProcess::execute(cmd);"
    


  • Ok - I have tried this out:

    Globally I defined: QProcess myProcess

    a) In the subroutine I use: QProcess::start()
    In this case the dialog is still blocked.

    b) If I use: QProcess::startDetached()
    Now the dialog closes as expected.

    In order do wait until this process ist finished I use:
    myProcess.waitForFinished(-1);

    I knew (-1) means - never time out. I use it right now for testing.

    In my case the main routine does not wait for the process to be finished.
    This of course causes more issues than ...

    Finally I tried this suggestion - and it did not work.
    Is this maybe an Mac OS X issue?

    How can I detect securely the state of myProcess ?



  • Her is a minimal example to illustrate the issue.
    http://files.lacunasolutions.com/various/QtMinimal.zip

    Please note this might not work on windows. You may need to change the system call in the subroutine to something else.


  • Lifetime Qt Champion

    @ademmler
    Hi
    Dont use myProcess.waitForFinished(-1); as it blocks the GUI.
    Use QProcess finished signal to wait to do rest of code until it sends signal.

    void MainWindow::on_openButton_clicked()
    {
        ui->statusbar->showMessage("Open file dialog.", 0);
    
        QString imagePath = QFileDialog::getOpenFileName(
                    this,
                    tr("Open File"),
                    QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
                    tr("TIFF (*.tif *tiff);;JPEG (*.jpg *.jpeg);;PNG (*.png)" )
                    );
    
        QApplication::processEvents();
    
        doSomething(imagePath);
    
        ui->statusbar->showMessage("Waiting for subprocess to end.", 0);
    
        connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [this,imagePath](){
            ui->statusbar->showMessage("Subprocess ended.", 0);
    
    
            imageObject = new QImage();
            imageObject->load(imagePath);
            image = QPixmap::fromImage(*imageObject);
    
            scene = new QGraphicsScene(this);
            pixmap = scene->addPixmap(image);
            scene->setSceneRect(image.rect());
            ui->graphicsView->setScene(scene);
    
            pixmap->setFlag(QGraphicsItem::ItemIsMovable);
        });
    
    
    }
    


  • @mrjj

    Perfect - that fixed the issue.

    How can I access stdout or error messages in this way?

    Would you tell me what do do, if I need to launch multiple sub processes and catch them separate?



  • @ademmler

    How can I access stdout or error messages in this way?

    Search the https://doc.qt.io/qt-5/qprocess.html docs page for stdout, stderr etc.

    Would you tell me what do do, if I need to launch multiple sub processes and catch them separate?

    Just create separate QProcess instances.



  • @JonB

    thx for telling me - I read this all day ... but still it did not work d for me ...



  • @ademmler
    Glancing at sample code https://gist.github.com/ynonp/8148340, this should work, and give you the idea of what is needed.



  • @JonB

    thx for pointing me to a working example.



  • @mrjj said in QtFileDialog stocks after until subprocess finished:

    );

    connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [this,imagePath](){
        ui->statusbar->showMessage("Subprocess ended.", 0
    

    I used this and it works fine so far - the blocked dialog gets free ...
    But I am wondering how I can pause the whole application until this thread is finished.

    Using process.waitForFinish() did not work here ...

    What I want to achieve is - a free dialog, a progress bar telling me
    that something is going on and at when all processes have end the image will be displayed and progress bar is stopped.


  • Lifetime Qt Champion

    @ademmler said in QtFileDialog stocks after until subprocess finished:

    But I am wondering how I can pause the whole application until this thread is finished.

    You dont pause a GUI app. That is simply wrong thinking.
    Most of Qt classes work with signal and slot.

    So when you do something, you simply hook up to a signal and then when the signal comes, you
    do what you need.

    So often you have to split the code in 2 parts.
    The setup part
    and the processing part where you react to the signal.

    You cannot pause an app. It will stop drawing and turns white etc.

    and don't use waitForFinish as it hangs the app. really not a good idea in GUI apps. ever.

    so just use signals and slots and the app will do nothing until some signal comes and then you
    do what you need in the slot.



  • @mrjj

    Ok - thank your for clearing this - hence I need to get into slots and signals concept ...


  • Lifetime Qt Champion

    @ademmler
    Yes its a must use with Qt :)
    At least when its a GUI app.



  • Hello guys,

    I have a conceptional question about Signals and slots which I could not find an answer.

    1. Lets say I launch 2 processes one directly after the other.

    2. Process A is much slower than process B.

    3. I need to wait for Process A (and his slot subtasks) to be finished before I execute the "slot" of process B.

    How can I achieve this?


  • Lifetime Qt Champion

    @ademmler
    Hi
    I assume you mean 2 x QProcess ?

    You would simply hook up to A and when it says finished you
    call the B slot.

    Or do you mean inside B, it needs to wait for A ?

    If you just mean inside the app, then you can just use A signal and then
    do what you need to do as B should be finished long time ago.



  • @ademmler said in QtFileDialog stocks after until subprocess finished:

    I need to wait for Process A (and his slot subtasks) to be finished before I execute the "slot" of process B.

    Maybe you mean you could/should not start process B until process A is finished? Saying you don't want to execute a slot connected to process B until after those of process A seems strange.



  • To go deeper into it.

    1. The reason is, that the time of the processes is always unknown and different. Hence I do not know how long each run of the app a process will take.

    2. The only rule is A has to follow B

    Isn't there any clear signal when a connect has taken place and when the related tasks are done?

    Can I emit another Signal with in a "connection" ?

    Regards Alexander



  • @ademmler said in QtFileDialog stocks after until subprocess finished:

    Isn't there any clear signal when a connect has taken place and when the related tasks are done?

    What "related tasks are done"? You have seen there is a QProcess::finished signal. If you want to only start B after A has finished, use that.


Log in to reply