V
V
VerNika2016-01-06 23:16:28
Programming
VerNika, 2016-01-06 23:16:28

Qt. How to pass data between forms?

There is a main form:

class MainWindow
{
    ...
private:
    ...
    QList<Institution> institutions;
};

And the second form:
class AddItem
{
    ...
};

The second form is called from the first:
void MainWindow::on_action_add_item_triggered()
{
    AddItem *form_2 = new AddItem(this);
    form_2->show();
}

How can I pass QList<Institution> institutionsto the second form so that the collection can be edited in it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AtomKrieg, 2016-01-07
@VerNika

By non-const reference (or pointer):

class AddItem
{
    AddItem(MainWindow *parent, QList<Institution>& ins);
...
private:
QList<Institution>& parent_institutions;
};

But in general this is bad practice. The private variable must be edited by the class that owns it. This requires its own methods - MainWindow::AddInstitution, MainWindow::RemoveInstitution, etc. Since this is your QT, then you need to use slots and signals.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question