构造函数中无法访问的QString

时间:2016-11-24 08:02:47

标签: c++ qt subclass qobject

我在一个大项目中搞砸了子类化。我意识到问题出现在子类化过程中,导致QString in的构造函数中的无法访问Subclass1

如果我在test = in;设置了一个breakpoiont,为什么我无法访问QString?

Windows 7 64位,Qt 5.6.2,MinGW 4.9.2

(可能只是一些粗心的错误)

Update1:​​现在有趣的部分,在超类的构造函数中也无法访问,但在second(in)中有效。

更新2:添加了图片

main.h

#ifndef MAIN_H
#define MAIN_H

#include <QObject>

class Superclass : public QObject
{
    Q_OBJECT
public:
    explicit Superclass(QString in, QObject *parent = 0) : QObject(parent), second(in)
    {
        QString test = in;
    }
protected:
    QString second;
};

class Subclass1 : public Superclass
{
    Q_OBJECT
public:
    Subclass1(QString in, QObject *parent = 0) : Superclass(in, parent)
    {
        test = in;
    }

protected:
    QString test;
};

class Manager : public QObject
{
    Q_OBJECT
public:
    explicit Manager(QObject *parent = 0) : QObject(parent)
    {
    }

public slots:
    void runinvoked()
    {
        ptr1 = new Subclass1("test",this);
    }

    void rundirect()
    {
        ptr2 = new Subclass1("test",this);
    }

protected:
    Superclass *ptr1;
    Superclass *ptr2;
};

#endif // MAIN_H

main.cpp

#include "main.h"

#include <QCoreApplication>
#include <QObject>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Manager *m = new Manager();
    m->rundirect();
    QMetaObject::invokeMethod(m,"runinvoked");
    return a.exec();
}

带有QString in空的超类,已正确分配QString second enter image description here

子类w /无法访问QString in enter image description here

0 个答案:

没有答案