Results 1 to 10 of 10

Thread: API in C++ with Qt4 GUI

  1. #1
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Question API in C++ with Qt4 GUI

    Hi all, i'm needing some big insights about an issue. Is it possible, to pass parameters on a C++ API to an Qt4 GUI project.
    Basically is it possible to include classes from an API built in C++ VS2005 Project to a Qt4 GUI Project.

    Regards,

    Joseph.

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

    Default Re: API in C++ with Qt4 GUI

    Yes. Qt is just another library, it doesn't enforce any real limitations to your application.

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

    pytro (15th December 2006)

  4. #3
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: API in C++ with Qt4 GUI

    Thanks for the help.

  5. #4
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: API in C++ with Qt4 GUI

    I'm working with a Network Enviroment API and there are many types of Threads.
    In my console application, at "main".cpp i have this code:
    ...
    Qt Code:
    1. xpto->start();
    2. ...
    3. while (!xpto->hasTerminated()) {
    4. // update data.
    5. // and update network peers with new data.
    6. }
    7. ...
    To copy to clipboard, switch view to plain text mode 
    xpto is an object referenced to a class, that is derived from Thread.


    The question is:
    Is it possible to implement non-Qt API Threads, inside Qt-Gui main.cpp?
    I want to update Qt-GUI widgets (i.e tableWidget items) with new data, every 20 sec.

    I have looked for an answer and found this:
    "Because of limitations inherited from the low-level libraries on which Qt's GUI support is built, QWidget and its subclasses are not reentrant. One consequence of this is that we cannot directly call functions on a widget from a secondary thread. If we want to, say, change the text of a QLabel from a secondary thread, we can emit a signal connected to QLabel::setText() or call QMetaObject::invokeMethod() from that thread."

    What is the best way to do this?

    Regards,

    Joseph.

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

    Default Re: API in C++ with Qt4 GUI

    You can always post events to objects living in the gui thread as QCoreApplication::postEvent() is thread-safe. Then you can implement a handler that updates the gui from within the gui thread.

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

    pytro (27th December 2006)

  8. #6
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: API in C++ with Qt4 GUI

    Hi again,
    PostEvent is working perfectly with CustomEvent.
    The problem is the while cycle that freezes Qt app.
    What is the best way to solve this? QThread?

    Thanks.

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

    Default Re: API in C++ with Qt4 GUI

    Quote Originally Posted by pytro View Post
    What is the best way to solve this? QThread?
    No. Timers with 0ms timeout and implementing the functionality in a slot. Alternatively you might call qApp->processEvents() in that loop so that events are processed even if the loop blocks the flow.

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

    pytro (8th January 2007)

  11. #8
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: API in C++ with Qt4 GUI

    Big Thanks again works like a charm

  12. #9
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: API in C++ with Qt4 GUI

    Hello again ,
    I want acess updated data from xpto Class/Thread.


    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. QtInterface w;
    5. Test *xpto;
    6. ....
    7. QTimer *timer = new QTimer(&a);
    8. a.connect(timer, SIGNAL(timeout()), &a, SLOT(w.dostuff(xpto)));
    9. timer->start(5000);
    10. ....
    11. }
    To copy to clipboard, switch view to plain text mode 
    Is this supose to call the SLOT in Qt.cpp every 5 sec., passing updated xpto?
    Because i tested with timers in Qt.cpp and is working perfectly.
    In Main.cpp isn't working.

    Do i have to use a timer in Main.cpp and use another timer in Qt.cpp to update QTableWidgetItem?
    How should i config both timers? (if needed)

    Thanks.
    Last edited by pytro; 23rd January 2007 at 11:34.

  13. #10
    Join Date
    Dec 2006
    Posts
    19
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: API in C++ with Qt4 GUI

    Found the reason:

    To start an event loop from a non-GUI thread, use QThread::exec()..

    I think...

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.