Question

I am a student programmer using Qt to build a GUI for work. I have ran into an issue; more or less and inconvience of sorts wiith multiple selections in the QTreeWidget. My GUI has a main interface with a QTreeWidget as the central item in this window. Below the QTreeWidget I have several buttons; copy, edit, and delete. As you might've already guessed each one of these buttons correlates to a function that executes the command. My tree widget has the ability to select multiple items; however, when multiple items are selected the only item that is passed through is the last item that was selected. I was hoping that somebody with some more insight in this IDE might be able to point me in the right direction for accomplishing this. Here is the process that is followed when one of these functions is executed.

void InjectionGUI::copyInjection_Clicked(QTreeWidgetItem *itemToCopy)
{
    InjectionData copyInjectionData;         //first use data from the tree widget row
    QString converter = itemToCopy->text(0); //to find the vector item that will be copied
    int id = converter.toInt();
    int nameNumber;
    copyInjectionData = qTreeInjectionData.at(id);
    qTreeInjectionData.append(copyInjectionData);
    buildTreeWidget();
}

void InjectionGUI::slotInjectionCopy()
{
    if(ui->treeWidgetInjections->currentItem() == 0)
    {
        QMessageBox invalidSelection;
        invalidSelection.setText("No row selected to copy");
        invalidSelection.setWindowTitle("Error");
        invalidSelection.exec();
    }
    else
    {
        copyInjection_Clicked(ui->treeWidgetInjections->currentItem());
    }
}

I'm not too sure what code will be relevant towards making this change; so if there is additional structure that anyone would like to see please just requested. I'm pretty sure that my problem or my solution is going to lie in the way that I'm using current item. After reviewing the documentation from Qt's website I'm still unsure how I would change this to allow multiple selections to be passed through the function. Please only provide constructive feedback; I'm only interested in learning and accomplishing a solution. Thanks in advance.

UPDATE! SOLVED!!! Just thought it might be nice to show what this looked like implemented:

QList<QTreeWidgetItem *> items = ui->treeWidgetInjections->selectedItems();
for(int i = 0; i < items.size(); i++)
{
    QTreeWidgetItem *qTreeWidgetitem = new QTreeWidgetItem;
    qTreeWidgetitem = items.at(i);
    copyInjection_Clicked(qTreeWidgetitem);
}
Was it helpful?

Solution

If you need to know which items are selected, you can use

QList<QTreeWidgetItem *> QTreeWidget::selectedItems() const

to have a list of all the currently selected items in the tree. Then you may call your function once for every item in the list, or you can overload your function to take as argument a QList<QTreeWidgetItem *> and then run through the list inside the called function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top