BUG or feature ?
-
I need to output "item" to TWO widgets , however, I can see only the first one.
NO, I am not making it up.
If I reverse the order it will always show the FIRST one ONLY.
Is this some kind of ( unwanted ) optimization ?
ui->list->addItem(item); //log to pass ) ui->list_3->addItem(item); // local trace
-
Feature. Widgets take references to existing objects and a widget can only be owned by a single parent, thus following the tree model.
-
@AnneRanch said in BUG or feature ?:
Is this some kind of ( unwanted ) optimization ?
No it's not.
But you need to provide more information about
item
's datatype if you want more help to resolve this. -
@Kent-Dorfman
Basically I cannot have same "item" in two places as coded. Right ?
Could I accomplish this duplication if I use "connect" in SAME object ?
It does not look feasible if the "parent" is still same.Would same apply if I pass the "item" using "connect" to another object / parent ?
I am not sure I understand the usage of term "parent" in this case.
But the "tree" term explains a lot. -
Hi,
If you want to show the same data with two different views without duplication of items, you should stop using the convenience widget like QTableWidget or QTreeWidget and go for a single model which you assign to your two different views.
If you want to stay with the convenience widgets, then you will have to create one item for each.
-
@SGaist I would not call it convenient when the variable cannot be reused. And that is probably why I have a heck of a time using SIGNAL if it is not there in the first place.
And it really screws up my debugging / tracking too. -
The convenience of these widgets is that you do not need to manage the model. With that comes the fact that the model is not shared so each his own and thus each has its own set of items.
This is unrelated to signal and slots.
-
Maybe take a step back and think about the problem. @SGaist said it all already, but I'll try to go step by step.
First, you are using the object-oriented part of C++. Like in the real world an object cannot be in the two places at once. A widget in Qt is an object. So, the widget also cannot be in two places at once. Each widget (or the item in your case) has a position and size associated and is (usually) put into a layout. If you put it into two layouts, both will change position and size of the widget and that is why you only see it once.
Second, it's time for MVC. Specifically, you need to thing about your model (the M in MVC) and your view (the V in MVC). You can have two views for the same model. The model will store the actual data. If you then change the data inside the model, the controller informs the views of the changes so they will update. (The controller is in a simplified view just the signal/slot-connections in Qt.) For your problem it is key to separate the data and the view. Qt makes it easy by having the correct classes for this approach already. E.g.
QTableWidget
is just aQTableView
which has its own internal model. UsingQTableWidget
is actually a shortcut which is why you change the data of theQTableWidget
(which forwards the changes in the data to its internal model). Take a step back to useQTableView
instead together with your own model. Set the model for two views at once and every change to the data in the model you make will automatically update the connected views. With this approach you do not have to handle the signal/slot connections between the model and the views yourself. This would be the proper approach. It is much harder initially to grasp how to use aQ...View
together with a model instead of using the correspondingQ...Widget
directly. However, instead of trying to the theQ...Widget
new tricks it is in the end easier to properly separate the model andQ..View
s. -
Just for the record - this slot function works as expected - items gets added to both lists - no missing items.
// slot int Form_Widget::test_passLABEL(QString label ) // { qCDebug(category_debug) <<"\nTASK : " << " \n TASK receive DATA " << "\nFile : " << (__FILE__) << "\nFunction : " << (__FUNCTION__) << "\n@line : " <<(__LINE__); ui->list_2->addItem("Form real pass label **************************"); ui->list_2->addItem(label); // TRACE ui->list->addItem(label); // item long elapsedTime = timer->elapsed(); QString text = "Found bt device in " + QString::number(elapsedTime/1000) + " seconds" ; ui->list->addItem(text); //item ui->list_2->addItem(text); //TRACE }
-
@AnneRanch said in BUG or feature ?:
Just for the record - this slot function works as expected
This function works because it has created two separate QListWidgetItems with a copy of your string, one in each list.
It is equivalent to:ui->list_2->addItem( new QListWidgetItem(label) ); ui->list->addItem( new QListWidgetItem(label) ); // OR new QListWidgetItem(label, ui->list_2); new QListWidgetItem(label, ui->list);
I suggest you read the relevant documentation and compare the addItem(QString) you just used with the addItem(QListWidgetItem*) you implied you were using earlier.
-
@AnneRanch said in BUG or feature ?:
Are you saying that "item" as in "addItem" works differently when "item" is QString ?
Yes, and it is documented, if you have read the link given by @ChrisW67
void QListWidget::addItem(QListWidgetItem *item)
Warning: A QListWidgetItem can only be added to a QListWidget once. Adding the same QListWidgetItem multiple times to a QListWidget will result in undefined behavior.