Solved QListWidget: DragDrop event Signal
-
I setted a QListWidget property as:
ui->mylist->setDragDropMode(QAbstractItemView::InternalMove);
Now I can drag and drop the internal list items. Ok!
I would like to know how to get a Qt signal when this internal dragdrop event occurs.
I look in the official documentation but I'm not founding this signal event:
https://doc.qt.io/qt-5/qlistwidget.htmlIs this possible?
-
Hi,
Because it doesn't exist.
What would you like to do on drop ?
By the way, did you already read the drag and drop chapter for item views in Qt's documentation ?
-
@sgaist thanks for the information, but now I don't know how to implement my idea:
I have a main application called "Job Manager" and it loads my custom developed plugins.
For now, I called these plugins: "Analysis X", "Analysis Y" and "Analysis Z".The input data for each plugin is a custom QJsonObject with diferent fields and values.
I would like that the user choose your plugin sequence and write your input data for each plugin in independent way.
So, here is a exemple:
The user can select a "Avaliable plugin" and add it to the "Added Analysis"
QListWidget
.My problem is related with how can I store/manage the individual
QJsonObjects
to send the correct input data for each "added" plugin in the list.Below, I did a exemple that the user adds 5 times the same plugin "Analysis X", but he wants to set a diferent
QJsonObject
for each "Analysis X" instance.
After this add process, the user will get something like this:
You can see in the Qt Creator IDE console that the QJsonObject is the same for each instance.
I store these configurations in a
QMap<int, QJsonObject>
, where thekey
is theQListWidgetItem
index and theQJsonObject
is the custom configuration (GUI input) for each plugin instance.My problem is:
If I modify the values of each plugin instance (set different values for the "name" field) AND dragdrop (reordering usingdradropMode = InternalMove
) the QListWidgetItems I loose the relationship between the Item index and theQJsonObject
input data.Look this image:
In this case, I will run the "Analysis X" plugin 5 times sequentially, but the
QJsonObject
input data for each instance is wrong now!So I would like to get something like a "Qt Signal" when the users drapdrop items (reordering using
dradropMode = InternalMove
) the QListWidget to match the Item index and theQJsonObejct
stored in theQMap<int, QJsonObejct
.Just the
QMap
store section of my C++ code:void job_manager::add_item() { // ===== JSON SECTION ===== // Get it on QMap: auto plugin = _plugin_map[selected_item]; // Get plugin input variables: QJsonObject input = plugin->get_input_variables(); // Insert the index (int) and input data (QJsonObject) in a QMap _properties->insert(id, input); }
Coul you help me?
-
Well, I solved this missing Qt signal replacing the
drapdrop
mouse operation by 2 buttons: "move item to up" and "move item to down".Here is a example of "move item to up" implementation:
void MainWindow::on_up_btn_clicked() { int current_id = ui->add_list->currentRow(); int target_id = current_id - 1; // Error checking: first element if (current_id > 0) { // Get the current item text: QString current_item = ui->add_list->item(current_id)->text(); // Get the current QJsonObject: QJsonObject current_json = my_array->at(current_id).toObject(); // Get the target item text: QString target_item = ui->add_list->item(target_id)->text(); // Get the target QJsonObject: QJsonObject target_json = my_array->at(target_id).toObject(); // Store a temp QJsonObject to swap: QJsonObject temp = target_json; // Replace items text: ui->add_list->item(target_id)->setText(current_item); ui->add_list->item(current_id)->setText(target_item); // Replace QJsonObjects: my_array->replace(current_id, temp); my_array->replace(target_id, current_json); // Set current row: ui->add_list->setCurrentRow(target_id); } }
-
A more generic solution could be:
void swap_items(QListWidget* list, QJsonArray* array, int current_id, int target_id) { // Get the current item text: QString current_item = list->item(current_id)->text(); // Get the current QJsonObject: QJsonObject current_json = array->at(current_id).toObject(); // Get the target item text: QString target_item = list->item(target_id)->text(); // Get the target QJsonObject: QJsonObject target_json = array->at(target_id).toObject(); // Store a temp QJsonObject to swap: QJsonObject temp = target_json; // Replace items text: list->item(target_id)->setText(current_item); list->item(current_id)->setText(target_item); // Replace QJsonObjects: array->replace(current_id, temp); array->replace(target_id, current_json); // Set current row: list->setCurrentRow(target_id); }
-
One other possibility would be to implement your own QAbstractListModel on top of your object list. That way you would directly manipulate the structure without additional back and forth of your data.