1
votes

I have a 2 widgets inherited from QDialog. One of these widgets is called by another widget.

I need to pass data from the parent widget to the child. For example, I want passing QStringList.

I can make signals and slots in both classes. Slot of parent widget class - transferList(QStringList) - filling my QStringList.

How should I make the signal and slot connection? The child widget, of course, knows nothing about the parent.

// .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
}
2

2 Answers

0
votes

You don't need a signal/slot for that: your parent knows the type of its child and has a pointer on it. So, you can call a method of PollIndication when you need to send data to your dialog.

void ConfIndication::transferList(QStringList lst) {
    lst.append("str1");
    lst.append("str2");
    pollInd->changeTransferList(lst);
}

If your dialog is modal, you can also create your dialog only when needed and give your list as parameter of the constructor.

void ConfIndication::transferList(QStringList lst) {
    lst.append("str1");
    lst.append("str2");
    PollIndication* pollInd = new PollIndication(lst, this);
    pollInd->exec();
}
0
votes

It is normally a bad idea to make a parent class to know what are their children....

you can in the parent class define an abstract method (think about some pure virtual) so every childclass is forced to implement it... after that, the parent class can invoke the method and the child will implement the login depending on how it must react to it...