sending QStringLists from dynamically created buttons using signals and slots
-
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++){
..... -
@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. -
@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" -
@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.
-
@Kushan
just
delete dialog2;
dialog2->deletelater();
(that also delete all you put into it)And BEFORE using it again
dialog2 = new Dialog2(); // new it againNote:
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