Unsolved Getting Data from a QDialog object
-
Hi All,
I have an application where the mainWindow is calling a QDialog with ok/cancel buttons. On this dialogue window I select several settings which get stored as QStrings in a settings struct. What I would like to do is to pass mainWindow this structure before the dialog closes, when I click the OK button.
I am struggling to find the right mechanism to do this, any help would be appreciated. I guess I can pass a pointer to a struct to the Dialog at the entry point, but as the struct belongs to mainWindow, then the dialogue will have to know about the mainwindow and that seems messy.
I'm aware that this is more of a language issue not strictly a Qt issue.Thank you.
Best Regards
Luis -
@Luism You can put this struct in its own header file, then you can easily share it between main window and dialog.
Also if dialog takes this struct as parameter, then this struct actually belongs to the dialog not the main window. -
Hi Jsulm,
that is a way to do it and I will probably use it that way, but it seems a bit of an overkill.
So if I create a class with the struct in it, create an instance of it in mainWindow and then pass a reference to it to QDialog when I activate it, it should work.
I will try and let you know.
Thank you.
Best Regards
Luis -
Let me propose a different solution:
//In the Dialog class myDialog : ..... struct structSettings { .... } private: structSettings settings; signals: void newSettings(structSettings data); connect(this, &QDialog::finished,[=] {emit newSettings(settings);});
//In MainWindow: connect(DialogObject, &myDialog::newSettings, this, &mainWindow::newSettingsFromDialog); private slots: void newSettingsFromDialog(myDialog::structSettings stgs){ //Do stuff with settings stgs }
This should work, I think.
-
@J.Hilk Yes it should. Do you really need a struct? If the struct only contains few fields then there is no need for a struct.
The problem with your own struct and signals is: you need to register your struct, so Qt knows how to handle it. See: http://doc.qt.io/qt-5/custom-types.html -
Hi Guys,
thanks for the ideas.
J.Hilk idea is very good for the Qt environment and it should work well, I will try it anyway. i haven't got my head around signals and slots to this level just yet. it still isn't my first my first choice when solving these problems.
the question of if I need a structure is an interesting one, no I don't, I will probably use a vector or a QStringList. When you actually start thinking about it like that, your idea of getting the structure as a class that the two objects, mainWindow and dialog would know about is not an overkill in fact is the same thing as picking up an instance of QVector the only difference is that I have to write the class and vector is all done for me.
I will implement the two ideas but using a vector reference is probably the easiest.Thank you
Best regards
Luis -
Hi Guys,
I'm strugling with this...
I tried to implement @J-Hilk code and just can't get it to run. most likelly is because I didn't get the slots/signals properly.so I implemented this part:
//In the Dialog class myDialog : ..... struct structSettings { .... } private: structSettings settings; signals: void newSettings(structSettings data); connect(this, &QDialog::finished,[=] {emit newSettings(settings);});
// at my Dialog class header private: /*Vector of all Port settings to pass to * receiving class */ QVector <QString> portSettingsL; signals: void newSettings(QVector <QString> portSettings); //at my Dialog class CPP connect(this, &QDialog::finished, [=] {emit newSettings(portSettingsL);});
On My mainWindow I only create the QDialogue object when the connect button is clicked:
void MainWindow::connectSerialDialogue() { SerialSettingsDialog *settingsDialogue = new SerialSettingsDialog(); settingsDialogue->show(); }
Now here is where I hit the problem when trying to implement the code. I tried including the connect clause inside this function but that didn't work.
I think I understand well the signals/slots inside the same class, the problem here is that we are talking about getting a signal across two classes and I'm not really getting it.
I would apreciate some direction on this.
Thank you.
Best Regards
Luis -
@Luism This cannot work.
Your signal is:void newSettings(structSettings data);
your slot is:
void newSettings(QVector <QString> portSettings);
So, signal and slot have completely different signatures! They both must have same signature: either both use struct or both use vector, as you cannot pass a struct as parameter where a vector is expected.
-
@Luism
Hi,
like @jsulm said, that is your main problem why Signal/Slot does not work in your situation.If you want a QVector<QString> in your main class I would suggest using that instead of the struct. Than you don't have to declare QVector<QString> using
Q_DECLARE_METATYPE
To your question
connecting 2 classes is rather simple:
connect(ObjectPointer1, FunctionRef1, ObjectPointer2, FunctionRef2)
Your 2 classes, are the Objects ->
//Assuming you make the connect in void MainWindow::connectSerialDialogue(); connect(settingsDialogue, &SerialSettingsDialog::newSettings, this, &MainWindow::gotNewSetting);