PDA

View Full Version : Passing a object between QDialog and QMainWindow



m_bishop
16th May 2013, 06:45
Can a QPushButton inside a QDialog create an object and pass it back to a QWidget inside of QMainWindow?

A dialog pops up with data that needs to be used to create a device and then this device gets monitored by the mainwindow.

A rough online example or a chapter to read in a book or a class info would be a great kick starter.

lanz
16th May 2013, 08:05
I can think of two possible ways, the first way you show your dialog, then after it closed with Ok status you can call dedicated dialog method to get your configured device.

class DialogV2 {
public:
YourObject *getConfiguredObject () {
return new YourObject;
};
};
A slightly alternative approach would be to return configuration structure with all necessary parameters and then create the device in your main window class (like QFileOpenDialog returning file name and then you create a QFile out of it).

Another way is to using signals and slots

class MainWindow {
public slots:
void objectCreated(YourObject *o);
};

class Dialog {
private slots:
void onCreateClicked () {
YourObject *obj = new YourObject;
emit objectCreated (obj);
};
signals:
void objectCreated (YourObject *o);
};
But it has two downsides that iI can think of:
1. You need to trace the lifetime of your object, and don't get it lost when signal is unconnected.
2. Object creation and corresponding slot will be called while dialog is still active, which can be undesirable behaviour.

Generally I suggest first solution, second solution is better if you generate lightweight value-type objects and don't really care about them.

m_bishop
16th May 2013, 14:58
I can think of two possible ways, the first way you show your dialog, then after it closed with Ok status you can call dedicated dialog method to get your configured device.

class DialogV2 {
public:
YourObject *getConfiguredObject () {
return new YourObject;
};
};


I am not sure how this would work as that the data would be destroyed when the QDialog closes. How would the QDialog save the data to be returned when my QWidget in QMainWindow calls
myDevice = myDialog->getConfiguredObject;

Maybe I just need some more caffeine to understand what you are trying to tell me.



A slightly alternative approach would be to return configuration structure with all necessary parameters and then create the device in your main window class (like QFileOpenDialog returning file name and then you create a QFile out of it).


I understand this approach.




Another way is to using signals and slots

class MainWindow {
public slots:
void objectCreated(YourObject *o);
};

class Dialog {
private slots:
void onCreateClicked () {
YourObject *obj = new YourObject;
emit objectCreated (obj);
};
signals:
void objectCreated (YourObject *o);
};
But it has two downsides that iI can think of:
1. You need to trace the lifetime of your object, and don't get it lost when signal is unconnected.
2. Object creation and corresponding slot will be called while dialog is still active, which can be undesirable behaviour.

Generally I suggest first solution, second solution is better if you generate lightweight value-type objects and don't really care about them.

I started reading about slots and signals last night and this might be what I was looking for. I'll mock something up to see how the downsides affect behavior. I would still like to understand what you are telling me on the first approach so that I can mock something up and put it in my tool box, so to speak.

lanz
16th May 2013, 15:32
I am not sure how this would work as that the data would be destroyed when the QDialog closes. How would the QDialog save the data to be returned when my QWidget in QMainWindow calls
myDevice = myDialog->getConfiguredObject;
Oh, I need more caffeine too, didn't get your point at first :D

class MainWindow {
public:
void onCreateDeviceClick () {
DialogV2 *dlg = new DialogV2;
if (QDialog::Accepted == dlg->exec ()) {
//user accepts
// dialog here is closed, but not destroyed
// all it's data is in reach
// so we can do anything with the data
// think again usage pattern of the QFileOpenDialog
this->myDevice = dlg->getConfiguredObject ();
} else {
// user cancels out
};
delete dlg;

};
};
Hope this clarifies it a bit.

m_bishop
16th May 2013, 15:53
Yes, it does. I can do good things with that info.