0

我的程序完美启动并显示了我创建的 QPlainTextEdit(console) 小部件,然后当我从该小部件调用方法时,程序失败并退出

主文件

#include "cmdmw.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    cmdMW w;
    w.show();
    return a.exec();
}

cmdmw.h

#ifndef CMDMW_H
#define CMDMW_H
#include <QMainWindow>
#include <QDebug>
namespace Ui {
class cmdMW;
}
class Console;
class cmdMW : public QMainWindow
{
    Q_OBJECT    
public:
    explicit cmdMW(QWidget *parent = 0);
     void writeToTextEdit(QByteArray message);
    ~cmdMW();
private:
Console *console;
};

#endif // CMDMW_H

cmdmw.cpp

#include "cmdmw.h"
#include "ui_cmdmw.h"
#include "console.h"
cmdMW::cmdMW(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::cmdMW)
{
    ui->setupUi(this);
    console = new Console(ui->centralwidget); 
    QByteArray text="my text";
    writeToTextEdit(text);
}
void cmdMW::writeToTextEdit(QByteArray message)
{
qDebug() << "starting the subroutine";//this is work perfectly and shows the output
console->putData(message);//here it fails >>"The program has unexpectedly finished." 
}

控制台.h

#ifndef CONSOLE_H
#define CONSOLE_H
#include <QPlainTextEdit>
class Console : public QPlainTextEdit
{
    Q_OBJECT

public:
    explicit Console(QWidget *parent = 0);

    void putData(QByteArray data);
};

#endif // CONSOLE_H 

控制台.cpp

#include "console.h"
#include <QScrollBar>
#include <QtCore/QDebug>
Console::Console(QWidget *parent)
    : QPlainTextEdit(parent)
{
    document()->setMaximumBlockCount(100);
    QPalette p = palette();
    p.setColor(QPalette::Base, Qt::black);
    p.setColor(QPalette::Text, Qt::green);
    setPalette(p);
}
void Console::putData(QByteArray data)
{
    qDebug()<< data; //doesn't work because the program exit before reach this line
    insertPlainText(QString(data));
    QScrollBar *bar = verticalScrollBar();
    bar->setValue(bar->maximum());
}
4

0 回答 0