How to access TextEditBox of one class in another class in another file
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
Code:
#ifndef SERIAL_PORT_H
#define SERIAL_PORT_H
#include <QWidget>
#include <QTextEdit>
namespace Ui {
class Serial_Port;
}
{
Q_OBJECT
public:
explicit Serial_Port
(QWidget *parent
= 0);
~Serial_Port();
private:
Ui::Serial_Port *ui;
};
#endif // SERIAL_PORT_H
serial_port.cpp
Code:
#include "serial_port.h"
#include "ui_serial_port.h"
Serial_Port
::Serial_Port(QWidget *parent
) : ui(new Ui::Serial_Port)
{
ui->setupUi(this);
}
Serial_Port::~Serial_Port()
{
delete ui;
}
CVmThread.h
Code:
#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
{
public:
Serial_Port *theser;
~CVmThread();
void RecDataformClientPort();
void run();
};
#endif
CVmThread.cpp
Code:
#include "CVmThread.h"
CVmThread
::CVmThread(QWidget*theThWidget
):ThreadWidgetReciver
(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.
Re: How to access TextEditBox of one class in another class in another file
why dont you include the header of the seriel port on the thread c++ file
Re: How to access TextEditBox of one class in another class in another file
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
Re: How to access TextEditBox of one class in another class in another file
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.
Re: How to access TextEditBox of one class in another class in another file
Quote:
Originally Posted by
Santosh Reddy
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