【问题标题】:Getting state of QCheckBox in a QGridLayout?在 QGridLayout 中获取 QCheckBox 的状态?
【发布时间】:2012-05-28 12:45:17
【问题描述】:

我有一个简单的布局来获取/设置 USB 板的 IO 状态。 每行对应一个模块,每列对应一个 IO 引脚。 目的是在一行上设置输出的状态,并查看输入是否与另一行上检查的输入匹配。

我这样做是为了设置布局

QLabel * templab = new QLabel;
templab ->setText(QString("Broche ->"));
gridLayout_2->addWidget(templab, 0, 0);
for (int row = 1; row < modules+1 ;row++)
{
    QLabel * templab = new QLabel;
    templab ->setText(QString("Module %1").arg(row-1));
    gridLayout_2->addWidget(templab, row, 0);
}
for (int col = 1; col < colonnes+1 ;col++)
{
    QLabel * templab = new QLabel;
    templab ->setNum(col);
    gridLayout_2->addWidget(templab, 0, col);
}
for (int row = 1; row < modules +1 ;row++)
{
    for (int col = 1; col < colonnes+1 ;col++)
    {
        QCheckBox* checkBox = new QCheckBox();
        gridLayout_2 ->addWidget(checkBox,row,col);
    }
}

如何检查复选框的状态?

我没有找到关于如何使用 gridLayout_2->itematposition(x,y) 获取复选框状态的任何线索

非常感谢。

编辑:按照 Spyke 的建议,我使用了:

QCheckBox * checkBox = findChild<QCheckBox*>(ui->gridLayout_5->itemAtPosition(x,y)->widget()->objectName());

我发现了另一种方法:

QCheckBox * ios[8][16];

 for (int row = 1; row < modules +1 ;row++)
    {
        for (int col = 1; col < colonnes+1 ;col++)
        {
            ios[row-1][col-1]= new QCheckBox();
            ui->gridLayout_5 ->addWidget(ios[row-1][col-1],row,col);
        }
    }

并测试状态:

if (ios[x][y]->checkState() == Qt::Checked)
{
    qDebug()<<"Checked"<<endl;
}
else
    qDebug()<<"UN Checked"<<endl;

}

【问题讨论】:

    标签: qt layout checkbox grid qgridlayout


    【解决方案1】:

    您可以使用gridLayout-&gt;findChild&lt;QCheckBox*&gt;(gridLayout-&gt;itemAt(0)-&gt;widget()-&gt;objectName());

    【讨论】:

    • 感谢spyke,我终于用上了:QCheckBox * checkBox = findChild(ui->gridLayout_5->itemAtPosition(x,y)->widget()->objectName());
    猜你喜欢
    • 2014-04-22
    • 2020-06-07
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多