Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    5
    14
    542
    Loading More Posts
    • 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
      N1coc4colA last edited by

      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 Reply Quote 0
      • A
        arsinte_andrei last edited by

        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 Reply Quote 0
        • N
          N1coc4colA last edited by

          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()"

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @N1coc4colA last edited by

            @N1coc4colA

            Hi
            Do you mean the

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

            loop `?

            1 Reply Last reply Reply Quote 0
            • A
              arsinte_andrei last edited by

              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 Reply Quote 4
              • N
                N1coc4colA last edited by

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

                mrjj 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion @N1coc4colA last edited by

                  @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 Reply Quote 3
                  • N
                    N1coc4colA last edited by

                    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. ;)

                    Pl45m4 1 Reply Last reply Reply Quote 0
                    • Pl45m4
                      Pl45m4 @N1coc4colA last edited by

                      @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 Reply Quote 1
                      • N
                        N1coc4colA last edited by

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

                        jsulm 1 Reply Last reply Reply Quote 0
                        • jsulm
                          jsulm Lifetime Qt Champion @N1coc4colA last edited by

                          @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 Reply Quote 3
                          • N
                            N1coc4colA @jsulm last edited by

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

                            Pl45m4 1 Reply Last reply Reply Quote 0
                            • Pl45m4
                              Pl45m4 @N1coc4colA last edited by Pl45m4

                              @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 Reply Quote 1
                              • N
                                N1coc4colA @Pl45m4 last edited by

                                OK, thank you for the answer

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post