[SOLVED] Data exchange between modal forms



  • Hi,

    As the title suggests, I want to exchange data between two modal forms : mainForm and previewForm.

    The fist one, mainForm, is created and called in the main.cpp :
    @int main(int argc,char *argv[])
    {
    QApplication mainApplication(argc,argv);
    mainForm form;
    form.show();
    return mainApplication.exec();
    }@

    The second one, previewForm, is created and called when a button in the mainForm is clicked :
    @void mainForm::openPreviewForm()
    {
    previewForm preview;
    QObject::connect(this, SIGNAL(sendPath(QString)), preview, SLOT(preview.receivePath(QString)));
    preview.setModal(true);
    preview.exec();
    }@

    As I want to pass a QString from the mainForm to the previewForm, I created a public signal in the mainForm class :

    @void sendPath(QString);@

    ... and a public slot in the previewForm class :

    @void receivePath(QString);@

    I got a problem when i try to connect both using :
    @QObject::connect(this,SIGNAL(sendPath(QString)),preview,SLOT(preview.receivePath(QString)));@

    Do you have ideas how to fix this ?

    Thanks !


  • Lifetime Qt Champion

    Hi,

    Your slot name is wrong, it should be
    @SLOT(receivePath(QString))@

    but anyway, it's useless since your dialog is modal and lives only until exec returns. You should simply call receivePath with the value you want before calling exec


  • Moderators

    It should be
    @
    QObject::connect(this, SIGNAL(sendPath(QString)), preview, SLOT(receivePath(QString)));
    @
    Notice no "preview." in the SLOT .

    Edit Yeah, um, what SGaist said :)



  • Thanks for your replies.

    So I got :
    @void mainForm::openPreviewForm()
    {
    previewForm preview;
    QObject::connect(this, SIGNAL(sendPath(QString)), preview, SLOT(receivePath(QString)));
    preview.setModal(true);
    preview.exec();
    }@

    ... and the following error :

    bq. mainForm.cpp:105:90: error: no matching function for call to 'mainForm::connect(mainForm* const, const char [19], previewForm&, const char [22])'


  • Lifetime Qt Champion

    preview is not a pointer, so it should be &preview.



  • Thanks, it is better !

    I updated openPreview like so :

    @void mainForm::openPreviewForm()
    {
    previewForm preview;
    QObject::connect(this, SIGNAL(sendPath(QString)), &preview, SLOT(receivePath(QString)));
    emit sendPath(pathUserFileInput);
    QMessageBox::information(this,"Video Path",pathUserFileInput);
    preview.setModal(true);
    preview.exec();
    }@

    But I don't know how I should implement receivePath(QString).

    Other question : I have to send back a float to the parent form, can I do it using something like this (if I create corresponding signals and slots) :

    @QObject::connect(this, SIGNAL(sendTime(double)), &form, SLOT(receiveTime(double)));@

    Thanks !


  • Lifetime Qt Champion

    There's really no need for the connection and emit just call

    @receivePath(pathUserFileInput);@

    The implementation is up to you. Only you know what you want to do in receivePath.

    Your preview form doesn't need to know anything about your mainForm. So you should do the connection ins openPreviewForm.

    @connect(&preview, SIGNAL(sendTime(double)), this, SLOT(receiveTime()));@



  • Ok thanks, I'll try it.

    So I did this :
    @void previewForm::receivePath(QString pathVideo)
    {
    pathUserFileInput = &pathVideo;
    }@

    Because I got this :
    @class previewForm : public QDialog
    {
    Q_OBJECT
    public:
    [...]
    QString *pathUserFileInput;
    [...]@

    The app crashes when I'm using *pathUserFileInput, is it the good way to get data from a slot ?


  • Lifetime Qt Champion

    Please, have a look at Qt's examples and documentation. They will show you how to use QString and handle parameters when using signals and slots. What you do right now is taking the address of a temporary object, thus the crash.




Log in to reply
 

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