QT:来自多个旋转框的项目列表

QT: list of item from multiple spinboxes

是否可以从每个旋转框获取值列表并将它们放入列表中?

for (int i = 0; i < norm_size+1; i++){
    getSpinleftValue(i);
}

我使用 for 循环来设置我的所有连接

.
void GuiTest::getSpinleftValue(int index){
    QSpinBox* spinleft[norm_size+1] = {ui->norm_spinBox_9,
                                       ui->norm_spinBox_10,
                                       ui->norm_spinBox_11,
                                       ui->norm_spinBox_12,
                                       ui->norm_spinBox_13,
                                       ui->norm_spinBox_14,
                                       ui->norm_spinBox_15,
                                       ui->norm_spinBox_16};
    QObject::connect(spinleft[index], SIGNAL(valueChanged(int)), this, SLOT(spinboxWrite(int, index)));
}
.

然后一旦通过 for 循环建立连接,我想将它们的输出写入一个列表以备后用。

.
void GuiTest::spinboxWrite(int arg1, int index){
    int norm_list[16];
    qDebug() << arg1;
    qDebug() << index;
}

在本例中,我使用了一些调试功能,以便查看它们是否正常工作。现在它看起来不工作,因为我没有写正确的 "connect" 部分。

No such slot GuiTest::spinboxWrite(int, index) in

我知道另一个解决方案是创建一堆这样的

void GuiTest::on_norm_spinBox_9_valueChanged(int arg1)
{
    //code here
}

但如果可以的话,我不想用 16 个污染我的整个文件!

信号 valueChanged(int) 和您的插槽 spinboxWrite(int, index)(请注意,在您的情况下索引甚至不是类型!)没有匹配的签名,因此 connect 将不起作用.来自 docs:

the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro.

我认为解决您的问题的最简单方法是将所有旋转框的 valueChanged(int) 信号连接到同一个插槽,然后使用 sender 获取已更改的旋转框,在构造函数中,您可以这样做:

GuiTest::GuiTest(QWidget* parent)/*do you initializations*/{

    //after setup ui, create your spin boxes. . .

    //get list of all children spin boxes
    //(you can replace that with a hard coded list if it is 
    //not applicable)
    QList<QSpinBox*> spinBoxes= findChildren<QSpinBox*>();
    //connect the signal from all spin boxes to the slot
    QSpinBox* spinBox;
    foreach(spinBox, spinBoxes)
        connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(SpinBoxChanged(int)));
}

这是您的 spinboxWrite 广告位的样子:

void GuiTest::SpinBoxChanged(int value){
    QSpinBox* sp= qobject_cast<QSpinBox*>(sender());

    //now sp is a pointer to the QSpinBox that emitted the valueChanged signal
    //and value is its value after the change
    //do whatever you want to do with them here. . .

}