PDA

View Full Version : thread question



gt.beta2
12th March 2009, 19:29
Hi
I was reading one example about threads(wish i know almost nothing about) and got carried away by the imagination:)

I have one dialog:

ubmsr.h

#ifndef UBMSR_H
#define UBMSR_H

#include <QtGui/QDialog>

#include "senderThread.h"

namespace Ui
{
class UbmsrClass;
}

class Ubmsr : public QDialog
{
Q_OBJECT

public:
Ubmsr(QWidget *parent = 0);
~Ubmsr();
private slots:
void startOrStopSenderThread();
private:
Ui::UbmsrClass *ui;
senderThread *sender;
};

#endif // UBMSR_H

ubmsr.cpp

#include "ubmsr.h"
#include "ui_ubmsr.h"

Ubmsr::Ubmsr(QWidget *parent)
: QDialog(parent), ui(new Ui::UbmsrClass)
{
ui->setupUi(this);
sender = new senderThread;
sender->setMessage("Hello!");
connect(ui->senderButton, SIGNAL(clicked()), this, SLOT(startOrStopSenderThread()));
}

Ubmsr::~Ubmsr()
{
delete ui;
}

void Ubmsr::startOrStopSenderThread(){
ui->senderButton->setText("asdasd");
if (sender->isRunning()) {
sender->stop();
ui->senderButton->setText(tr("Start Sender"));
ui->senderMsg->setEnabled(true);
ui->senderPort->setEnabled(true);
}else{
sender->start();
ui->senderButton->setText(tr("Stop Sender"));
ui->senderMsg->setEnabled(false);
ui->senderPort->setEnabled(false);
}
}



senderThread.h

#ifndef SENDERTHREAD_H
#define SENDERTHREAD_H

#include <QThread>

class senderThread : public QThread
{
Q_OBJECT

public:
senderThread();
void setMessage(const QString &message);
void stop();

protected:
void run();

private:
volatile bool senderStopped;
QString messageStr;

};

#endif

senderThread.cpp

#include <QtCore>
#include <iostream>

#include "senderThread.h"

using namespace std;

senderThread::senderThread()
{
senderStopped = false;
}

void senderThread::setMessage(const QString &message)
{
messageStr = message;
}

void senderThread::run()
{
while (!senderStopped)
cerr << qPrintable(messageStr);
senderStopped = false;
cerr << endl;}

void senderThread::stop()
{
senderStopped = true;
}

I can start and stop the thread and change the button label.

Is it possible to access the objects in the ui from a member function in the thread class?!

The final idea is to have 2 diferent threads sending and reading from a local network and showin all in the dialog i created.

I already managed the network issue ... the problem is putting this all togheter :o

How can i do it?

Thanks

wysota
12th March 2009, 20:35
No, you can't access GUI objects from non-gui thread. You should do "worker" stuff in the "worker" thread and then send a signal or event to the main thread that it needs to render the data on screen.

Lykurg
12th March 2009, 20:40
Is it possible to access the objects in the ui from a member function in the thread class?!

Hi, this is not a very mature answer due to I am also not familiar with threads, but accessing a member is only possible by setting a pointer in the thread to the "main" class. But that's a bad idea, so leave it! But you can even use the signal and slot mechanism through the thread border. That should probably be the solution to your problem.

Lykurg

Edit: too late...

gt.beta2
13th March 2009, 18:56
I'm having trouble with the translation.
What does thread-safe or reentrant mean?
In short words if possible ;)
Thanks

opss ... found it http://doc.trolltech.com/4.5/threads.html#the-threading-classes

gt.beta2
14th March 2009, 23:54
Still about this subject... i've been reading about QtConcurrentRun.
Would it be possible for me to launch 2 member functions of the initial Ubmsr class in 2 different thread using this?
Imagine that f1() is broadcasting every 1 second and f2() is listening in other port!? This way i would have access to the GUI objects.

Does this make sense or showld i forget it?

wysota
15th March 2009, 01:34
In theory you could do that. But my personal opinion is that it's not worth the trouble. If you want to have multiple threads, then use a classical threaded approach and use signals and slots to communicate with the main thread that will manipulate the GUI. The real question is if you should use threads at all.