Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Code:
QObject::connect(indexate,
SIGNAL(clicked
()),
&f,
SLOT (walk
(p
)));
It is simple solution, and the red wavy underline disappear even before compilation (is inactive again).
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
d_trans I do not understand do you think I am right in connect statement as I cannot to check as I need to manipulate Qtcreator to make build and debug available.
But i have similar situation yet about putting this connect statement in single thread:
Here is my solution but I have two issues: the first is how to define the run() of Qthread as a slot?
Here is my approach:
Code:
I create Thread.h for several thread headers: here is for the first one for indexate button and walk() method.
#ifndef ITHREAD_H
#define ITHREAD_H
#include <QWidget>
#include "Filewalker.h"
#include "Lister.h"
{
Q_OBJECT
public slots:
void run();
};
#endif
Then define run() in Lister.cpp without defining IThread constructor.
void IThread::run()
{
}
Maybe I should define run in new .cpp as walk() is also defined here.
Then in main () I initialize
Ithread variable, and start it.
But to put it in
connect I found just one wayout to assign ITread.
start() to
QObject instance by analogy to Java.
IThread A;
QObject::connect(indexate,
SIGNAL(clicked
()),
&f,
SLOT (pp
));
//It is underlined in red from the left of line that probably is not //correct as well as two previous lines. As Ithread::run() in another file despite the red line appera even I click by mouse any new line before any insertion in Lister.cpp
Again I cannot check as I have no time to correct Qtcreator(and everytime), but I see it just in this way, especially starting Ithread???
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Quote:
It is simple solution
AND STILL INCORRECT
The SIGNAL() and SLOT() macros each take as arguments the signature of the methods they will invoke, with the return type always being implicitly understood to be "void".
Examples of correct SIGNAL() or SLOT() syntax are:
Code:
SIGNAL( myMethod1() ) // a method that takes no parameters and returns void.
SIGNAL( myMethod2( int ) ) // a method that takes an int parameter type
SIGNAL( myMethod3
( int,
const QString & ) ) // a method that takes two parameters, one an int, the second a const QString reference
Examples of incorrect syntax are:
Code:
SIGNAL( myMethod2( myVariable ) ) // "myVariable" is the name of a variable, not a type
SIGNAL( myMethod3( 4, "some string" ) ) // "4" and "some string" are literals, not types
(Other examples of incorrect syntax include every "solution" you have posted).
Furthermore, you cannot connect a signal with fewer parameters to a slot with more parameters unless the slot's parameters have default values. You cannot connect a signal to a slot with different parameter types. You can connect a signal with more parameters to a slot with fewer parameters; the extra parameters from the signal will be ignored.
Code:
connect( myInstance1, SIGNAL( myMethod1() ), myInstance2, SLOT( myMethod2( int ) ) ) // error - no match for int slot parameter
connect( myInstance1, SIGNAL( myMethod1( int ) ), myInstance2, SLOT( myMethod2() ) ) // OK; int signal parameter is ignored
connect( myInstance1,
SIGNAL( myMethod1
( int,
const QString & ) ), myInstance2,
SLOT( myMethod2
( int,
double ) ) ) // error - parameter types don't match
Quote:
QObject::connect(indexate, SIGNAL(clicked()),&f,SLOT (pp));
Wrong - see example #1 of incorrect syntax above. "pp" is not a type, it is the name of a variable. And also wrong because the signal clicked() has no parameters. And wrong one more time because "pp" is not the signature of a slot defined in the class reference by the receiver "&f".
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Quote:
Originally Posted by
artt
Here is my solution but I have two issues: the first is how to define the run() of Qthread as a slot?
And why would you want run() as a slot?
You know that is the method that is executed by the worker thread, right? Just like in Java.
Quote:
Originally Posted by
artt
IThread A;
QObject pp=A.start();
That won't compile because QThrad::start() doesn't return anything.
You know, just like in Java.
It starts the thread which will then execute the run() method.
You know, just like in Java.
Cheers,
_
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Should I make start() as a slot? Or to be correctly: Should I made some thirdlevel Wrapper method for walk as slot? That is place Lister::walk() in run(), then A.start(), and them Someclass(Lister also f.e.)::walkwiththread() as slot, where I would put A.start()?// should I use event in my thread to correctly interact with Mainthread? Some kind of post...()?
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Quote:
Originally Posted by
artt
Should I make start() as a slot?
QThread::start() is a slot.
Quote:
Originally Posted by
artt
Or to be correctly: Should I made some thirdlevel Wrapper method for walk as slot? That is place Lister::walk() in run(), then A.start(), and them Someclass(Lister also f.e.)::walkwiththread() as slot, where I would put A.start()?// should I use event in my thread to correctly interact with Mainthread? Some kind of post...()?
I don't have a clue what you mean.
But if you meant creating a slot that will call walk with an argument, then yes.
Cheers,
_
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
I did not understand the first of your last statements? Is it some constatation(affirmation)? And I mean to make public slot: void Lister::walkwiththread() { A.start()}, when void IThread::run { Lister::walk(QString)}...
/"Yes, that's a workable direction" -- is it not too much? Maybe there would be some cycle -- lister method --ithreadmethod--starting
ithreadmethod--another lister method -- could it work? -- as I am really very tired of inactive Build-Debug menu
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Yes, that's a workable direction.
Cheers,
_
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
If I define IThread::run() in Lister.cpp with definition of Lister::walk(QString path0), lower in the same file I got such errors with 2 modifications - should I define these 2 methods in different files or in inverse order?
Code:
1. void IThread::run()
{
static void Lister
::walk(QString path0
);
}
ERRROR:invalid use of qualified-name 'Lister::walk'
2.
void IThread::run()
{
}
ERROR:expected primary-expression before ')' token
There is also run-time error with the argument of walk(pp) - so should I understand that I need just walk()?
QString p="C:\\C";
Object::connect: No such slot Lister::walk(pp) in ..\Filewalker2\main.cpp:32
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Quote:
Originally Posted by
artt
Code:
1. void IThread::run()
{
static void Lister
::walk(QString path0
);
}
ERRROR:invalid use of qualified-name 'Lister::walk'
2.
void IThread::run()
{
}
ERROR:expected primary-expression before ')' token
The first one doesn't make sense. You want to call a function, not declare one.
The second one is a function call, but you are passing a type as the argument instead of a value.
Since that would not work in Java either I have now serious doubts that you have even programmed a single line in Java as well.
Quote:
Originally Posted by
artt
There is also run-time error with the argument of walk(pp) - so should I understand that I need just walk()?
QString p="C:\\C";
Object::connect: No such slot Lister::walk(pp) in ..\Filewalker2\main.cpp:32
That has been already been answered several times.
Cheers,
_
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
1. You provided such radical thoughts: it is very big blame to write I do not write a line in a Java
despite it seems is thirdly light than Qt for such project. And of course there were attempts and errors.
After providing arguments:
QString path0="C:\\C";
for Lister::walk(path0) it works (and after putting A->start() on the heap).
But walkwiththread() works with small folders -- for example with 28 entities (shows the properties of 28 files) - but when with C:\Windows, it is inactive. And in debugger mode - when clicking on indexate() button for running walkwiththread()
I got the instant message: "Thread 1 of Group 1 finished" (in one of status bar of Qtcreator tabs), then clicking the second time "Thread 2 of Group 1 finished", etc. So maybe I need some another wrapper or additional method for launching
time-consuming thread - as 28 entities(or 19, or 5) renders instantly??
Anyway in both cases when seeing Debugger launched in console - I see at first the familiar message about absence of 22 libraries--but it display after the list of results with small folders..
And I put in .pro file: Qt+= gui core thread concurrent or ... threads concucrrent --despite I cannot found the reference about it in 2 my read books, but there is reference for "concurrent" in Blanchette despite it is another Qtconcurrent class.
So partially such scheme works..
And what about walk(pp) -- even in Auto-fill mode of Qtcreator I see SLOT(walk(QString)) - so the message in some kind the error - as such slot is present. Or should I to provide walk() without arguments or make clicked(pp) with arguments.
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Quote:
Originally Posted by
artt
1. You provided such radical thoughts: it is very big blame to write I do not write a line in a Java
Well, that was based on the failure to understand the difference between a type and a variable.
Because Java has exactly the same terminology for these concepts.
E.g.
Code:
class Main
{
static void main(String[] args)
{
String pp;
}
};
String is a type, pp is a variable of type String. pp is not a type.
In Java doing this would also not compile
Code:
void walk(String path)
{
}
void doWalk()
{
walk(String);
}
Again, because walk() expects an argument of type String, not the type itself.
Since half of the thread deals with you not understanding these simple facts for C++, I could only conclude that you don't understand them in Java either.
Quote:
Originally Posted by
artt
but when with C:\Windows, it is inactive.
What does that even mean "inactive"?
Does the method not execute depending on what you pass as its argument?
Quote:
Originally Posted by
artt
And I put in .pro file: Qt+= gui core thread concurrent
I am pretty sure "thread" is not a name for a Qt module.
"gui", "core" and "concurrent" are, the latter being needed for use of QtConcurrent, not for QThread.
QThread is part of QtCore.
Quote:
Originally Posted by
artt
or ... threads concucrrent --despite I cannot found the reference about it in 2 my read books, but there is reference for "concurrent" in Blanchette despite it is another Qtconcurrent class.
QtConcurrent is a namespace for functions that primarily deal with parallel algorithms, such as invoking the same function on all elements in a container, for multiple container entries concurrently.
Quote:
Originally Posted by
artt
And what about walk(pp) -- even in Auto-fill mode of Qtcreator I see SLOT(walk(QString))
So far your code indicated that pp was a variable of type QString, not a type called pp.
Do you have a class called "pp"?
Is the slot taking an argument of type "pp"?
Cheers,
_
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Of course, QString pp="C:\\C" means that pp is variable of Qstring. But I cannot put simply SLOT(walk(Qstring)), as the folder would not be defined so I put there concrete variable pp. Oh, in the course of writing these comment, I guessed maybe I need
to put Lister::walk(QString) as Lister::walk(pp), or even put pp("C:\\C") as a argument in declaration of slot public Lister { slot: walk(QString pp). } So it happened to be some euristics. In some way it should works.
Anyway - in thread environment this issue should be absent- but QThread do not work with processing big folder - taht is issue now. So I do undestand I need some tool for time-consuming for time-sonsuming thread.
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Quote:
Originally Posted by
artt
Of course, QString pp="C:\\C" means that pp is variable of Qstring.
Good that we could clarify that.
Quote:
Originally Posted by
artt
But I cannot put simply SLOT(walk(Qstring)), as the folder would not be defined so I put there concrete variable pp.
Explained several times already.
Quote:
Originally Posted by
artt
Anyway - in thread environment this issue should be absent- but QThread do not work with processing big folder - taht is issue now.
A thread is just a parallel execution context.
It doesn't matter to the thread how big a folder is.
Quote:
Originally Posted by
artt
So I do undestand I need some tool for time-consuming for time-sonsuming thread.
No idea what you mean.
Cheers,
_
1 Attachment(s)
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Attachment 11582
I'm beaming out of here.
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
I do not really understand you facepalm, and suspects of java code-- the only I copied from the net was exactly filewalker pattern (yet in qt).
Anyway eveything almost works
-- "is inactive" -- when I press indexate button and walkerwiththread() process C:\\Document and Settings (wriitten directly in Lister.cpp)-- i got nothing in console.
If it processes C:\\C (wriitten directly in Lister.cpp) it instantly render 28 results for other folder.
Maybe not everything is on heap, and yet in stack as it was at the beginning?
Anyway in both cases -- when Debug started...
I got such lines in console:
Could not load shared library symbols for 22 libraries, e.g. C:\WINDOWS\system32\ntdll.dll.
Use the "info sharedlibrary" command to see the complete listing.
So it happened when my Qfilelistinfo was on the stack.
Then small folders shows:
Code:
File:1656 catalog.php C:/includes/admin 4
File:1735 conf.php C:/includes/admin 4
File:1590 custord.php C:/includes/admin 4
File:4256 catalog_products_categories.php C:/includes/admin/sub 4
File:2754 catalog_special.php C:/includes/admin/sub 4
... (Big Folders nothing).
If I click again thread(it shows the thread2, and so on)
I got again
File:1656 catalog.php C:/includes/admin 4
File:1735 conf.php C:/includes/admin 4...
So this "Could not load shared library" appears at the beginning of run, so creation of Qwidget.
Really a very small need sto be done -- and it is really needs some SwingWorker java analogue as it renders instantly, but
in case of C:\Windows it could noty happened as well in the case of whole C:\, or all disks..
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Quote:
Originally Posted by
artt
-- "is inactive" -- when I press indexate button and walkerwiththread() process C:\\Document and Settings (wriitten directly in Lister.cpp)-- i got nothing in console.
If it processes C:\\C (wriitten directly in Lister.cpp) it instantly render 28 results for other folder.
And you have equal access rights to both folders?
Have you added addition logging to see what happens?
Quote:
Originally Posted by
artt
Maybe not everything is on heap, and yet in stack as it was at the beginning?
We've already determined that this doesn't matter.
Quote:
Originally Posted by
artt
Anyway in both cases -- when Debug started...
I got such lines in console:
Could not load shared library symbols for 22 libraries, e.g. C:\WINDOWS\system32\ntdll.dll.
Use the "info sharedlibrary" command to see the complete listing.
Obviously the debugger can't find the debug symbols for these libraries.
Not a problem until you have to debug into them.
Quote:
Originally Posted by
artt
Really a very small need sto be done -- and it is really needs some SwingWorker java analogue as it renders instantly
So it works when you run everything from the main thread?
Cheers,
_
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
No once I put:
Code:
void IThread::run()
{
Lister::walk(path0);
} -- and run it. Then change to:
void IThread::run()
{
Lister::walk(path0);
}
and run.
Everything not works, unfortunately...
Excerpts from Qt Assistant:
In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished.
Maybe I need some additional methods or tools to wait when worker thread made nessesary work, and then return in mainthread-- as I see that workerthread return immediately, it doesnt wait.
I read 2-3 days ago about linking worker thread to main(GUI)thread and as I remember there were mentioned as events as the some method post..() but it is not 100% postevents(), although maybe. Anyway there are also option Qtconcurrent(), despite i am not aware is it applicable for several threads (buttons) along mainthread, and qRunnable.
There is also Mandelbrot and the Blocking Fortune Client example in QtAssistant -despite it is too compicated for me as for now, and I do not need any blocking and mutex, just deblocking-that several buttons could be available in the same time-and main gui would not be non-freezing
Added after 22 minutes:
No once I put:
Code:
void IThread::run()
{
Lister::walk(path0);
} -- and run it. Then change to:
void IThread::run()
{
Lister::walk(path0);
}
and run.
Everything not works, unfortunately...
Excerpts from Qt Assistant:
In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished.
Maybe I need some additional methods or tools to wait when worker thread made nessesary work, and then return in mainthread-- as I see that workerthread return immediately, it doesnt wait.
I read 2-3 days ago about linking worker thread to main(GUI)thread and as I remember there were mentioned as events as the some method post..() but it is not 100% postevents(), although maybe. Anyway there are also option Qtconcurrent(), despite i am not aware is it applicable for several threads (buttons) along mainthread, and qRunnable.
There is also Mandelbrot and the Blocking Fortune Client example in QtAssistant -despite it is too compicated for me as for now, and I do not need any blocking and mutex, just deblocking-that several buttons could be available in the same time-and main gui would not be non-freezing
//Just now I put the folder to path0 variable with about rendered 1000 results, and 3-4 secs. of processing. So it is not instant.
But it actually do not reacts for such meta-directories as C:\Windows, C:\Documenst and Settings or simply the C:\ -- that is fact. You should understand that I change the name of folders directly in the code, and for testing purposes
Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)
Quote:
Originally Posted by
artt
Everything not works, unfortunately...
Let me repeat:
Does it work when you have no additional thread?
Quote:
Originally Posted by
artt
In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished.
Maybe I need some additional methods or tools to wait when worker thread made nessesary work, and then return in mainthread-- as I see that workerthread return immediately, it doesnt wait.
If you mean QThread::start() returns immediately, then yes of course, that's the whole point.
Quote:
Originally Posted by
artt
I read 2-3 days ago about linking worker thread to main(GUI)thread and as I remember there were mentioned as events as the some method post..() but it is not 100% postevents(), although maybe.
You can use events but using a signal is way easier.
If the thread doesn't have to report progress, then connect to the thread's finished() signal.
Anyway, make sure the program works without threads before making it more complex.
Cheers,
_