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.4k 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.
  • M Offline
    M Offline
    mandruk1331
    wrote on 26 Mar 2016, 15:55 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 26 Mar 2016, 16:19 last edited by
      #2

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

      M 1 Reply Last reply 26 Mar 2016, 16:43
      0
      • M mrjj
        26 Mar 2016, 16:19

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

        M Offline
        M Offline
        mandruk1331
        wrote on 26 Mar 2016, 16:43 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
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 26 Mar 2016, 19:35 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.

          M 1 Reply Last reply 27 Mar 2016, 00:14
          0
          • M mrjj
            26 Mar 2016, 19:35

            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.

            M Offline
            M Offline
            mandruk1331
            wrote on 27 Mar 2016, 00:14 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

            M 1 Reply Last reply 27 Mar 2016, 00:57
            0
            • M mandruk1331
              27 Mar 2016, 00:14

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 27 Mar 2016, 00:57 last edited by
              #6

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

              M 1 Reply Last reply 27 Mar 2016, 01:11
              0
              • M mrjj
                27 Mar 2016, 00:57

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

                M Offline
                M Offline
                mandruk1331
                wrote on 27 Mar 2016, 01:11 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

                M 1 Reply Last reply 27 Mar 2016, 09:06
                0
                • M mandruk1331
                  27 Mar 2016, 01:11

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 27 Mar 2016, 09:06 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.

                  M 1 Reply Last reply 27 Mar 2016, 09:48
                  0
                  • M mrjj
                    27 Mar 2016, 09:06

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

                    M Offline
                    M Offline
                    mandruk1331
                    wrote on 27 Mar 2016, 09:48 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

                    1/9

                    26 Mar 2016, 15:55

                    • Login

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