PDA

View Full Version : problems starting process



parsito
10th May 2007, 18:14
Hi to everyone,
I'm doing one gui using Qt3. First I did the gui with QtDesigner and then, using uic command I created the .h and .cpp files. Now I'm using KDevelop as IDE.
The GUI is really easy, It has only one menu with 4 buttons and one textEdit area where It has to appear all the results of the actions of every button.
I did like this:
I have three files: main.cpp, btscanning.h and btscanning.cpp. Everything compiles perfectly.
In .cpp I have:
the main function:

BTScanning::BTScanning( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
te_results = new QTextEdit( this, "te_results" );
te_results->setGeometry( QRect( 250, 180, 341, 281 ) );

pb_scan = new QPushButton( Menu, "pb_scan" );
pb_scan->setGeometry( QRect( 40, 40, 112, 24 ) );

// signals and slots connections
connect( pb_scan, SIGNAL( clicked() ), this, SLOT( scan() ) );
}
these are the important ones, of course I have more things inside this function.

and then scan() function:
void BTScanning::scan()
{
QProcess *process = new QProcess(this);
QByteArray exit;

process->addArgument("hcitool scan");

exit = process->readStdout();
if(!process->start()){
te_results->insertParagraph(QString("the process is not running"),-1);
}
else{
te_results->insertParagraph(QString(exit),-1);
}
te_expl->insertParagraph(QString("The function scanns an area looking for some blueetoth devices that are connected."),-1);

}

and It appears in te_results "the process is not running".
I've been trying a lot of things but allways the same error. The process is not running and I don't know why.Anybody can help me please?
Thanks a lot.

jacek
10th May 2007, 18:28
exit = process->readStdout();
if(!process->start()){
...
}
else {
...
}
You should first start the process, then read the standard output --- not the other way around.

Note that the output might not be available immediately, so the best way is to read it in a slot connected to QProcess::readyReadStdout() signal.

parsito
10th May 2007, 19:23
Hi jacek,
I did as you said:

QProcess *proc = new QProcess(this);
proc->addArgument("hcitool scan");
connect(proc, SIGNAL(readyReadStdout()), this, SLOT(scan()));
if ( !proc->start() ) {
// error handling
QMessageBox::critical( 0,
tr("Fatal error"),
exit();
}

and then the slot:

void BTScanning::scan()
{
te_results->append( proc->readStdout() );
}
I found one example and I'm following it, more or less. The problem is:
btscanning.cpp: In member function ‘virtual void BTScanning::scan()’:
btscanning.cpp:125: error: ‘proc’ was not declared in this scope
can you please tell me what I'm doing wrong?:confused:

marcel
10th May 2007, 19:56
Declare proc as a member in your class, because the way you did it now is not correct. Now, proc is not visible in the scan function because it is declared in other function.

parsito
10th May 2007, 22:06
Hi again,
I created proc as a member of the class. In .h file:
private:
QProcess *proc; //I tried also with public: QProcess *proc;

and then I did like this in the .cpp file:
BTScanning::BTScanning( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
proc = new QProcess(this);
connect( proc, SIGNAL(readyReadStdout()), this, SLOT(scan()) );

te_results = new QTextEdit( this, "te_results" );
te_results->setGeometry( QRect( 250, 180, 341, 281 ) );

pb_scan = new QPushButton( Menu, "pb_scan" );
pb_scan->setGeometry( QRect( 40, 40, 112, 24 ) );

// signals and slots connections
connect( pb_scan, SIGNAL( clicked() ), this, SLOT( scan() ) );
}

the second connect is because I have the button and the action has to be when I click the button of the menu.
The scan() function is now:
void BTScanning::scan()
{
proc->addArgument("hcitool scan");
proc->start();
if (!proc->start())
te_expl->insertParagraph(QString("proc is not running"),-1);

te_results->append(proc->readStdout());
}

and it appears again the message "proc is not running" in te_expl area....
I tried a lot of different combinations but nothing.....can you help me??

parsito
11th May 2007, 15:18
I did It!!!!
Now everything works perfectly. What I did is create the object process as a member of the main function ( thanks to marcel ). Then I created another function to connect the process to this slot.
The way of doing It was:
inside the scan() function I created the process, add the arguments and start it and then connect it to the other function using readyReadStdout().
Finally the other function that I created is like this:

QProcess *process = (QProcess*)sender();

while(process->canReadLineStdout()){
te_results->insertParagraph(process->readLineStdout(), -1);
}

if(process->normalExit()){
te_results->insertParagraph(process->readStdout(), -1);
}

thanks to wysota for that.
And thanks to everyone for your help.

wysota
11th May 2007, 15:51
Isn't this by any chance a double thread?
http://www.qtcentre.org/forum/f-newbie-4/t-kdialog-and-klocate-problems-6785.html

parsito
11th May 2007, 19:22
Yes, It is, and I'm sorry about that. I thought that once I started in the newbie forum and people answered me, I can pass to the Qt Programming forum because the other thread was only for the firsts steps. I'm sorry but as I said, is the first time that I write in a forum like this one.
I'm really really sorry. Where I have to continue? Well, I will continue in the newbie forum.
Sorry again to everyone.:o

wysota
11th May 2007, 22:32
Don't worry, it's not a problem. Just don't start multiple threads on the same subject. It's confusing and causes the need to repeat some of the explanations from the other thread and sometimes causes simmilar answers in both threads.