PDA

View Full Version : Qt4/C++ - Accessing data on a QDialog form a QMainWindow



jimbo
29th March 2015, 19:37
Hello,

I'm constructing multiple QDialogs from a button on a QMainWindow.
Each dialog gets filled with different data when constructed and I (may) edit this data.
I can find which dialogs have been edited and get the dialogs objectName when I click
the qmainwindow 'save' button.
Is it possible to access, say a QLabel->text() on one of the dialogs only knowing the dialogs objectName?
If 'yes, how?
Perhaps I should adopt a different approach.

Regards
class propForm : public QDialog
{
Q_OBJECT

public:
propForm(QWidget *parent = 0);
...

propForm::propForm(QWidget *parent) : QDialog(parent)
{
...
void myMaiWindow::showDialogButton() // slot
{
cm::formCount += 1;
prop = new propForm(this);
prop->setObjectName("prop" + QString::number(cm::formCount));
qDebug() << "name - " << prop->objectName();
}

void myMainWindow::saveButton() // slot
{
qDebug() << "Save - " << cm::filePath;
qDebug() << "last Lost - " << cm::formLostFocus << "\n";

QList<QDialog *> widgets = findChildren<QDialog *>();
qDebug() << widgets;

for (int i; i < cm::formCount; i++) {
// if edited get data and save the file
// getting the data is the don't know bit.
}

jefftee
30th March 2015, 04:01
Have you tried QDialog::findChild()?

d_stranz
30th March 2015, 04:11
Perhaps I should adopt a different approach.

You should adopt a different approach. The whole idea of object-oriented programming is to encapsulate information and package it in ways that you do not need to know anything about the internal implementation of something in order to use it in an application.

In Qt terms, it means that the user of a dialog should never need to know anything about the sub-widgets contained within the dialog. Instead, the dialog should inform its user that something it might be interested in has been edited by the user. So you have a bunch of ways to do this:

- design a data structure (a class or struct) that holds the information that is to be edited by the dialog, and give the dialog get / set methods so that the application can set the initial values for editing, and get the edited values when the user closes the dialog with the OK button.

- use the same data structure and a "set" method, but add a signal to the dialog that is emitted with an edited copy of the struct when the user changes something

- expose signals and slots for each sub-widget in the dialog that the application can connect to to learn of editing changes.

These are ranked from highest degree of encapsulation to lowest. The first method can be used without any exposed dialog signals or slots, meaning you can use anything that accepts the data structure. The last method is the worst because it essentially exposes everything. You can't change anything about the dialog without changing the application that uses it.

anda_skoa
30th March 2015, 08:20
Regarding findChildren: it you already know that you need access to an object after its creation, why throw away the knowledge of its pointer?

Better store it in some form of container, e.g. an associative container like a map or hash.

Cheers,
_