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. Name an unknown number of widgets with a name followed by an integer

Name an unknown number of widgets with a name followed by an integer

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 1.1k 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.
  • N Offline
    N Offline
    N1coc4colA
    wrote on 11 Jul 2019, 03:35 last edited by
    #1

    Hello,
    I search on the web since 3 days, and I can't find the answer...
    I have an unknown number, with this integer, I create the right number of widgets using a loop and the integer. But I send data to the widget, the last widget send data and it create a bug: the first element created store the last element's data. I want to solve it by using something like an array (std::vector / std::array) or x = setObjectName("y" + QString::number(i); but after, I have some problems: I don't know how to use or it give some access problems. Have someone a simple solution or a template to help me?
    Thank you for every reading or answer, my code is available at: https://github.com/N1coc4colA/Mails , the problem is in the mainwindow.cpp. Please note the fact that MailButton don't works as it should.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      arsinte_andrei
      wrote on 11 Jul 2019, 08:17 last edited by
      #2

      sorry for my ignorance but after I had a look into your git I couldn't find the code that you speaking about... can you post it here, please. and let us know when you get the error on the creation or when you use it??

      1 Reply Last reply
      0
      • N Offline
        N Offline
        N1coc4colA
        wrote on 11 Jul 2019, 09:03 last edited by
        #3

        Look at the file "mainwindow.cpp", "mainwindow.h" and "mailbutton.*" At the root of the git. I can't past the code now, I havent my pc.
        https://github.com/N1coc4colA/Mails/blob/master/mainwindow.cpp
        Look at the end of the file, in the function "ChilkatSample()"

        M 1 Reply Last reply 11 Jul 2019, 09:13
        0
        • N N1coc4colA
          11 Jul 2019, 09:03

          Look at the file "mainwindow.cpp", "mainwindow.h" and "mailbutton.*" At the root of the git. I can't past the code now, I havent my pc.
          https://github.com/N1coc4colA/Mails/blob/master/mainwindow.cpp
          Look at the end of the file, in the function "ChilkatSample()"

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 11 Jul 2019, 09:13 last edited by
          #4

          @N1coc4colA

          Hi
          Do you mean the

           int numEmails = bundle->get_MessageCount();
          while (i < numEmails) {
          

          loop `?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            arsinte_andrei
            wrote on 11 Jul 2019, 09:30 last edited by
            #5
            qDebug() << "Opening Mailbox (inbox) was successfull";
            

            is not doing anything - is after return

            mail->setObjectName(magik);
            
            1. this has to be exactly after the creation of the object - is the NAME of the object you created -
            2. you create to many objects with the very same name - names have to be unique
            3. use something like
            mail->setObjectName(magik.append(QString::number(i)));
            

            hope that helped

            1 Reply Last reply
            4
            • N Offline
              N Offline
              N1coc4colA
              wrote on 12 Jul 2019, 12:49 last edited by
              #6

              Yes, but after, to access it with a pointer, I have to use what? magik.append(QString::number(i)))->.... ?

              M 1 Reply Last reply 12 Jul 2019, 13:05
              0
              • N N1coc4colA
                12 Jul 2019, 12:49

                Yes, but after, to access it with a pointer, I have to use what? magik.append(QString::number(i)))->.... ?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 12 Jul 2019, 13:05 last edited by
                #7

                @N1coc4colA
                Hi
                That only sets the object name.
                If you need pointers to the widgets, you should keep a list around
                or build one on demand with
                FindChildren.

                1 Reply Last reply
                3
                • N Offline
                  N Offline
                  N1coc4colA
                  wrote on 18 Jul 2019, 07:00 last edited by
                  #8

                  OK, thank you. I think I'll use the second one, Do a list is hard, prefer the second, due to the unknown number of widgets and the fact that I need to use connect(), it'll be better for me.Thanks for your help, Master of Qt, who's on every chats, I'll maybe ask you for help in the future. ;)

                  P 1 Reply Last reply 18 Jul 2019, 07:33
                  0
                  • N N1coc4colA
                    18 Jul 2019, 07:00

                    OK, thank you. I think I'll use the second one, Do a list is hard, prefer the second, due to the unknown number of widgets and the fact that I need to use connect(), it'll be better for me.Thanks for your help, Master of Qt, who's on every chats, I'll maybe ask you for help in the future. ;)

                    P Online
                    P Online
                    Pl45m4
                    wrote on 18 Jul 2019, 07:33 last edited by
                    #9

                    @N1coc4colA said in Name an unknown number of widgets with a name followed by an integer:

                    Do a list is hard

                    It's not that hard... but you have to ensure, that everything is cleaned up correctly.
                    Maybe a vector / list of QSharedPointers.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    1
                    • N Offline
                      N Offline
                      N1coc4colA
                      wrote on 18 Jul 2019, 08:01 last edited by
                      #10

                      connect(this->findChildren<MailButton *>("mail" + QString::number(i)), SIGNAL(Processed(QString)), this, SLOT(MessageFallBack(QString)));
                      I can use it, so?

                      J 1 Reply Last reply 18 Jul 2019, 08:42
                      0
                      • N N1coc4colA
                        18 Jul 2019, 08:01

                        connect(this->findChildren<MailButton *>("mail" + QString::number(i)), SIGNAL(Processed(QString)), this, SLOT(MessageFallBack(QString)));
                        I can use it, so?

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 18 Jul 2019, 08:42 last edited by
                        #11

                        @N1coc4colA said in Name an unknown number of widgets with a name followed by an integer:

                        I can use it, so?

                        No, this can't work.
                        You have to call connect for every button. You can do this in a loop.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        N 1 Reply Last reply 18 Jul 2019, 18:23
                        3
                        • J jsulm
                          18 Jul 2019, 08:42

                          @N1coc4colA said in Name an unknown number of widgets with a name followed by an integer:

                          I can use it, so?

                          No, this can't work.
                          You have to call connect for every button. You can do this in a loop.

                          N Offline
                          N Offline
                          N1coc4colA
                          wrote on 18 Jul 2019, 18:23 last edited by
                          #12

                          @jsulm, I have my loop, all the code is already in a loop. I have a loop of the number of mails, it create the MailButton I talked about. I had to change the name in depending of the no. and to use some pointers and some connect(), now it's fix. You said that I have to put it the loop, so it's right. But, as I said it should work, right?

                          P 1 Reply Last reply 18 Jul 2019, 20:32
                          0
                          • N N1coc4colA
                            18 Jul 2019, 18:23

                            @jsulm, I have my loop, all the code is already in a loop. I have a loop of the number of mails, it create the MailButton I talked about. I had to change the name in depending of the no. and to use some pointers and some connect(), now it's fix. You said that I have to put it the loop, so it's right. But, as I said it should work, right?

                            P Online
                            P Online
                            Pl45m4
                            wrote on 18 Jul 2019, 20:32 last edited by Pl45m4
                            #13

                            @N1coc4colA

                            You can not loop over this. You have to use an object with an index.

                            For example:

                            QVector <MyButtons*> m_buttons;
                            
                            for (int i = 0; i < 10; i++)
                            {
                                m_buttons.pushback(new MyButton ());
                                connect (m_buttons.at(i), &MyButton::clicked, dest, &MyDestination::DoSomething);
                            }
                            

                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            N 1 Reply Last reply 19 Jul 2019, 12:33
                            1
                            • P Pl45m4
                              18 Jul 2019, 20:32

                              @N1coc4colA

                              You can not loop over this. You have to use an object with an index.

                              For example:

                              QVector <MyButtons*> m_buttons;
                              
                              for (int i = 0; i < 10; i++)
                              {
                                  m_buttons.pushback(new MyButton ());
                                  connect (m_buttons.at(i), &MyButton::clicked, dest, &MyDestination::DoSomething);
                              }
                              
                              N Offline
                              N Offline
                              N1coc4colA
                              wrote on 19 Jul 2019, 12:33 last edited by
                              #14

                              OK, thank you for the answer

                              1 Reply Last reply
                              0

                              3/14

                              11 Jul 2019, 09:03

                              11 unread
                              • Login

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