gpt4 book ai didi

c++ - Qt5 C++ 如何访问模型中的项目以检查状态?

转载 作者:行者123 更新时间:2023-11-28 07:28:09 27 4
gpt4 key购买 nike

我有一个包含多个复选框项目的组合框,我需要能够遍历它们并确定它们是否被选中,或者更合适的是,一旦选中一个项目,就触发一个事件,但我不能似乎让信号/插槽组合正常工作。

这是我的一些代码:

void MainWindow::setupQueryBuilder(Ui::MainWindow* ui)
{
QStandardItemModel* assessmentModel = new QStandardItemModel(3, 1);
QStandardItem* assessmentPleaseSelect = new QStandardItem();
QStandardItem* assessmentVisionScreening = new QStandardItem();
QStandardItem* assessmentHearingAssessment = new QStandardItem();
QStandardItem* assessmentDevelopmentalAssessment = new QStandardItem();

assessmentPleaseSelect->setText("Please Select...");
assessmentPleaseSelect->setFlags(Qt::ItemIsEnabled);
assessmentPleaseSelect->setData(Qt::CheckStateRole);

assessmentVisionScreening->setText("Vision Screening");
assessmentVisionScreening->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
assessmentVisionScreening->setData(Qt::Unchecked, Qt::CheckStateRole);

assessmentHearingAssessment->setText("Hearing Assessment");
assessmentHearingAssessment->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
assessmentHearingAssessment->setData(Qt::Unchecked, Qt::CheckStateRole);

assessmentDevelopmentalAssessment->setText("Developmental Assessment");
assessmentDevelopmentalAssessment->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
assessmentDevelopmentalAssessment->setData(Qt::Unchecked, Qt::CheckStateRole);

assessmentModel->insertRow(0, assessmentPleaseSelect);
assessmentModel->insertRow(1, assessmentVisionScreening);
assessmentModel->insertRow(2, assessmentHearingAssessment);
assessmentModel->insertRow(3, assessmentDevelopmentalAssessment);

ui->SearchAssessmentSelect->setModel(assessmentModel);
}

最佳答案

我没有试过这个,但是通过查看文档,你有一个 signal 用于 QStandardItemModel 称为 itemChanged(QStandardItem *item) .您想要做的是创建一个名为(例如)modelItemChanged(QStandardItem *item)slot 并将它们connect connect(assessmentModel, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(modelItemChanged(QStandardItem *))).

这应该将您的模型设置为在 QStandardItem 更改时触发 slot,并且它会传递一个指向已更改项目的指针。在您的 modelItemChanged(QStandardItem *item) 函数中,您可以查询 QStandardItem *item 以查看它处于什么状态。

关于c++ - Qt5 C++ 如何访问模型中的项目以检查状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18329076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com