Qt 从父组件向子组件发送数据
Qt sending data from parent to child widget
我有 2 个小部件继承自 QDialog。其中一个小部件被另一个小部件调用。
我需要将数据从父窗口小部件传递给子窗口小部件。例如,我想传递 QStringList.
我可以在两个 classes 中制作信号和插槽。父小部件的插槽 class - transferList(QStringList) - 填充我的 QStringList。
信号槽应该怎么连接?当然,子部件对父部件一无所知。
// .h-file of parent widget.
class ElectricIndicationDialog : public QDialog {
#include "PollIndication.h" // class of child widget
QSharedPointer <PollIndication> pollInd;
public slots:
void transferList(QStringList);
signals:
void listTfansfer(QStringList);
private:
QStringList sendList;
};
// .cpp-file of parent widget
pollInd = QSharedPointer <PollIndication>(new PollIndication());
pollInd->show();
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
}
// .h-file of child widget
class PollIndication : public QDialog {
public slots:
void getList(QStringList);
signals:
void listGet(QStringList);
private:
QStringList recList; // We transfer data to it
}
为此您不需要 signal/slot:您的 parent 知道其 child 的类型并且在其上有一个指针。因此,当您需要向对话框发送数据时,您可以调用 PollIndication 的方法。
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
pollInd->changeTransferList(lst);
}
如果您的对话框是模态的,您也可以仅在需要时创建对话框并将您的列表作为构造函数的参数。
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
PollIndication* pollInd = new PollIndication(lst, this);
pollInd->exec();
}
做一个parentclass来了解他们的children....
通常是个坏主意
你可以在 parent class 中定义一个抽象方法(想想一些纯虚拟的)所以每个 childclass 都被迫实现它......在那之后,parent class 可以调用该方法,child 将根据它必须如何反应来实现登录...
我有 2 个小部件继承自 QDialog。其中一个小部件被另一个小部件调用。
我需要将数据从父窗口小部件传递给子窗口小部件。例如,我想传递 QStringList.
我可以在两个 classes 中制作信号和插槽。父小部件的插槽 class - transferList(QStringList) - 填充我的 QStringList。
信号槽应该怎么连接?当然,子部件对父部件一无所知。
// .h-file of parent widget.
class ElectricIndicationDialog : public QDialog {
#include "PollIndication.h" // class of child widget
QSharedPointer <PollIndication> pollInd;
public slots:
void transferList(QStringList);
signals:
void listTfansfer(QStringList);
private:
QStringList sendList;
};
// .cpp-file of parent widget
pollInd = QSharedPointer <PollIndication>(new PollIndication());
pollInd->show();
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
}
// .h-file of child widget
class PollIndication : public QDialog {
public slots:
void getList(QStringList);
signals:
void listGet(QStringList);
private:
QStringList recList; // We transfer data to it
}
为此您不需要 signal/slot:您的 parent 知道其 child 的类型并且在其上有一个指针。因此,当您需要向对话框发送数据时,您可以调用 PollIndication 的方法。
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
pollInd->changeTransferList(lst);
}
如果您的对话框是模态的,您也可以仅在需要时创建对话框并将您的列表作为构造函数的参数。
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
PollIndication* pollInd = new PollIndication(lst, this);
pollInd->exec();
}
做一个parentclass来了解他们的children....
通常是个坏主意你可以在 parent class 中定义一个抽象方法(想想一些纯虚拟的)所以每个 childclass 都被迫实现它......在那之后,parent class 可以调用该方法,child 将根据它必须如何反应来实现登录...