Name an unknown number of widgets with a name followed by an integer
-
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. -
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??
-
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()" -
Hi
Do you mean theint numEmails = bundle->get_MessageCount(); while (i < numEmails) {
loop `?
-
qDebug() << "Opening Mailbox (inbox) was successfull";
is not doing anything - is after return
mail->setObjectName(magik);
- this has to be exactly after the creation of the object - is the NAME of the object you created -
- you create to many objects with the very same name - names have to be unique
- use something like
mail->setObjectName(magik.append(QString::number(i)));
hope that helped
-
Yes, but after, to access it with a pointer, I have to use what? magik.append(QString::number(i)))->.... ?
-
@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. -
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. ;)
-
@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. -
connect(this->findChildren<MailButton *>("mail" + QString::number(i)), SIGNAL(Processed(QString)), this, SLOT(MessageFallBack(QString)));
I can use it, so? -
@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. -
@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?
-
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); }
-
OK, thank you for the answer