如何获取另一个类中对象的值?比如我有 2 个类(类 WidgetA 和类 WidgetB),这两个类都是由类 Widget 创建的,如下所示:
//widget.h
#include <QWidget>
#include <QtGui>
class widgetA;
class widgetB;
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
private:
QComboBox* comboBox;
QStackedWidget* stackWidget;
widgetA *wa;
widgetB *wb;
};
class widgetA : public QWidget
{
Q_OBJECT
public:
widgetA(QWidget *parent = 0);
private:
QString mystr;
QLineEdit* lineEdit;
};
class widgetB : public QWidget
{
Q_OBJECT
public:
widgetB(QWidget *parent = 0);
QLabel* label;
};
这是文件widget.cpp:
#include "widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
comboBox = new QComboBox(this);
comboBox->addItem(tr("Widget A"));
comboBox->addItem(tr("Widget B"));
wa = new widgetA(this);
wb = new widgetB(this);
stackWidget = new QStackedWidget(this);
stackWidget->addWidget(wa);
stackWidget->addWidget(wb);
stackWidget->setCurrentIndex(0);
connect(comboBox, SIGNAL(currentIndexChanged(int)), stackWidget, SLOT(setCurrentIndex(int)));
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(comboBox);
layout->addWidget(stackWidget);
setLayout(layout);
}
widgetA::widgetA(QWidget *parent):
QWidget(parent)
{
lineEdit = new QLineEdit;
QHBoxLayout *lay = new QHBoxLayout;
lay->addWidget(lineEdit);
lineEdit->setReadOnly(true);
setLayout(lay);
}
widgetB::widgetB(QWidget *parent):
QWidget(parent)
{
label = new QLabel("Hello QT");
QHBoxLayout* lay = new QHBoxLayout;
lay->addWidget(label);
setLayout(lay);
}
如何将 WidgetB 类中的 QLabel 文本label
转换为 WidgetA 类中的 QString mystr
?