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

pipe console output in a fortran func (inside a QThread) to Qt GUI ?



  • How to pipe console output in a fortran func(inside a QThread) to Qt GUI ?
    I am using a QThread to run a fortran function and would like to get the console output from the fortran func for display on my Qt widgets. How to do it?
    I know how to run a fortran program with QProcess and get fortran output inside my Qt code with QProcess::readyReadStandardOutput(). However, QThread does not have similar functions. It seems I can not run a fortran func with QProcess. Thanks for your help in advance.


  • Lifetime Qt Champion

    Hi,

    Can you show how you run your fortran code with QThread ?



  • I made a class FPreprocessor which inherits QObject like worker class.
    auto preprocessor = new FPreprocessor;
    preprcoessor->moveToThread( my_thread );
    connect....
    connect( this, &prerun, preprocessor, &FPreprocessor::run)
    my_thread->start();
    emit prerun();
    FPreprocessor has a func run() which is called when signal prerun is emitted.
    fortran function call is made inside FPreprocessor::run();


  • Qt Champions 2017

    A function is not a process, and a thread is not a process (mostly), so what you want is not possible. You can try to follow what's mentioned here, however, call me a cynic, but I really, really doubt the wisdom of it. You'd be better off actually using a C function you define be called in the fortran routine (or w/e it is) to bring you back whatever it is you need.



  • @kshegunov Thanks for your reply. That is what I thought too. I am trying to make a c function to be called from fortran code. This c function can forward fortran output to my GUI. It is a bit messy. I will let you know if it works. However, I still doubt it is impossible for the GUI to catch the fortran output directly.


  • Qt Champions 2017

    @JoeCFD said in pipe console output in a fortran func (inside a QThread) to Qt GUI ?:

    It is a bit messy. I will let you know if it works.

    Not more than calling a procedure in fortran instead of WRITE.

    However, I still doubt it is impossible for the GUI to catch the fortran output directly.

    This is not fortran output. The standard input and output are files that are prepared (and opened) for you by the runtime. Trying to hijack them can have unforeseen consequences on top of you being required to parse whatever it is written to them. How is this worse:

    extern "C" void my_callback_(double * theValueINeed)
    {
        double valueFromFortran = *theValueINeed;       // Save this value to your C/C++ code's part or w/e
    }
    
    subroutine ReportToC(valueToReport)
    	real(kind = 8), intent(in) :: valueToReport
    	call my_callback(valueToReport)
    end subroutine ReportToC
    

    I'd say it's much better to have:

    call ReportToC(234.1)
    

    instead of trying to stitch up some frail workaround.

    Don't get this the wrong way, I know where you're coming from. I've seen quite a lot of fortran code and most of it looks like it was written by half-braindead monkeys, so you still are going to need to find where you have to inject these callbacks. It's a massive undertaking by itself.



  • Hooked it up with a c callback func and a worker class. Able to send fortran output to my GUI for display now. Many Thanks


Log in to reply