Bonjour,

J'ai un soucis avec QProgressBar, J'aimerais lancer une QProgressBar pendant que mon algorithme fonctionne (un chargement s'affiche), et j'aimerais le faire avec un thread (c'est ce qui me semple juste ). mais ça fonctionne pas,

bon voila les codes que j'ai utilisée :

Class_Thread.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef __CLASSE_THREAD_H__
#define __CLASSE_THREAD_H__
#include <QThread>
 
class MyThread : public QThread
{
Q_OBJECT
 
public:
	MyThread() ;
	void run();
	void Start();
	void Stop();
	void New ();
private:
 
};
 
#endif // __CLASSE_THREAD_H__
Classe_Mythread.cpp

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Classe_Thread.h"
#include "stdio.h"
#include <QThread>
#include <QtGui>
 
 
using namespace QtConcurrent;
 
const int iterations = 10;
 
void spin(int &iteration)
{
    const int work = 1000 * 1000 * 40;
    volatile int v = 0;
    for (int j = 0; j < work; ++j)
        ++v;
 
    //qDebug() << "iteration" << iteration << "in thread" << QThread::currentThreadId();
}
 
void MyThread::New ()
{
 
    // Prepare the vector.
    QVector<int> vector;
    for (int i = 0; i < iterations; ++i)
        vector.append(i);
 
    // Create a progress dialog.
    QProgressDialog dialog;
    dialog.setLabelText(QString("Progressing using %1 thread(s)...").arg(QThread::idealThreadCount()));
 
    // Create a QFutureWatcher and conncect signals and slots.
    QFutureWatcher<void> futureWatcher;
    QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset()));
    QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel()));
    QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int,int)), &dialog, SLOT(setRange(int,int)));
    QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog, SLOT(setValue(int)));
 
    // Start the computation.
    futureWatcher.setFuture(QtConcurrent::map(vector, spin));
 
    // Display the dialog and start the event loop.
    //dialog.exec();
    dialog.exec();
 
    futureWatcher.waitForFinished();
}
 
MyThread::MyThread() : QThread()
{
 
}
void MyThread::run()
    {
        New ();
    }
 
    void MyThread::Start()
    {
   	}
 
    void MyThread::Stop()
    {
        win -> close ();
    }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
void MainWindow::on_pushButton_clicked()
{
	MyThread* P = new MyThread();
	P->run ();
	printf ("Les deux thread fonctionne en parallel \n");
}
L'affichage de la chaine "Les deux ...." s'affiche apres la fin d'exéction du message de chargement.

Merci