PDA

View Full Version : The Widget is not displayed when I Build and run the QT



karthikkm1987
21st December 2011, 11:31
The widget window is not being displayed .
It says like this when I build and run.
Please help me debug the error.


Starting /home/karthik/modbusarm2-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Release/modbusarm2...
The program has unexpectedly finished.
/home/karthik/modbusarm2-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Release/modbusarm2 exited with code 0

My Qt Program is like this:-

main.cpp


#include <QtGui/QApplication>
#include "modbusarm2.h"

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

return a.exec();
}


modbusarm2.cpp


#include "modbusarm2.h"
#include "ui_modbusarm2.h"
#include<QWidget>
#include<QProcess>

modbusarm2::modbusarm2(QWidget *parent) :
QWidget(parent),
ui(new Ui::modbusarm2)
{
ui->setupUi(this);
proc1arm1=new QProcess();
QString stringproc1arm1="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adcbuttonservermainsh";
QString stringproc2arm1="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adcbuttons1disp";
QString stringproc3arm1="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adcbuttons2disp";
QString stringproc1arm2="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adcbuttonservermain2sh";
QString stringproc2arm2="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adc1buttons1disp";
QString stringproc3arm2="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adc1buttons2disp";

/*Friendly Arm 1*/
proc1arm1->start(stringproc1arm1);//main process of arm1
connect(proc1arm1,SIGNAL(readyReadStandardOutput() ),this,SLOT(arm1process()));

proc2arm1->start(stringproc2arm1);//adc value of arm1
connect(proc2arm1,SIGNAL(readyReadStandardOutput() ),this,SLOT(arm1adc()));

proc3arm1->start(stringproc3arm1);//buttons value of arm1
connect(proc3arm1,SIGNAL(readyReadStandardOutput() ),this,SLOT(arm1buttons()));

/*Friendly Arm 2*/
proc1arm2->start(stringproc1arm2);//main process of arm2
connect(proc1arm2,SIGNAL(readyReadStandardOutput() ),this,SLOT(arm2process()));

proc2arm2->start(stringproc2arm2);//adc value of arm2
connect(proc2arm2,SIGNAL(readyReadStandardOutput() ),this,SLOT(arm2adc()));

proc3arm2->start(stringproc3arm2);//buttons value of arm2
connect(proc3arm2,SIGNAL(readyReadStandardOutput() ),this,SLOT(arm2buttons()));

/* Closing All Processess */
connect(ui->close,SIGNAL(clicked()),proc1arm1,SLOT(kill()));
connect(ui->close,SIGNAL(clicked()),proc2arm1,SLOT(kill()));
connect(ui->close,SIGNAL(clicked()),proc3arm1,SLOT(kill()));
connect(ui->close,SIGNAL(clicked()),proc1arm2,SLOT(kill()));
connect(ui->close,SIGNAL(clicked()),proc2arm2,SLOT(kill()));
connect(ui->close,SIGNAL(clicked()),proc3arm2,SLOT(kill()));

}

//////////////Friendly Arm 1 Functions////////////////

void modbusarm2::arm1process()
{
/*QString stringproc2arm1="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adcbuttons1disp";
QString stringproc3arm1="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adcbuttons2disp";
proc2arm1->start(stringproc2arm1);//adc value of arm1
connect(proc2arm1,SIGNAL(readyReadStandardOutput() ),this,SLOT(modbusarm2::arm1adc()));

proc3arm1->start(stringproc3arm1);//buttons value of arm1
connect(proc3arm1,SIGNAL(readyReadStandardOutput() ),this,SLOT(modbusarm2::arm1buttons()));
*/
QByteArray datamain1 = proc1arm1->readAllStandardOutput();
QString textmain1=ui->arm_1b_2->text()+ QString(datamain1);
ui->arm_1b_2->setText(textmain1);

}

void modbusarm2::arm1adc()
{
QByteArray data1 = proc2arm1->readAllStandardOutput();
QString text1=ui->arm_1a->text()+ QString(data1);
ui->arm_1a->setText(text1);

}

void modbusarm2::arm1buttons()
{
QByteArray data2 = proc3arm1->readAllStandardOutput();
QString text2=ui->arm_1b->text()+ QString(data2);
ui->arm_1b->setText(text2);

}

//////////////Friendly Arm 2 Functions////////////////

void modbusarm2::arm2process()
{
/*QString stringproc2arm2="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adc1buttons1disp";
QString stringproc3arm2="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcbuttonsmain/adc1buttons2disp";
proc2arm2->start(stringproc2arm2);//adc value of arm2
connect(proc2arm2,SIGNAL(readyReadStandardOutput() ),this,SLOT(modbusarm2::arm2adc()));

proc3arm2->start(stringproc3arm2);//buttons value of arm2
connect(proc3arm2,SIGNAL(readyReadStandardOutput() ),this,SLOT(modbusarm2::arm2buttons()));
*/
QByteArray datamain2 = proc1arm2->readAllStandardOutput();
QString textmain2=ui->arm_2b_2->text()+ QString(datamain2);
ui->arm_2b_2->setText(textmain2);

}

void modbusarm2::arm2adc()
{
QByteArray data3 = proc2arm2->readAllStandardOutput();
QString text3=ui->arm_2a->text()+ QString(data3);
ui->arm_2a->setText(text3);

}

void modbusarm2::arm2buttons()
{
QByteArray data4 = proc3arm2->readAllStandardOutput();
QString text4=ui->arm_2b->text()+ QString(data4);
ui->arm_2b->setText(text4);

}

////////Ending Functions///////////////

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


modbusarm2.h


#ifndef MODBUSARM2_H
#define MODBUSARM2_H
#include <QProcess>
#include <QWidget>

namespace Ui {
class modbusarm2;
}

class modbusarm2 : public QWidget
{
Q_OBJECT

public:
modbusarm2(QWidget *parent = 0);
~modbusarm2();
QProcess* proc1arm1;
QProcess* proc2arm1;
QProcess* proc3arm1;
QProcess* proc1arm2;
QProcess* proc2arm2;
QProcess* proc3arm2;

public slots:
void arm1process();
void arm1buttons();
void arm1adc();
void arm2process();
void arm2buttons();
void arm2adc();


private:
Ui::modbusarm2 *ui;
};

#endif // MODBUSARM2_H


modbusarm2.pro

#-------------------------------------------------
#
# Project created by QtCreator 2011-12-21T14:00:49
#
#-------------------------------------------------

QT += core gui

TARGET = modbusarm2
TEMPLATE = app


SOURCES += main.cpp\
modbusarm2.cpp

HEADERS += modbusarm2.h

FORMS += modbusarm2.ui


and i am including modbusarm2.ui file too please help 7198

Spitfire
21st December 2011, 15:17
Did you even try to run a debugger?

On line 23, 26, 30, 33 and 36 in your .cpp file you're accessing invlalid pointers so no wonder your app crashes.

You need to repeat line 11 for every pointer defined in the header file.

karthikkm1987
22nd December 2011, 07:51
Thanks.it worked.Yes you were right i didnt create new processes initially.
One more thing Is it possible to select required data from the readAllStandardOutput() output of QProcess and put them seperately in textedits or lineedits????
can you give me example?

Spitfire
22nd December 2011, 14:24
Probably it is possible, but I don't quite understand your question.

Do you want to break the output into lines, or do you want to extract some data from a line?
In both cases you just need to find what you're looking for in QByteArray.

Depending on what you need it may be easier to go different route rather tahn readAllStandardOutput().

karthikkm1987
23rd December 2011, 05:45
the shell script is like this.


#!/bin/bash
for((i=1,j=20;j,i;i++,j++))
do
echo "i=$i"
echo "j=$j"
done

the output will come like this when i execute in one single qprocess.

i=1
j=20
i=2
j=21
...........so on


I have edited the display like this.


#include "testprocess.h"
#include "ui_testprocess.h"
#include<QProcess>

testprocess::testprocess(QWidget *parent) :
QWidget(parent),
ui(new Ui::testprocess)
{
ui->setupUi(this);
proc=new QProcess();
QString stringproc="/home/karthik/test";
proc->start(stringproc);
connect(proc,SIGNAL(readyReadStandardOutput()),thi s,SLOT(dataread()));
connect(ui->close,SIGNAL(clicked()),proc,SLOT(kill()));
}

void testprocess::dataread()
{
QByteArray data1,data2;
data1=data2=proc->readAllStandardOutput();

QString text1,text2;
text1=QString(data1);
text1.mid( 0, text1.indexOf(" ", 0 ) );
text2=QString(data2);
text2.mid( 1, text2.indexOf(" ", 1 ) );
ui->ivalue->setText(text1);
ui->jvalue->setText(text2);
}

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


here I am getting the output
7202

What needs to be done so that
1)I will get only output of values i=1,i=2 ..... in one text edit and j=20,j=21 ...... in another text edit.
2)After some editing again I need to get only i and j values in separate text edits.

Spitfire
23rd December 2011, 11:34
Ok, that's quite simple.

Break the output string on new lines and then put all lines with even indexes in one dialog, and with odd indexes in other dialog.


QByteArray data = proc->readAllStandardOutput();
QStringList lines = QString( data ).split( "\n" );
for( int i = 0; i < lines.size(); ++i )
{
qDebug() << i%2;
if( i%2 )
{
ui->j_box->addLine( lines[i] );
}
else
{
ui->i_box->addLine( lines[i] );
}
}That's just a mock code so don't expect it to compile, but it should give you an idea what you need to do.

if you need to get the values only without the "i=" and "j=" before adding every line to a text box split it on "=" and take only last element. something like this:
ui->j_box->addLine( lines[i].split("=").last() );

Note: this will probably work only when you hook it to 'finished()' signal, because when readyReadStandardOutput() is emitted not whole data is there.

I'm not sure how readyReadStandardOutput() is emitted (if it's per line of output or less offten).
If it's per line you need to keep track what you've read previously (i or j) and toggle between textboxes.
If you get two lines then you can use the same approach as above.

Hope this helps.