简体   繁体   English

QTreeWidget通过一个函数传递多个项目(多个选择)

[英]QTreeWidget Passing multiple Items (more then one selection) through a function

I am a student programmer using Qt to build a GUI for work. 我是使用Qt来构建工作GUI的学生程序员。 I have ran into an issue; 我遇到了一个问题; more or less and inconvience of sorts wiith multiple selections in the QTreeWidget. 与QTreeWidget中的多个选择相比,排序或多或少的不便。 My GUI has a main interface with a QTreeWidget as the central item in this window. 我的GUI具有一个主界面,该界面以QTreeWidget为中心。 Below the QTreeWidget I have several buttons; 在QTreeWidget下面,我有几个按钮。 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. 我希望对IDE具有更深入了解的人能够为我指出完成此任务的正确方向。 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. 在查看了Qt网站的文档之后,我仍然不确定如何更改此设置以允许通过功能传递多个选择。 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);
}

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. 然后,您可以为列表中的每个项目调用一次函数,或者可以重载函数以将QList<QTreeWidgetItem *>用作参数,然后在被调用函数内部遍历列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM