我有兩個類,MyClass
和Widget
。以下是MyClass
班,並從我的Widget
班我想獲得str
變量。這是如何完成的?訪問另一個類的變量
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass();
void fetch();
public slots:
void replyFinished(QNetworkReply*);
private:
QNetworkAccessManager* m_manager;
};
MyClass::MyClass()
{
m_manager = new QNetworkAccessManager(this);
connect(m_manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}
void MyClass::fetch()
{
m_manager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));
}
void MyClass::replyFinished(QNetworkReply* pReply)
{
QByteArray data=pReply->readAll();
QString str(data);
//this str should be available in my widget class
}
編輯:這裏是一個小部件的重要組成部分
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
//here str should be accessed
}
哪裏是Widget類的靜態成員連接的main.cpp? –
如果你想傳遞數據,你*可以讓小部件連接到networkAccessManager或讓MyClass發出一個信號讓它接收。但坦率地說,有這麼多的方法來剝皮這隻貓。 – Huy
我添加了Widget類和str應該被訪問 –