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

sending QStringLists from dynamically created buttons using signals and slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatorsignal & slotdynamicbuttons
21 Posts 4 Posters 6.9k Views
  • 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 mrjj
    5 Dec 2017, 16:14

    @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 5 Dec 2017, 16:31 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

    M 1 Reply Last reply 5 Dec 2017, 16:34
    0
    • K Kushan
      5 Dec 2017, 16:31

      @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

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Dec 2017, 16:34 last edited by mrjj 12 May 2017, 16:36
      #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 5 Dec 2017, 16:45
      0
      • M mrjj
        5 Dec 2017, 16:34

        @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 5 Dec 2017, 16:45 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"

        M 1 Reply Last reply 5 Dec 2017, 16:56
        0
        • K Kushan
          5 Dec 2017, 16:45

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 5 Dec 2017, 16:56 last edited by mrjj 12 May 2017, 17:01
          #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 5 Dec 2017, 17:01
          0
          • M mrjj
            5 Dec 2017, 16:56

            @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 5 Dec 2017, 17:01 last edited by
            #15

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

            M 1 Reply Last reply 5 Dec 2017, 17:03
            0
            • K Kushan
              5 Dec 2017, 17:01

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 5 Dec 2017, 17:03 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 5 Dec 2017, 17:13
              0
              • M mrjj
                5 Dec 2017, 17:03

                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 5 Dec 2017, 17:13 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?

                M 1 Reply Last reply 5 Dec 2017, 17:29
                0
                • K Kushan
                  5 Dec 2017, 17:13

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 5 Dec 2017, 17:29 last edited by mrjj 12 May 2017, 20:48
                  #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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 5 Dec 2017, 20:31 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

                    M K 2 Replies Last reply 5 Dec 2017, 20:46
                    1
                    • S SGaist
                      5 Dec 2017, 20:31

                      In the case mentioned by @mrjj , use deleteLater.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 5 Dec 2017, 20:46 last edited by
                      #20

                      @SGaist
                      doh, of course.
                      Thanks

                      1 Reply Last reply
                      0
                      • S SGaist
                        5 Dec 2017, 20:31

                        In the case mentioned by @mrjj , use deleteLater.

                        K Offline
                        K Offline
                        Kushan
                        wrote on 6 Dec 2017, 02:17 last edited by
                        #21

                        @SGaist Thanx mate :) Qt rox :D

                        1 Reply Last reply
                        0

                        20/21

                        5 Dec 2017, 20:46

                        • Login

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