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

时间:2016-07-05 19:10:40

标签: c++ qt signals-slots

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

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

在这种情况下,我正在使用一些调试功能,所以我可以看看它们是否正常工作。现在它看起来不像它正在工作,因为我没有正确地写“连接”部分。

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

我知道另一种解决方案是创建一堆这些

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

但如果我能提供帮助,我宁愿不用 16 来污染我的整个文件!

1 个答案:

答案 0 :(得分:1)

信号valueChanged(int)和您的广告位spinboxWrite(int, index)(请注意索引在您的情况下甚至不是类型!)没有匹配的签名,因此connect无法工作。来自docs

  

传递给SIGNAL()宏的签名的参数必须少于传递给SLOT()宏的签名。

我认为解决问题的最简单方法是将所有旋转框中的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. . .

}
相关问题