QT无法访问会员

时间:2014-08-21 19:42:23

标签: c++ qt class members qplaintextedit

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

的main.cpp

#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." 
}

console.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 

console.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());
}

0 个答案:

没有答案