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

时间:2013-08-20 07:16:54

标签: c++ qt checkbox qstandarditemmodel

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

以下是我的一些代码:

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

1 个答案:

答案 0 :(得分:1)

我没有试过这个,但是通过查看文档signal QStandardItemModel名为itemChanged(QStandardItem *item)。您要做的是创建一个名为slot的{​​{1}} modelItemChanged(QStandardItem *item)connectconnect(assessmentModel, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(modelItemChanged(QStandardItem *)))

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