Seite 1 von 1

QThread

Verfasst: 18. Juli 2011 02:10
von js
Abend,
wie ist es zu bewerkstelligen in einer Thread klasse auf die .ui file zuzugreifen?
//thread.h

Code: Alles auswählen

#ifndef THREAD_H
#define THREAD_H
#include <QThread>
#include "ui_tpopt.h"
#include <QtGui/QMainWindow>

class Thread : public QThread {
   Q_OBJECT
	   
public:
	Thread();
protected:
   void run();
private:	 
Ui::tpoptClass u;  
};
//thread.cpp

Code: Alles auswählen

#include "thread.h"
#include "tpopt.h"

#include <QTextStream>
#include <QFile> 

Thread::Thread() {
	
}
void Thread::run() {
ui. textEdit_5->setText(“test“);
}
//tpopt.h

Code: Alles auswählen

#ifndef TPOPT_H
#define TPOPT_H

#include <QtGui/QMainWindow>
#include "ui_tpopt.h"
#include "ui_projekt.h"
#include "projekt.h"
#include "thread.h"

class tpopt : public QMainWindow
{
Q_OBJECT

public:
	tpopt(QWidget *parent = 0, Qt::WFlags flags = 0);
	~tpopt();
	

private:
	Thread thread1;
	Ui::tpoptClass ui;
public slots:
};

#endif // TPOPT_H

Leider bricht das Programm mit der Fehlermeldung ab:
Unbehandelte Ausnahme bei 0x652f42da (QtGuid4.dll) in tpopt.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xccccccd0.
Class Q_GUI_EXPORT QTextEdit : public QAbstractScrollArea
{

Q_DECLARE_PRIVATE(QTextEdit)


Bin für jeden Tip sehr dankbar.
Danke
lg

Re: QThread

Verfasst: 18. Juli 2011 06:36
von Christian81
1. Darf ein QThread nicht auf die GUI zugreifen und wenn es so wäre würde ich sagen - C++ Grundlagen

Re: QThread

Verfasst: 18. Juli 2011 13:56
von js
Zitat:// Darf ein QThread nicht auf die GUI zugreifen //
Ok, wei0t du vielleicht ob ich mit QProcess auch interne asynchrone Funktionen starteten kann?

Re: QThread

Verfasst: 18. Juli 2011 19:37
von Christian81
Sorry - die Frage verstehe ich nicht. Ein Thread kann z.B. per Signal/Slot mit dem Hauptthread kommunizieren.

Re: QThread

Verfasst: 18. Juli 2011 20:27
von js
Zitat : // Sorry - die Frage verstehe ich nicht. //
Nun ja ich möchte das nach eine externe funktion gestartet ohne das, dass gesammte Programm einfriert.
Mit QProcess kann ich ein externes Programm starten.
start(proginame) leider aber keine intere Funktion wie ich es gerade mit den Threads versuche.

Zitat : //Ein Thread kann z.B. per Signal/Slot mit dem Hauptthread kommunizieren.//
es scheitert glaube ich an einer Kleinigkeit.

Edit::

das progi wird ohne Fehler gestartet, aber wenn run() gestartet wird bricht alles mit der Fehlermeldung ab:

Run-Time Check Failure #3- The variable 'b' is being used without being initialized

verweis auf connect(this, SIGNAL(lV(QString)), b, SLOT(setV(QString)));


thread.h

Code: Alles auswählen

#ifndef THREAD_H
#define THREAD_H
#include <QThread>
#include "ui_tpopt.h"
#include <QtGui/QMainWindow>
#include <QProcess>

class Thread : public QThread {
   Q_OBJECT
       
public:
Thread();
protected:
   void run();
signals:
     void lV(QString newValue);

};

#endif

void Thread::run() {

    tpopt *b;   
    connect(this, SIGNAL(lV(QString)), b, SLOT(setV(QString)));

}
tpopt.h

Code: Alles auswählen

#ifndef TPOPT_H
#define TPOPT_H

#include <QtGui/QMainWindow>
#include <QProcess>
#include "ui_tpopt.h"
#include "ui_projekt.h"
#include "projekt.h"
#include <thread.h>

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>


class tpopt : public QMainWindow
{
Q_OBJECT

public:
    tpopt(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~tpopt();
   

private:
    Thread thread1;
    Ui::tpoptClass ui;
public slots:
     void setV(QString value){
     ui.textEdit_6->setText(value);
     }

};


#endif // TPOPT_H
tpopt.h

Code: Alles auswählen

#include <QtGui>
#include "tpopt.h"

#include <QProcess>
#include <QInputDialog>
#include <QMessageBox>
#include <signal.h>
#include "thread.h"

#include <QTextStream>
#include <QFile>
… ( hat sich nichts geändert )

Re: QThread

Verfasst: 18. Juli 2011 20:44
von Christian81
Und was ist da jetzt so verwunderlich dran?? Ich mein - worauf zeig b ? -> C++ Grundlagen!

Re: QThread

Verfasst: 18. Juli 2011 22:38
von js
Ups sry, habs aber nun gebacken bekommen läuft.

Thread.h

Code: Alles auswählen

//thread.h
#ifndef THREAD_H
#define THREAD_H
#include <QThread>
#include "ui_tpopt.h"
#include <QtGui/QMainWindow>
#include <QProcess>

class Thread : public QThread {
   Q_OBJECT
	   
public:
	Thread(QWidget *parent = 0);
	~Thread();
protected:
   void run();
signals:
     void lV(QString newValue);
};
#endif
thread.h

Code: Alles auswählen

...
run(){

	lV("Nachricht vom thread");
}
tpopt

Code: Alles auswählen

#ifndef TPOPT_H
#define TPOPT_H

#include <QtGui/QMainWindow>
#include <QProcess>
#include "ui_tpopt.h"
#include "ui_projekt.h"
#include "projekt.h"
#include <thread.h>

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>


class tpopt : public QMainWindow
{
Q_OBJECT

public:
	tpopt(QWidget *parent = 0, Qt::WFlags flags = 0);
	~tpopt();
	

private:
	Thread thread1;
	Ui::tpoptClass ui;
     void setV(QString value){
	 ui.textEdit_6->setText(value);
     }
};


#endif // TPOPT_H
tpopt.cpp

Code: Alles auswählen

...
tpopt::tpopt(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
	connect(&thread1, SIGNAL(lV(QString)), this, SLOT(setV(QString))); 

Re: QThread

Verfasst: 20. Juli 2011 16:56
von js
Eine Kleinigkeit humpelt noch.
Im Mainthread habe ich kein Problem mit der processError aber im Thread tut sich nichts. Nur wenn ich alles abbreche kommt in der Eingabeaufforderung:
QObject::connect: Cannot queue arguments of type 'QProcess::ProcessError'
(Make sure 'QProcess::ProcessError' is registered using qRegisterMetaType().)

thread.h

Code: Alles auswählen

...
public slots:
void processError(QProcess::ProcessError);


thread.cpp

Code: Alles auswählen

Thread::Thread()
: QThread()
{
	asy = new QProcess();
	asy->moveToThread(this);
	connect(asy, SIGNAL(readyRead()), this, SLOT(readfrompp()));
	connect(asy, SIGNAL(error(QProcess::ProcessError) ),this , SLOT( processError(QProcess::ProcessError) ) );
}

Code: Alles auswählen

void Thread::processError(QProcess::ProcessError e) {
	QString err("Prozess Bericht:\n");
		err.append("Folgender Fehler ist aufgetreten: ");
	switch( e ) {
		case QProcess::FailedToStart:
			err.append("Failed to Start\n");
			break;
		case QProcess::Crashed:
			err.append("Crashed\n");
			break;
		case QProcess::Timedout:
			err.append("Timeout\n");
			break;
		case QProcess::WriteError:
			err.append("WriteError\n");
			break;
		case QProcess::ReadError:
			err.append("ReadError\n");
			break;
		case QProcess::UnknownError:
			err.append("UnknownError\n");
			break;
	}
	mess(err); 
}

Re: QThread

Verfasst: 20. Juli 2011 18:10
von Christian81
Die Warnung sagt doch schon was Du zu tun hast...