I need to create a simple Fortran program to be bound with a complex Qt program.
The only problem I meet is the utilization of QTimer (which is mandatory): when I call a function using QTimer from Fortran, I always get this error message:
QObject::startTimer: QTimer can only be used with threads started with QThread.
All the other "complex" stuff (QUdpSocket, utilization of classes..) work properly.
How could I solve this problem? After the error message, I think I should find a trick to use QThread for Fortran but I have no idea how to do. (I also tried to inherit dummy from QThread instead of QObject : does not work either)
Here is a simple example. I give now the output and after, the source code.
output when the C++/Qt program is directly launched
begin
hello
end
hello
hello
... // one "hello" per second
output when the f90 program is launched
begin
QObject::startTimer: QTimer can only be used with threads started with QThread
end
main.cpp
#include <QApplication>
#include <QtGui>
#include "dummyclass.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DummyClass*ds = new DummyClass();
Q_UNUSED(ds);
return app.exec();
}
dummyclass.hpp
#include <QTimer>
#include <QObject>
#include <QApplication>
#include <QTextStream>
class dummyclass : public QObject // public QThread
{
Q_OBJECT
public:
DummyClass();
private:
QTimer timer;
private slots:
void hello();
}
dummyclass.cpp
#include "dummyclass.hpp"
DummyClass::DummyClass()
{
QTextStream(stdout) << "begin" << endl;
connect(&timer, SIGNAL(timeout()), this, SLOT(hello()));
timer.start(1000);
QTextStream(stdout) << "endl" << endl;
}
void DummyClass::hello()
{
QTextStream(stdout) << "Hello !" << endl;
}
dummymain.f90
program dummy_main
use dummy_module
use, intrinsic :: ISO_C_Binding, only: C_CHAR, C_NULL_CHAR
type(dummy_type) :: dummy
call newDummy(dummy)
end program dummy_main
dummy_module.f90
module dummy_module
use, intrinsic :: ISO_C_Binding, only: C_CHAR, C_NULL_CHAR
use, intrinsic :: ISO_C_Binding, only: C_ptr, C_NULL_ptr
implicit none
private
type dummy_type
private
type(C_ptr) :: object = C_NULL_ptr
end type dummy_type
!------------------------
! C function declarations
!------------------------
interface
function C_dummyClass__new_ () result(this) bind(C,name="dummyClass__new_")
import
type(C_ptr) :: this
end function C_dummyClass__new_
subroutine C_dummyClass__delete_ (this) bind(C,name="dummyClass__delete_")
import
type(C_ptr), value :: this
end subroutine C_dummyClass__delete_
end interface
interface newDummy
module procedure newDummy__new
end interface newDummy
interface deleteDummy
module procedure dummyClass__delete
end interface deleteDummy
public :: newDummy, deleteDummy
!------------------------------------------------------------------------------
CONTAINS
!------------------------------------------------------------------------------
!-------------------------------------------------
! Fortran wrapper routines to interface C wrappers
!-------------------------------------------------
subroutine dummyClass__new(this)
type(dummy_type), intent(out) :: this
this%object = C_dummyClass__new_()
end subroutine dummyClass__new
subroutine dummyClass__delete(this)
type(dummy_type), intent(inout) :: this
call C_dummyClass__delete_(this%object)
this%object = C_NULL_ptr
end subroutine dummyClass__delete
!------------------------------------------------------------------------------
end module dummy_module
compilation
g++ -fPIC -Wall -Wextra -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -lQtGui -lQtCore -lQtNetwork -lpthread -I/usr/include/QtGui -I/usr/include/Qt -I/usr/include/QtCore -I/usr/include/QtNetwork -c *.cpp
gfortran -c *.f90
gfortran -Wall -Wextra -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -lQtGui -lQtCore -lQtNetwork -lpthread -I/usr/include/QtGui -I/usr/include/Qt -I/usr/include/QtCore -I/usr/include/QtNetwork -o dummy *.o /home/me/build/dummy/src/moc_dummy_class_.cxx -lstdc++
(note: the moc file for dummy class was already generator by QtCreator)