Passing Variable Between Forms



  • I have two forms. Form one, call it Form1, is asking for name, age, etc. With a button, where user can click on it and it'll be imported to appropriate variable. Form2 shows the variable submitted via Form1, like Name: [Name], Place of Birth: [Place], etc.

    Despite many tries, I can't seem to implement this. I've read enough apparently I'm supposed to use Signal and Slot to do this, but I can't seem to grok this. Like, at all. How do I achieve this?


  • Qt Champions 2018

    @UrsaTempest What did you try and what were the issues?
    For this you define a signal in Form1 and in Form2 you define a slot. Then in your main window (or wherever you create both forms) you connect the signal to the slot. Form1 emits the signal as soon as the data is entered and Form2 puts the values into its UI.



  • First I try using variable like in my OP post, then I try to grok signal and slot. Though, hm.

    I think I sort-of got your explanation? I think. Will try it later today.



  • Right, now I remember my difficulty.
    I can sorta understand how signal and slot works - signal emits signal, slot receive signal. But of the example I read, I don't know how to put the value into slot.

    Like, I create public slots on Form2.h, named them name_write, and dob_write. Then I go to Form2.cpp. Form2.cpp has two labels for showing input, name_display and dob_display.

    My difficulty is I don't know how to use signal and slot to change those label to whatever the user put as input.


  • Qt Champions 2018

    @UrsaTempest The values are passed as parameters:

    class Form1
    {
    signals:
        void someSignal(int value);
    }
    
    class Form2
    {
    public slots:
        void someSlot(int value);
    }
    
    // Then in Form1 you emit the signal
    emit someSignal(10);
    

Log in to reply
 

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