Unsolved Problem with QtConcurrent
-
Hi
I have a following problem: I'm trying to do a two threaded desktop (gui) application with using QtConcurrent class, such that, let's say, I would have two QTextEdit, two QPushButton, and in every QTextEdit fields would be realized an ordinary loop (e.g. for) in this way, when I would have clicked, let's say, the first button (linked to the first QTextEdit field) then the loop is realized, as mentioned in brackets, in first QTextEdit; then after e.g. 3 seconds I'm clicking the second button, and the second loop is starting in second field (QTextEdit) in a fully independent mode (so that I would be able to see, it's really multithread process). The both loops would show the incremental increased numbers with "append()" function, so that it would be easy to see how this two thread system is just functioning. -
@MrRobot1 said in Problem with QtConcurrent:
I'm trying to do a two threaded desktop (gui) application
I don't understand what you are saying in your question. But, if it is relevant, you must not access (or at minimum not update) Qt GUI objects in any thread other than the single, main, GUI one. You can send signals from other threads to the GUI thread asking it to update something on behalf of the other thread, but you must not directly update from any other thread.
-
Haven't I said it clearly? Sorry, maybe for my English, I'm not a native speaker. I meant I wanted just to do an application, which would be with two threads (on the basis of QtConcurrent). That's all. I've searched even all WEB, looking for some example codes, just something even similar to that what I want to do, and I have nothing found. Only general examples of QtConcurrent applying. For me it is not enough. Maybe I'm not so intelligent, but it does not help me. Could you write me some even schematic code how to do this? Let us assume we have the main class window, then I understood what you have written but not good enough to be able to do such an application.
-
@MrRobot1 I suggest you take a look at examples: https://doc.qt.io/qt-5/examples-threadandconcurrent.html
-
@jsulm Thanks for the link, but I've known these examples already earlier, and they are a bit too complicated as, however, for me. As I've written yet: maybe someone could just write a simple, general code for described problem. Again, I would want to make an application, that would be two threaded, i.e., precisely: an application with two loops, being 'activated' (those loops) by clicking two buttons, respectively. I really do not have completely any idea how to do it. I don't know if I am allowed to put here some codes from my attempts?
-
I seems like
QtConcurrent
is the wrong tool for the task.QtConcurrent
is meant for single tasks that execute and then finish. You should also note thatQtConcurrent
uses a thread pool. Depending on the number of cores you have something might not execute withQtConcurrent
immediately if other 'threads' (or rather 'tasks') are still using the pool.I would suggest using
QThread
for what you are trying to do. This has its own pitfalls but it is the better approach if you want to have an explicit thread (and not a task in a thread pool). Instead of a normal loop I suggest having a look into callingstart()
on theQThread
to start the event loop for that thread. Use aQTimer
with a timeout of 0s to successively call the same slot over and over again to simulate a loop (keep the counter somewhere else). In this way you can callquit()
on the thread from the outside to stop it. -
@MrRobot1
Based on how I understood your requirement, you should not use threading at all. Instead, use timers. -
@SimonSchroeder Thank you very much. What you said sheds some light on my problem. I thought a few times in this way, i.e., that perhaps I'm using just unsuitable tool (QtConrurrent) to my task. Now I realized that there must have been a problem, the more, especially, as you write "(...)something might not execute with QtConcurrent immediately if other 'threads' (or rather 'tasks') are still using the pool."
Well then, I will have to make me focused on QThread and QTimer classes. Again, I'm very grateful for your suggestions.PS: I don't know how to mark my problem is solved, here on forum (To be more precise: it is not yet -- in the meaning I've not made my application yet, but in the meaning that QtConcurrent is not suitable for my purpose). Can you, or someone else, tell me how to mark this?