Qt readAllStandardOutput() 导致程序意外结束

发布于 2024-11-14 10:15:35 字数 1460 浏览 1 评论 0原文

当我尝试将 readAllStandardOutput() 发送到 QString 时,即使我将 QByteStream 转换为 QString,我也会遇到意外的程序崩溃,知道这是为什么吗? 的源代码,

这是mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>
#include <QString>


#include "exeprocess.h"

/*main window ---------------------------------------*/

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QProcess *proc;

signals:
    void outLog(QString outLogVar); //plug this into the QTextEdit box

public slots:
    void logReady(); // plug the QProcess into this


private:
    Ui::MainWindow *ui;
};




#endif // MAINWINDOW_H

mainwindow.cpp

#include <QByteArray>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QProcess *proc = new QProcess; //initialize proc
    QStringList arguments;
    arguments << "-h";

    connect(proc, SIGNAL(readyReadStandardOutput ()), this, SLOT(logReady()));

    proc->start("/Applications/Graphics/3Delight-9.0.87/bin/renderdl", arguments);

}

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

void MainWindow::logReady(){
    QString str = proc->readAllStandardOutput();
    emit outLog(str);
}

谢谢!

When I try to emit readAllStandardOutput() to a QString im getting an unexpected program crash, even if i connvert the QByteStream to a QString, any idea why that is? heres teh source

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>
#include <QString>


#include "exeprocess.h"

/*main window ---------------------------------------*/

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QProcess *proc;

signals:
    void outLog(QString outLogVar); //plug this into the QTextEdit box

public slots:
    void logReady(); // plug the QProcess into this


private:
    Ui::MainWindow *ui;
};




#endif // MAINWINDOW_H

mainwindow.cpp

#include <QByteArray>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QProcess *proc = new QProcess; //initialize proc
    QStringList arguments;
    arguments << "-h";

    connect(proc, SIGNAL(readyReadStandardOutput ()), this, SLOT(logReady()));

    proc->start("/Applications/Graphics/3Delight-9.0.87/bin/renderdl", arguments);

}

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

void MainWindow::logReady(){
    QString str = proc->readAllStandardOutput();
    emit outLog(str);
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-11-21 10:15:35

这一行就是问题所在:

QProcess *proc = new QProcess; //initialize proc

您通过重用该名称来隐藏成员变量。当调用 logReady 时,您调用 readAllStandardOutput() 的“proc”是一个不同的(空)指针,因此一切都会崩溃。修复很简单:将上面的行替换为

proc = new QProcess;

This line is the problem:

QProcess *proc = new QProcess; //initialize proc

You're shadowing the member variable by reusing that name. When logReady is called, the 'proc' that you call readAllStandardOutput() on is a different (null) pointer and so everything crashes. The fix is simple: replace the above line with

proc = new QProcess;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文