Pass data between QWidgets



  • I have a little problem, I have a Log_in window which connects to a server when a "Connect to Server" button is pressed, and when the connection is established I want to Register so I could LogIn, but in my new register window, I can't send the data because the Reg form does not contain the info about connection. Question: how I can pass an object(which contains the connection info) from the LogIn window, to the Reg form?


  • Qt Champions 2016

    Hi
    Do you open the Reg.frm from login.form ?



  • @mrjj Hello. Yes. Log_In form connects to the server (created a Communication Class), when I press the button, and I want to send the data from the Reg_form to the server, but I don't know how to do this, because the object which contains the connection info is in the Log_in form... I tried to use extern but no luck


  • Qt Champions 2016

    ok. extern is not a super good choice, you
    can give also " Communication Class" to reg.form
    when you construct it and show it.

    I assume you have something like
    Reg_form Reg(this);
    Reg.exec() to show the Reg_form on button click

    You could just add a public function in Reg.form
    void SetComm( CommunicationClass *TheComm )
    and let it store a internal pointer to it that u can use in its button click.

    • so u go something like
      Reg_form Reg(this);
      Reg->SetComm( TheCommunicationClass );
      Reg.exec()
    • the TheCommunicationClass
      should live as member variable in Log_In-form (in the .h file, in the class)
      And be the one you do actually open/use. use & if its not a pointer.

    It would be better to have CommunicationClass in mainwindow and let the dialogs sends signals to make main
    perform the login/send etc. as using a pointer becomes clumsy if u say want to open yet another dialog but
    can indeed work for 2.



  • @mrjj And Now I want to copy the object stuff like this:
    S_Cop is a Communication object which is in MRegForm. In the scope of the function it works good. But in other functions it loses all it's data
    void MRegForm::A_obj(Com_Network &obj){
    S_Cop = &obj;
    }
    The previous problem is solved. I was using the Signal/Slot mechanism. In LogIn form I create an instance of RegForm, and when in the LogIn form I connect to the server I emit a signal which will pass the object to the reg form... and it worked. Is it ok if i'll do it like this?
    Code:
    LogIn:
    MLogIn::MLogIn(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MLogIn)
    {
    QObject::connect( this, SIGNAL( PassObject(Com_Network&)),&Regs, SLOT( A_obj(Com_Network&)));
    ui->setupUi(this);
    }
    void MLogIn::on_Connect_S_clicked()
    {
    Conn.Initialize();
    Conn.CreateSocket("127.0.0.1",'1400');
    Conn.Connect();
    emit PassObject(Conn);
    }
    RegForm:
    void MRegForm::A_obj(Com_Network &obj){
    qDebug()<<"Signal achieved";
    qDebug()<<obj.i;
    }


  • Qt Champions 2016

    @mandruk1331
    hi
    Good work :)
    Yes it seems fine.



  • @mrjj And Now I want to copy the object stuff like this:
    S_Cop is a Communication object which is in MRegForm. In the scope of the function it works good. But in other functions it loses all it's data
    void MRegForm::A_obj(Com_Network &obj){
    S_Cop = &obj; // this one is OK!
    qDebug()<<S_Cop.i; // i - is 9
    }

    void MRegForm::on_pushButton_clicked() //Register_button
    {
    qDebug()<<S_Cop.i; - is 0 // Not good. I think because it's out of scope. But I copied it in the previous slot, and the scope is the class, so it has to contain the data from the object I passed... but there is no data, dunno why
    }


  • Qt Champions 2016

    @mandruk1331
    Hi
    Im not sure what Com_Network is. Is it your own class?

    This statement could make copy of S_Cop if is not type (Com_Network *)
    S_Cop = &obj;

    For test, try with pointers in signal+slot instead of & reference.
    so
    emit PassObject( &Conn);
    A_obj(Com_Network *obj)
    Com_Network *S_Cop
    Reference should have worked but since S_Cop seems bad in
    on_pushButton_clicked, try with pointers and see if it makes difference.



  • @mrjj Com_Network is my own class which I use for Communication between Client and Server. Ok, I will give it a try.


Log in to reply
 

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