PDA

View Full Version : How to access TextEditBox of one class in another class in another file



sudhir_reddy83
28th March 2013, 21:58
Hii Friends.

I am developing a virtualization based GUI.

In this application I am building a serial port GUI app.

In my application I am having to .cpp files namely serial_port.cpp and CVmThread.cpp
My serial_port.cpp is having a ui form having following files.
serial_port.cpp
serial_port.h
serial_port.ui

But CVmThread.cpp is a non-GUI form having following files.
CVmTread.cpp
CVmThread.h

The code which I am using is given below
serial_port.h


#ifndef SERIAL_PORT_H
#define SERIAL_PORT_H

#include <QWidget>
#include <QTextEdit>


namespace Ui {
class Serial_Port;
}

class Serial_Port : public QWidget
{
Q_OBJECT


public:
explicit Serial_Port(QWidget *parent = 0);
~Serial_Port();

QTextEdit* RxtextEdit;

private:
Ui::Serial_Port *ui;
};

#endif // SERIAL_PORT_H


serial_port.cpp


#include "serial_port.h"
#include "ui_serial_port.h"

Serial_Port::Serial_Port(QWidget *parent) :
QWidget(parent),
ui(new Ui::Serial_Port)
{
ui->setupUi(this);
}

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


CVmThread.h


#ifndef __CVMTHREAD_H
#define __CVMTHREAD_H

#include <qapplication.h>
#include <QWidget>
#include <qthread.h>
#include "serial_port.h"
#include "ui_serial_port.h"

#if defined(QT_NO_THREAD)
# error Thread support not enabled.
#endif

class CVmThread : public QThread
{
public:

CVmThread(QWidget *theThWidget);
Serial_Port *theser;
~CVmThread();

void RecDataformClientPort();
void run();
};
#endif


CVmThread.cpp


#include "CVmThread.h"
CVmThread::CVmThread(QWidget*theThWidget):ThreadWi dgetReciver(theThWidget),bThreadStopped(FALSE)
{
SocPort.nComfd=thetvm.ComportOpen(0);
qDebug("Comport:%d",SocPort.nComfd);
}
CVmThread::~ CVmThread()
{

}

void CVmThread::run()
{
RecDataformClientPort();
}

void CVmThread::RecDataformClientPort()
{
theser->RxtextEdit->setplaintext("TEXT");
}


My question is that in serial_port.ui I am having a TextEditBox which I need to access in CVmThread.cpp file.
I need to print some predefined text.
I am creating an object of serial_port class and calling it in CVmThread.cpp but am unable to access the TextEditBox.

Please do help me in this regard.

giblit
29th March 2013, 00:00
why dont you include the header of the seriel port on the thread c++ file

sudhir_reddy83
29th March 2013, 14:11
I have included the header file of serial_port.h in CVmThread.cpp
Am able to execute the statement to print text in TextEditBox
But still am unable to print data in the TextEditBox

Santosh Reddy
29th March 2013, 14:28
You cannot access GUI objects (QTextEdit) from another thread (QThread::run()).

Instead send signal from the Serial_Port object and receive it from a slot of CVmThread.

anda_skoa
2nd April 2013, 10:13
You cannot access GUI objects (QTextEdit) from another thread (QThread::run()).

Instead send signal from the Serial_Port object and receive it from a slot of CVmThread.

Or the other way around, depending on which of the two classes produces the data and which one consumes it.

So instead of calling setPlainText() on the text edit you would emit a signal carrying a QString and connect that to the setPlainText() slot or to a slot that in turn calls setPlainText()

Cheers,
_

P.S.: even if it weren't for the threading issue, accessing some other classes internal members is almost always a bad idea. Methods in Serial_Port that then all methods if Serial_Port internals is way cleaner