PDA

View Full Version : API in C++ with Qt4 GUI



pytro
15th December 2006, 13:00
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.

wysota
15th December 2006, 13:02
Yes. Qt is just another library, it doesn't enforce any real limitations to your application.

pytro
15th December 2006, 13:24
Thanks for the help. :)

pytro
27th December 2006, 17:45
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:
...

xpto->start();
...
while (!xpto->hasTerminated()) {
// update data.
// and update network peers with new data.
}
...

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.

wysota
27th December 2006, 20:57
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.

pytro
8th January 2007, 11:25
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.

wysota
8th January 2007, 11:36
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.

pytro
8th January 2007, 13:19
Big Thanks again works like a charm :cool:

pytro
23rd January 2007, 11:45
Hello again :),
I want acess updated data from xpto Class/Thread.



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtInterface w;
Test *xpto;
....
QTimer *timer = new QTimer(&a);
a.connect(timer, SIGNAL(timeout()), &a, SLOT(w.dostuff(xpto)));
timer->start(5000);
....
}

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.

pytro
24th January 2007, 11:49
Found the reason:

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

I think... :confused: