Результат работы команды, которую указываю в QProcess->start("ping localhost"), отображается, но
следующей за ней вызов
QTextStream out(stdout);
out << "Hello";
или
printf("Hello");
fflush(stdout);
ничего не выдает.
Исподьзую следующий пример:
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("system"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("IBM 866"));
MainWindow w;
w.show();
return a.exec();
}
thread.h
#ifndef THREAD_H
#define THREAD_H
#include <QThread>
class QProcess;
class ServerThread : public QThread
{
Q_OBJECT
public:
ServerThread(QObject *parent = 0);
~ServerThread();
protected:
void run();
public slots:
void output();
signals:
void updateOutput(const QString &msg);
private:
QProcess *process;
};
#endif // THREAD_H
thread.cpp
#include "thread.h"
#include <QProcess>
ServerThread::ServerThread(QObject *parent) : QThread(parent), process(0)
{
}
ServerThread::~ServerThread()
{
quit();
wait();
delete process;
}
void ServerThread::run()
{
process = new QProcess;
process->setProcessChannelMode(QProcess::MergedChannels);
connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(output()));
process->start("ping localhost"); //выводится
// а это не выводится
printf("Hello");
fflush(stdout);
exec();
}
void ServerThread::output()
{
QByteArray bytes = process->readAllStandardOutput();
emit updateOutput(bytes);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QProcess>
#include <QString>
#include "thread.h"
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_clicked();
void setOutput(const QString &msg); //пробовал объявлять и в private и в public, различий нет
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ServerThread *th = new ServerThread;
connect(th,SIGNAL(updateOutput(QString)),this,SLOT(setOutput(QString)), Qt::QueuedConnection);
th->start();
}
void MainWindow::setOutput(const QString &msg)
{
ui->textEdit->insertPlainText(msg);
}
Может быть какие-то настройки проекта надо изменить? или я что-то не так делаю?
Помогите, пожалуйста, разобраться!