Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pass data between QWidgets
Forum Updated to NodeBB v4.3 + New Features

Pass data between QWidgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
server - clientc++send
9 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by
    #1

    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?

    Mandruk1331

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      mandruk1331M 1 Reply Last reply
      0
      • mrjjM mrjj

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

        mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by mandruk1331
        #3

        @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

        Mandruk1331

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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.

          mandruk1331M 1 Reply Last reply
          0
          • mrjjM mrjj

            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.

            mandruk1331M Offline
            mandruk1331M Offline
            mandruk1331
            wrote on last edited by mandruk1331
            #5

            @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;
            }

            Mandruk1331

            mrjjM 1 Reply Last reply
            0
            • mandruk1331M mandruk1331

              @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;
              }

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              mandruk1331M 1 Reply Last reply
              0
              • mrjjM mrjj

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

                mandruk1331M Offline
                mandruk1331M Offline
                mandruk1331
                wrote on last edited by
                #7

                @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
                }

                Mandruk1331

                mrjjM 1 Reply Last reply
                0
                • mandruk1331M mandruk1331

                  @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
                  }

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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.

                  mandruk1331M 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @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.

                    mandruk1331M Offline
                    mandruk1331M Offline
                    mandruk1331
                    wrote on last edited by
                    #9

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

                    Mandruk1331

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved