Placing a single function in a thread?
-
Hello!
I have a function in my program that has an XQuery file retrieve data from an XML file and send it to the C++ program. Whenever this function is executed, it takes a ton of time. How can I place this function inside a different thread then the GUI part of the app? I am not interested in placing the entire class within a different thread, just this function. Once the function is finished, the thread will be destroyed. How do I do this?
Thanks!
-
Hi,
This page shows you all the threading options in Qt, and how to choose between them: http://qt-project.org/doc/qt-5/threads-technologies.html
See the 2nd "Example Use Case" at the bottom of the page.
-
Hello!
(first excuse me because of my English writing)
I`m newbie in Qt but I create a simple GUI application along with thread.
I hope this will be useful.
Download link:
http://rainbow7.persiangig.com/Qt/Threading.rar/download
(click the orange picture that is written Download)
http://rainbow7.persiangig.com/Qt/Threading.rar/downloadYa Ali.
-
Ok so I decided to use QtConcurrent. Here is my code snippet for placing initializeSideBar() into its own tab:
@
void initializeSideBar();
QFuture<void> future = QtConcurrent::run(initializeSideBar);
@This is essentially copied from the example "http://qt-project.org/doc/qt-4.8/qtconcurrentrun.html":here. It doesn't compile so obviously I'm doing something wrong.
heres the error Im getting:
:-1: error: symbol(s) not found for architecture x86_64
Thanks for your help!
-
The error is not related to your thread code. Your code syntax is correct. You need to solve this problem first: http://qt-project.org/forums/viewthread/39503/
However, if initializeSideBar() calls widget functions, then you will likely get a crash when you run your program. You cannot access GUI objects from a different thread.
-
That problem appears to be fixed. initializeSideBar() does call a slot that creates QToolButtons, but it doesn't directly access them, it just calls the slot to create them. Is there a special way to call slots in the GUI thread from my new thread?
-
[quote author="nicky j" date="1394909482"]Is there a special way to call slots in the GUI thread from my new thread?[/quote]Yes, just make sure that the slot owner lives in the GUI thread. See "QObject|Thread Affinity":http://qt-project.org/doc/qt-5/QObject.html#thread-affinity
-
I think your link is broken
-
Sorry, should be fixed now.
-
yep thats better.
So because I am calling slots from within a slot in a different thread, the slots I called will crash because they are interacting with widgets. How do I call them and have them execute in the GUI thread? -
See "Synchronizing Threads!High-Level Event Queues":http://qt-project.org/doc/qt-5/threads-synchronizing.html#high-level-event-queues
First, if your slot interacts with widgets, the object that "owns" the slot must live in the GUI thread.
Once your object is in the correct thread, you have 2 options:
Emit a signal that is connected to that slot, OR
Invoke the slot using QMetaObject::invokeMethod(), using a Qt::QueuedConnection