You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Sunday, January 8th 2012, 12:37pm

Qextserialport working in console application but not in gui application

I am trying to get data from serial port and displaying it on QtextEdit.In Console application i was getting data but when i used the same class in gui app the widget is not coming .There is no window at all.i am attaching the files .Please help me to sort out the problem.
my project file is
#-------------------------------------------------
#
# Project created by QtCreator 2012-01-08T09:26:08
#
#-------------------------------------------------

QT += core gui

TARGET = textedit
TEMPLATE = app


SOURCES += main.cpp\
widget.cpp \
myport.cpp

HEADERS += widget.h \
myport.h

FORMS += widget.ui
LIBS += -L/usr/share/lib -lqextserialport




widget.h is
#ifndef WIDGET_H
#define WIDGET_H
#include"myport.h"
#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void writetext(QByteArray mydata);
private:
Ui::Widget *ui;
MyPort *helloport;
};

#endif // WIDGET_H




widget.cpp is
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
helloport =new MyPort ;
helloport->run();
}

Widget::~Widget()
{
delete ui;
}
void Widget::writetext(QByteArray mydata)
{
QString str=mydata ;
ui->textEdit->append(str);
}

myport.h is


#ifndef MYPORT_H
#define MYPORT_H
#include<qextserialport.h>
#include<QMutex>
#include <QThread>

class MyPort : public QThread
{
Q_OBJECT
public:
MyPort(QObject *parent = 0);
~MyPort();

signals:
void sendreceiveddata(QByteArray );
public slots:
void run();
void myslot(QByteArray);
private:
QextSerialPort *port ;
QMutex *recMutex ;
};

#endif // MYPORT_H




myport.cpp is
#include "myport.h"
#include<QDebug>
MyPort::MyPort(QObject *parent) :
QThread(parent)
{
recMutex =new QMutex;
port =new QextSerialPort ;
port->setBaudRate(BAUD38400);
port->setFlowControl(FLOW_OFF);
port->setDataBits(DATA_8);
port->setParity(PAR_EVEN);
port->setStopBits(STOP_2);
if(this->port->open(QIODevice::ReadWrite))
{
qDebug()<<"Connected !";
}
// connect(this,SIGNAL(sendreceiveddata(QByteArray)),this,SLOT(myslot(QByteArray)));
}
MyPort::~MyPort()
{
this->port->close();
delete this->port;
}
void MyPort::run()
{
int byteavailable = 0 ;
char data[1024] ;
QByteArray datareceived ;
forever
{
byteavailable = this->port->bytesAvailable();
if(byteavailable)
{
recMutex->lock();
this->port->read(data,byteavailable);
data[byteavailable]='\0';
datareceived=data ;
recMutex->unlock();
emit sendreceiveddata(datareceived);
// qDebug()<<data;
}
}
}
void::MyPort::myslot(QByteArray mydata)
{
qDebug()<<mydata;
}



main.cpp is
#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();

return a.exec();
}




if i use the same myport class in console application and enable that connection in myport.c then,i get the output in console.

2

Sunday, January 8th 2012, 12:50pm

sounds like something is blocking before w.show().

oh, look here:

Source code

1
helloport->run();

Do you know that calling run on a QThread doesn't make a new thread? calling run directly, will block your current thread. Perhaps you should read the QThread docs?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Sunday, January 8th 2012, 1:27pm

thanks Amleto ,
i replaced the helloport->run();
with helloport->start();
and then it worked

4

Sunday, January 8th 2012, 2:06pm

great! :)
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.