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. sending QStringLists from dynamically created buttons using signals and slots
Forum Updated to NodeBB v4.3 + New Features

sending QStringLists from dynamically created buttons using signals and slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatorsignal & slotdynamicbuttons
21 Posts 4 Posters 7.2k Views 2 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.
  • K Kushan

    @mrjj yeah but how can I change my code so that only
    text is "John"
    text is "Smith"

    appears when I click 1st button. and
    text is "Hello"
    text is "Anne"
    appear when I click the button

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

    @Kushan

    You seems to connect multiple times to same dialog2

    for(int i=0;i<subMessages.size();i++){
    ....
    connect(this,SIGNAL(sendList(QStringList)),&dialog2,SLOT(receiveList(QStringList)));

    When you then call emit it will call that many times, so i think you mean just to call once
    out side the loop.


    connect(this,SIGNAL(sendList(QStringList)),&dialog2,SLOT(receiveList(QStringList)));
    for(int i=0;i<subMessages.size();i++){
    .....

    K 1 Reply Last reply
    1
    • mrjjM mrjj

      @Kushan

      You seems to connect multiple times to same dialog2

      for(int i=0;i<subMessages.size();i++){
      ....
      connect(this,SIGNAL(sendList(QStringList)),&dialog2,SLOT(receiveList(QStringList)));

      When you then call emit it will call that many times, so i think you mean just to call once
      out side the loop.


      connect(this,SIGNAL(sendList(QStringList)),&dialog2,SLOT(receiveList(QStringList)));
      for(int i=0;i<subMessages.size();i++){
      .....

      K Offline
      K Offline
      Kushan
      wrote on last edited by
      #11

      @mrjj yeah thanx but the problem is if I place it outside then I can't connect the signal to a particular button right? Here I connect the signal using the value of i in the for loop

      mrjjM 1 Reply Last reply
      0
      • K Kushan

        @mrjj yeah thanx but the problem is if I place it outside then I can't connect the signal to a particular button right? Here I connect the signal using the value of i in the for loop

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

        @Kushan
        Only for the dialog2 as you dont create new dialog2 and hence get multiple
        connect to same.

        for the LE button , inside loop is correct. ( as you find new one to connect to )

        So only call
        connect(this,SIGNAL(sendList(QStringList)),&dialog2,SLOT(receiveList(QStringList)));
        ONCE or it will send many times to same dialog.

        K 1 Reply Last reply
        0
        • mrjjM mrjj

          @Kushan
          Only for the dialog2 as you dont create new dialog2 and hence get multiple
          connect to same.

          for the LE button , inside loop is correct. ( as you find new one to connect to )

          So only call
          connect(this,SIGNAL(sendList(QStringList)),&dialog2,SLOT(receiveList(QStringList)));
          ONCE or it will send many times to same dialog.

          K Offline
          K Offline
          Kushan
          wrote on last edited by
          #13

          @mrjj Thanx alot! now it is 80% working! But the only problem I face now is when I click the 1st button close the dialog2.ui which pops up and click the second button and open dialog2.ui it is like

          text is "John"
          text is "Smith"
          text is "Hello"
          text is "Anne"

          How can I remove the following part?
          text is "John"
          text is "Smith"

          mrjjM 1 Reply Last reply
          0
          • K Kushan

            @mrjj Thanx alot! now it is 80% working! But the only problem I face now is when I click the 1st button close the dialog2.ui which pops up and click the second button and open dialog2.ui it is like

            text is "John"
            text is "Smith"
            text is "Hello"
            text is "Anne"

            How can I remove the following part?
            text is "John"
            text is "Smith"

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

            @Kushan
            If you reuse same dialog2 for button1 AND button2, it will still have the Labels from last time.
            I think most easy would be to delete it and create it again.
            (its not super easy to clear a layout)

            I assume its this one ?
            Dialog2 dialog2;

            its a bit hard to delete as its not pointer.

            you could use

            void clearLayout(QLayout *layout) 
            {
                if (layout) {
                    while(layout->count() > 0){
                        QLayoutItem *item = layout->takeAt(0);
                        QWidget* widget = item->widget();
                        if(widget)
                            delete widget;
                        delete item;
                    }
                }
            }
            

            void Dialog2::receiveList(QStringList List){
            clearLayout(ui->verticalLayout);
            ....

            So each time you call Dialog2::receiveList it removes what it has and then add the new stuff.

            K 1 Reply Last reply
            0
            • mrjjM mrjj

              @Kushan
              If you reuse same dialog2 for button1 AND button2, it will still have the Labels from last time.
              I think most easy would be to delete it and create it again.
              (its not super easy to clear a layout)

              I assume its this one ?
              Dialog2 dialog2;

              its a bit hard to delete as its not pointer.

              you could use

              void clearLayout(QLayout *layout) 
              {
                  if (layout) {
                      while(layout->count() > 0){
                          QLayoutItem *item = layout->takeAt(0);
                          QWidget* widget = item->widget();
                          if(widget)
                              delete widget;
                          delete item;
                      }
                  }
              }
              

              void Dialog2::receiveList(QStringList List){
              clearLayout(ui->verticalLayout);
              ....

              So each time you call Dialog2::receiveList it removes what it has and then add the new stuff.

              K Offline
              K Offline
              Kushan
              wrote on last edited by
              #15

              @mrjj Thanx when I delete the dialog2.ui and click the 2nd button nothing gets opened

              mrjjM 1 Reply Last reply
              0
              • K Kushan

                @mrjj Thanx when I delete the dialog2.ui and click the 2nd button nothing gets opened

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

                What you mean delete dialog2.ui ? ( ui files are like cpp/.h files)

                is it not the variable in Dialog called Dialog2 dialog2 u are using ?

                K 1 Reply Last reply
                0
                • mrjjM mrjj

                  What you mean delete dialog2.ui ? ( ui files are like cpp/.h files)

                  is it not the variable in Dialog called Dialog2 dialog2 u are using ?

                  K Offline
                  K Offline
                  Kushan
                  wrote on last edited by
                  #17

                  @mrjj Thanx alot it works fine! I am also interested in using pointers!

                  if I had used Dialog2 *dialog2 = new Dialog2(); instead of Dialog Dialog2 what should I do without using the clearLayout method?

                  mrjjM 1 Reply Last reply
                  0
                  • K Kushan

                    @mrjj Thanx alot it works fine! I am also interested in using pointers!

                    if I had used Dialog2 *dialog2 = new Dialog2(); instead of Dialog Dialog2 what should I do without using the clearLayout method?

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

                    @Kushan
                    just
                    delete dialog2;
                    dialog2->deletelater();
                    (that also delete all you put into it)

                    And BEFORE using it again
                    dialog2 = new Dialog2(); // new it again

                    Note:
                    Qt has a auto delete system. So any parent will delete its children so
                    dont delete a button u have given to a layout.
                    http://doc.qt.io/qt-5/objecttrees.html
                    It works here as you dont give the dialog a parent.

                    if you did
                    Dialog2 *dialog2 = new Dialog2(this); "this" would own it and
                    it would be bad to delete it manually as when ever "this" is deleted it would
                    try to delete dialog. ( a second time)

                    update
                    Also
                    you can use
                    setAttribute(Qt::WA_DeleteOnClose);
                    which will auto delete dialog when you press close/ok/cancel
                    and in that case , you must new it before use each time and it will
                    delete itself

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #19

                      In the case mentioned by @mrjj , use deleteLater.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      mrjjM K 2 Replies Last reply
                      1
                      • SGaistS SGaist

                        In the case mentioned by @mrjj , use deleteLater.

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

                        @SGaist
                        doh, of course.
                        Thanks

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          In the case mentioned by @mrjj , use deleteLater.

                          K Offline
                          K Offline
                          Kushan
                          wrote on last edited by
                          #21

                          @SGaist Thanx mate :) Qt rox :D

                          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