Results 1 to 15 of 15

Thread: A while(1) C function to run in Qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A while(1) C function to run in Qt

    C++ compilers don't care if you call "C" or "C++" functions in your programs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by wysota View Post
    C++ compilers don't care if you call "C" or "C++" functions in your programs.
    Oh wait, My "C" functions are in a .C file . So i am unable to do Worker *worker=new Worker at all .

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A while(1) C function to run in Qt

    Wrap your plain functions in a class and the problem is solved. It really is not complicated. Here is a complete, threaded program that prints powers of two:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QObject>
    3. #include <QThread>
    4. #include <QDebug>
    5.  
    6. // Plain function with C linkage declaration
    7. extern "C" {
    8. int multiplyByTwo(int i);
    9. }
    10.  
    11. // Plain function impl
    12. int multiplyByTwo(int i) {
    13. return i * 2;
    14. }
    15.  
    16.  
    17.  
    18. class Worker : public QObject {
    19. Q_OBJECT
    20.  
    21. public:
    22. Worker();
    23. ~Worker();
    24. public slots:
    25. void process();
    26. signals:
    27. void finished();
    28. private:
    29. // add your variables here
    30. };
    31.  
    32. Worker::Worker() { }
    33. Worker::~Worker() { }
    34.  
    35. // Start processing data.
    36. void Worker::process() {
    37. int value = 1;
    38. while (value < 1000000000) {
    39. qDebug() << value;
    40. value = multiplyByTwo(value);
    41. }
    42. emit finished();
    43. }
    44.  
    45.  
    46. int main(int argc, char **argv) {
    47. QCoreApplication app(argc, argv);
    48.  
    49. QThread* thread = new QThread;
    50. Worker* worker = new Worker;
    51. worker->moveToThread(thread);
    52. QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
    53. QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    54. QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    55. QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    56. thread->start();
    57.  
    58. QObject::connect(thread, SIGNAL(destroyed()), qApp, SLOT(quit()));
    59. return app.exec();
    60. }
    61. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    Thanks guys i have found another way to "Threading ", maybe wrongly executed .
    I did it by declaring the "C" function into a function in the cpp file .
    Then i used a Qtimer to connect the timer to the function and start it.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by 020394 View Post
    Thanks guys i have found another way to "Threading ", maybe wrongly executed .
    I did it by declaring the "C" function into a function in the cpp file .
    Then i used a Qtimer to connect the timer to the function and start it.
    There is no threading here, timers do not spawn new threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jul 2013
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: A while(1) C function to run in Qt

    Try to use the QtConcurrent::run or the QtConcurrent::map instead the QThread.

  7. #7
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    So when can I use the QTimer ? I am trying to use the QTimer to run a function along with the Gui. Is it the right way to ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A while(1) C function to run in Qt

    A timer is for running some code in a specific moment in time. It is handled by an event like (almost) everything else in Qt.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by wysota View Post
    A timer is for running some code in a specific moment in time. It is handled by an event like (almost) everything else in Qt.
    Isnt It the same as running threads ? For now i am doing a school project with different groups all doing different part . for me i am doing the Gui. I need to integrate the rest of the codes my friends do . EG (Algorithm , Checking of a keypad. etc). Am i able to use QTimer for them since i want them to run at a specific moment of time when i am at the Gui page

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by 020394 View Post
    Isnt It the same as running threads ?
    No. If you are to eat lunch at 12 o'clock, it doesn't mean at 12 some other instance of yourself is spawned and eats a sandwich but rather you have to stop what you had been doing, take out a sandwich, unwrap it, eat it and then attend your other tasks.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    anda_skoa (22nd July 2013)

  12. #11
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    I see so threading is always running in the background . But i been using timer to check the current time(Updatiing the time on the Label) , Isnt that considered as threadings?

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by 020394 View Post
    I see so threading is always running in the background . But i been using timer to check the current time(Updatiing the time on the Label) , Isnt that considered as threadings?
    No. Timers have nothing to do with threading.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. pass member function as argument int template function
    By bibhukalyana in forum General Programming
    Replies: 1
    Last Post: 12th March 2013, 07:05
  2. Replies: 11
    Last Post: 5th September 2012, 20:47
  3. Replies: 4
    Last Post: 2nd August 2012, 07:42
  4. Replies: 3
    Last Post: 25th May 2010, 09:46
  5. Replies: 0
    Last Post: 10th March 2010, 08:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.