从不同的类访问QComboBox

时间:2017-06-08 22:37:58

标签: c++ qt

我有2个文件Gui.cpp,ReadData.cpp(和标题)。我需要从ReadData.cpp访问Gui.cpp中的QComboBox成员。当我这样做时,代码编译得很好,但是当我运行我的应用程序时,它会崩溃,说访问冲突。我做错了什么?

如何从readdata访问Gui.cpp中的QComboBox?

Gui.h

class Gui : public QObject
{
        Q_OBJECT

    public:
        Gui() {}
        ~Gui() {}
        QComboBox* comboBox;
        ReadData* r;
   private:
        void run();
}

Gui.cpp

void Gui::run()
{
    QWidget *w = new QWidget();

    comboBox = new QComboBox();

    //code...
    r = new ReadData();
    THROW_IF_FALSE(QObject::connect(loadButton, SIGNAL(clicked()), r, SLOT(read())));
 }

readdata.h

class ReadData : public QObject
{
    Q_OBJECT

public:
    ReadData() {}
    ~ReadData() {}

    public slots :
    void read();
private:
    void displayResult(QString arg);
};

readdata.cpp

void ReadData::read()
{
    Gui cb;
    QString str1 = cb.comboBox->currentText();
}

0 个答案:

没有答案