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

发布于 2024-12-29 09:10:56 字数 1608 浏览 4 评论 0原文

我是一名学生程序员,使用 Qt 构建 GUI 来工作。我遇到了一个问题;在 QTreeWidget 中进行多项选择或多或少会带来一些不便。我的 GUI 有一个主界面,其中 QTreeWidget 作为该窗口的中心项。在 QTreeWidget 下面有几个按钮;复制、编辑和删除。您可能已经猜到这些按钮中的每一个都与执行命令的函数相关。我的树小部件能够选择多个项目;但是,当选择多个项目时,唯一传递的项目是最后选择的项目。我希望对此 IDE 有更多了解的人能够为我指明完成此任务的正确方向。以下是执行这些函数之一时遵循的过程。

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());
    }
}

我不太确定哪些代码与进行此更改相关;因此,如果有人想看到其他结构,请提出要求。我很确定我的问题或解决方案将在于我使用当前项目的方式。在查看了 Qt 网站上的文档后,我仍然不确定如何更改它以允许通过该函数传递多个选择。请仅提供建设性反馈;我只对学习和完成解决方案感兴趣。提前致谢。

更新!解决了!!! 只是觉得展示一下它的实现效果可能会很好:

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2025-01-05 09:10:56

如果您需要知道选择了哪些项目,可以使用

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

树中所有当前选定项目的列表。
然后,您可以为列表中的每个项目调用一次函数,或者可以重载函数以将 QList 作为参数,然后在被调用函数内运行列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文