Fortran bind with C++/Qt : QTimer issue “QObject::startTimer: QTimer can only be used with threads started with QThread”










4














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 inherate 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)










share|improve this question























  • The reason this happens is propably becaus fortran creates it's own threads without initializing QApplication. Without a QApplication created on the main thread, Qt will not work properly!
    – Felix
    Nov 12 at 9:35






  • 2




    problem solved ! - I made DummyClass inherit from QThread (instead of QObject) - I call QTimer::start within the "run" method of DummyClass - I call QApplication in the constructor of DummyClass before running DummcClass::start() (not beautiful, I know...) Thank you :)
    – Daayus
    Nov 12 at 10:17
















4














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 inherate 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)










share|improve this question























  • The reason this happens is propably becaus fortran creates it's own threads without initializing QApplication. Without a QApplication created on the main thread, Qt will not work properly!
    – Felix
    Nov 12 at 9:35






  • 2




    problem solved ! - I made DummyClass inherit from QThread (instead of QObject) - I call QTimer::start within the "run" method of DummyClass - I call QApplication in the constructor of DummyClass before running DummcClass::start() (not beautiful, I know...) Thank you :)
    – Daayus
    Nov 12 at 10:17














4












4








4







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 inherate 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)










share|improve this question















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 inherate 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)







c++ qt fortran qtimer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 8:57









francescalus

16.9k73256




16.9k73256










asked Nov 12 at 8:41









Daayus

212




212











  • The reason this happens is propably becaus fortran creates it's own threads without initializing QApplication. Without a QApplication created on the main thread, Qt will not work properly!
    – Felix
    Nov 12 at 9:35






  • 2




    problem solved ! - I made DummyClass inherit from QThread (instead of QObject) - I call QTimer::start within the "run" method of DummyClass - I call QApplication in the constructor of DummyClass before running DummcClass::start() (not beautiful, I know...) Thank you :)
    – Daayus
    Nov 12 at 10:17

















  • The reason this happens is propably becaus fortran creates it's own threads without initializing QApplication. Without a QApplication created on the main thread, Qt will not work properly!
    – Felix
    Nov 12 at 9:35






  • 2




    problem solved ! - I made DummyClass inherit from QThread (instead of QObject) - I call QTimer::start within the "run" method of DummyClass - I call QApplication in the constructor of DummyClass before running DummcClass::start() (not beautiful, I know...) Thank you :)
    – Daayus
    Nov 12 at 10:17
















The reason this happens is propably becaus fortran creates it's own threads without initializing QApplication. Without a QApplication created on the main thread, Qt will not work properly!
– Felix
Nov 12 at 9:35




The reason this happens is propably becaus fortran creates it's own threads without initializing QApplication. Without a QApplication created on the main thread, Qt will not work properly!
– Felix
Nov 12 at 9:35




2




2




problem solved ! - I made DummyClass inherit from QThread (instead of QObject) - I call QTimer::start within the "run" method of DummyClass - I call QApplication in the constructor of DummyClass before running DummcClass::start() (not beautiful, I know...) Thank you :)
– Daayus
Nov 12 at 10:17





problem solved ! - I made DummyClass inherit from QThread (instead of QObject) - I call QTimer::start within the "run" method of DummyClass - I call QApplication in the constructor of DummyClass before running DummcClass::start() (not beautiful, I know...) Thank you :)
– Daayus
Nov 12 at 10:17


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258452%2ffortran-bind-with-c-qt-qtimer-issue-qobjectstarttimer-qtimer-can-only-be%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258452%2ffortran-bind-with-c-qt-qtimer-issue-qobjectstarttimer-qtimer-can-only-be%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto