QThread

Alles rund um die Programmierung mit Qt
Antworten
js
Beiträge: 59
Registriert: 4. Juli 2011 21:29

QThread

Beitrag 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
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: QThread

Beitrag von Christian81 »

1. Darf ein QThread nicht auf die GUI zugreifen und wenn es so wäre würde ich sagen - C++ Grundlagen
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
js
Beiträge: 59
Registriert: 4. Juli 2011 21:29

Re: QThread

Beitrag 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?
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: QThread

Beitrag von Christian81 »

Sorry - die Frage verstehe ich nicht. Ein Thread kann z.B. per Signal/Slot mit dem Hauptthread kommunizieren.
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
js
Beiträge: 59
Registriert: 4. Juli 2011 21:29

Re: QThread

Beitrag 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 )
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: QThread

Beitrag von Christian81 »

Und was ist da jetzt so verwunderlich dran?? Ich mein - worauf zeig b ? -> C++ Grundlagen!
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
js
Beiträge: 59
Registriert: 4. Juli 2011 21:29

Re: QThread

Beitrag 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))); 
js
Beiträge: 59
Registriert: 4. Juli 2011 21:29

Re: QThread

Beitrag 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); 
}
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: QThread

Beitrag von Christian81 »

Die Warnung sagt doch schon was Du zu tun hast...
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
Antworten