Accessing from one form to another

  • Hi.
    I'm new in Qt and I've stuck with a problem. I have QDialog to edit sql record, and MainWindow with viewTable. And all i want to do is possibilty to "refresh" viewTable in MainWindow when I click save button on edit dialog. Is there any way to do it without inheritance? I've searched forum and google as well, I've tried signals and slots but I'm far too new in Qt to do it properly.

  • Signals and slots are the right track.
    What you are after is communication between two objects. It doesn't really matter if the objects are forms or something else. What you might do, is equip your main window with a slot (I propose to call it refresh()), and give your dialog a signal called triggerRefresh(). Then, when you create your dialog from your main window, you connect the signal to the slot you just defined. Next, you need to make sure you actually emit the signal from the dialog at the appropriate time.

    However, I am wondering, if you need all of this. I assume the dialog will only need to be visible while editing a record, and after editing (on pressing Save) the dialog will be closed and only then the main window will need to update? In that case, you can just make the dialog modal and trigger the update based on the result of your dialog (accepted or rejected; you only refresh on accepted (Save) of course).

    Yet another way of dealing with this, may be to make use of the model/view framework. Since you display a table, you have a model that holds your data, I'd think. You currently pass the dialog you use to edit the data the actual data to edit, I assume. Am I right? How about instead of passing the data, you pass the relevant QModelIndex(-es)? That way, you can simply have the edit dialog call setData() on the model when you click the Save button, and the data will be stored in the underlying model directly and automatically updated in your main window. Note that this way also uses signals and slots, but those signals and slots are already part of the model/view framework.

  • Thank You for reply, Andre. Dialog is set to modal and I'm trying to do it exactly in way You've wrote in second paragraph. However I'm not sure how to do it properly, anyway I will try to implement it.

  • Well, you should recognize that when the dialog's exec() call returns, you still have access to a variable that represents that dialog. That gives you the opportunity to call methods on the dialog: that it is closed (no longer visible to the user), does not mean the object is destroyed! So, just give your dialog some methods to access the values you need, and call on those methods from your main window after the dialog has returned an accepted value.

  • I assume that the connect should be in my QDialog constructor?
    @connect(ui->pushButtonSave, SIGNAL(clicked()),QObject MainWindow, SLOT(MainWindow::refresh()));@
    And I don't have any idea how to declare MainWindow connection. QObject MainWindow doesn't work...

  • If you want a signal-slot connection (I thought that you understood that you don't need one), then you make such a connection at the place where you have references to both the involved objects. The most logical place in this case would be the place in your mainWindow code where you create the dialog. So, not in the constructor of the dialog. Something like this:

    EditDialog* editDialog = new EditDialog(this);
    //set your values
    connect(editDialog, SIGNAL(triggerRefresh()), this, SLOT(refresh()));

    To make your dialog actually emit the signal, you may do something like this in the constructor of your dialog:

    connect(ui->pushButtonSave, SIGNAL(clicked()), this, SIGNAL(triggerRefresh()));

    Note that it seems that you did not use QDialog as the base of your dialog. Perhaps you should re-evaluate that choice. QDialog gives you the option to use standard buttons, including a save button, that have the appropriate roles pre-defined. The nice thing is, is that they will order themselves in the order that is defined by the platform your application runs on, so the user is never suprised by what button will cancel and what button will accept.

  • I think I don't really understand the idea of slots/signals and triggers too :/, so I better learn more basics:). My last experience with GUI was C++ Builder, and Qt is something completly new for me. in Builde what i have to do was include header file of second form and it was just all. Thanks for reply.

  • I can't do it. Could somebody show me an example of accessing one form from another?
    My test code
    from mainwindow.h
    @public slots:
    void test();
    void trigger();

    private slots:
    void on_pushButtonMainWindow_clicked();@
    @void MainWindow::on_pushButtonMainWindow_clicked()
    Dialog dialog(this);
    connect(this, SIGNAL(trigger()), &dialog, SLOT(test()));

    void MainWindow::test()
    void MainWindow::trigger()
    ui->lineEdit->setText("this is a test");
    from dialog.cpp
    @ connect(ui->pushButtonDialog, SIGNAL(clicked()), this, SLOT(MainWindow::trigger()));@
    And it gives me:
    @Object::connect: No such slot Dialog::MainWindow::trigger() in ..\slots\dialog.cpp:9
    Object::connect: (sender name: 'pushButtonDialog')
    Object::connect: (receiver name: 'Dialog')
    Object::connect: No such signal MainWindow::trigger() in ..\slots\mainwindow.cpp:20
    Object::connect: (sender name: 'MainWindow')
    Object::connect: (receiver name: 'Dialog')@
    Sorry for double post

  • Signals are sent from the objects that define them. Your main window has no signal trigger - that's what the debug console tells you literally :-)

    In your case, you probably want to connect a signal of a button or an action to the slot.

    If you define your own signal triggered, it does not need an implementation in the cpp file (actually you must not provide any!). And if you have that your signal must be emit somewhere to have the connection be called in the end.

    Did you read the "Signals & Slots": docs already?

  • I've briefly read this doc and C++ GUI Programming with Qt 4 By Jasmin Blanchette, Mark Summerfield too (about signals and slots).
    More, in project I'm actually working with, I have 2implementations of signals & slots, and they are working fine. But they both signals and slots in mainwindow area.
    @ connect(ui->tableViewHouses, SIGNAL(clicked(const QModelIndex&)), this, SLOT(clickedRow(const QModelIndex&)));
    connect(ui->tableViewInhabitants, SIGNAL(doubleClicked(const QModelIndex&)),
    this, SLOT(doubleClickedRow(const QModelIndex&)));
    I have a problem to connecting from main to dialog. I know that problem is my lack of knowledge, but i have no idea howto do it between different windows

  • Does your edit dialog have a signal defined called trigger (that is, does it have a line in the header file that looks like this)?
    void trigger();

    On top of that, do you have this line in the header of that dialog?
    Q_OBJECT // this line is usually the first line inside the actual class definition

    Your error message suggests that you don't.

  • Define the signal in one form, emit when you have something to tell to the other form.

    Connect in the other form. Or connect in some managing code - it depends.

    Other possibility is to give the form a pointer to the other form and call a method directly.

    Or do it like Andre suggested:
    Call exec() on a "QDialog": subclass and get the data from a method of this dialog and put it into the widgets of the main form, if the exec returns with Accepted.

    I personally would go with the last approach.

  • [quote author="Andre" date="1299516055"]Does your edit dialog have a signal defined called trigger (that is, does it have a line in the header file that looks like this)?
    void trigger();

    On top of that, do you have this line in the header of that dialog?
    Q_OBJECT // this line is usually the first line inside the actual class definition

    Your error message suggests that you don't.[/quote]
    Yes, I haven't get signal, I've added trigger now, but still it doesn't work properly.

    Its to much for me. Im quiting with this. Thanks for Your replies and Your time. Maybe some other time I'll try to understand slots and signals between forms.

  • What you do is: Have the main window emit a signal that the dialog reacts on. You most probably want it the other way round!

    Good luck with your projects so far. If you only read the docs briefly you'll be stuck in similar situations here and then in the future again... be warned!

  • It works:) Thank You both for help. I've do it nor in signals&slots but in the way which Andre suggested with dialog set on Accepted
    @void MainWindow::on_pushButton_clicked()
    Dialog *dlg = new Dialog(this);
    if(dlg->exec() == QDialog::Accepted) ui->lineEdit->setText("Accepted");
    This works perfectly for me, altough i know that i will have to learn slots&signals in future.
    Thanks again for Your suggestions.
    Best regards.

  • nicely explained, here...

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.