我正在尝试将QProcess
内的mainwindow()
信号连接到QObject
对象内的另一个基于mainwindow()
的类,但是我收到此错误:< / p>
QObject::connect: Cannot connect (null)::readyReadStandardOutput () to (null)::logReady()
下面是代码,它不是完整的,但我不知道它为什么不起作用。
exeProcess.h
#ifndef EXEPROCESS_H
#define EXEPROCESS_H
#include <QObject>
class exeProcess : public QObject
{
Q_OBJECT
public:
explicit exeProcess(QObject *parent = 0);
signals:
void outLog(QString outLogVar); //will eventually connect to QTextEdit
public slots:
void logReady();
};
#endif // EXEPROCESS_H
exeProcess.cpp
#include "exeprocess.h"
exeProcess::exeProcess(QObject *parent) :
QObject(parent)
{
}
void exeProcess::logReady(){
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include "exeprocess.h"
/*main window ---------------------------------------*/
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QProcess *proc;
exeProcess *procLog;
public slots:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));
}
MainWindow::~MainWindow()
{
delete ui;
}
谢谢!
答案 0 :(得分:12)
您需要创建proc
和procLog
个对象。
你只有指针作为类成员,所以你必须初始化那些(new
)。 connect
仅适用于实时对象。
答案 1 :(得分:1)
proc是一个指针,但它没有指向任何东西。您必须在连接之前实例化qprocess!
proc = new QProcess();
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));