PDA

View Full Version : QProcess Bash in Event Filter (Multiple Processes)



Arsenic
8th November 2008, 03:38
Hi
I'm trying to build a Multi-Tab Bash program, so I can have a separate QProcess of Bash in multiple tabs, and add and delete tabs as I like.

But whenever I run my program, without the QLists and only one instance without the Tabs, it works fine. It DOESN'T work fine when I add in the QLists and stuff.

The error: the Bash doesn't show up in many of the terminals.



m_Logw << new LogWindow;
m_Logw[0]->installEventFilter(this);
m_Logw[0]->setLineWrapMode(QTextEdit::WidgetWidth);
m_Bash << new QProcess;
m_Bash[0]->setReadChannelMode(QProcess::SeparateChannels);
connect (m_Bash[0], SIGNAL(readyReadStandardOutput()),
this, SLOT(showOutput()));
m_Bash[0]->start("bash", QStringList("-i"), QIODevice::ReadWrite);



//.................. after constructor....................

bool Class::eventFilter(QObject *o, QEvent *e) {
if (e->type() == QEvent::KeyPress) {
QKeyEvent *k = static_cast<QKeyEvent*> (e);
int key = k->key();
QString str = k->text();
m_UserInput[m_Tabs->currentIndex()].append(str);
updateCursor();
if ((key == Qt::Key_Return) || (key == Qt::Key_Enter) ) {
execute();
return true; /* We processed the event. This
prevents other widgets from seeing it. */
} else if(key == Qt::Key_Backspace){
m_UserInput[m_Tabs->currentIndex()].chop(2); // chop the ending character and last character
QString temp = m_Logw[m_Tabs->currentIndex()]->toPlainText();
temp.chop(1); // remove the last character from the console window
m_Logw[m_Tabs->currentIndex()]->setPlainText(temp); // reset it to the new version
updateCursor(); // update cursor to go to end of screen
return true;
} else {
m_Logw[m_Tabs->currentIndex()]->insertPlainText(str);

return true;
}
}
return QMainWindow::eventFilter(o, e); /* Let the
base class eventFilter have a shot at it. */
}

void Class::showOutput() {
QByteArray bytes = m_Bash[m_Tabs->currentIndex()]->readAllStandardOutput();
m_Logw[m_Tabs->currentIndex()]->append(QString(bytes));
}

caduel
8th November 2008, 08:42
in the first few lines you add objects to lists and the access them via index [0]. you are aware that this only is correct if the list was empty before?
otherwise the appended(!) objects will be found at [size()-1].
I suggest you setup the objects first and then append them. This is a tiny bit faster and saves you the chance of getting the index wrong.

HTH