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

QT: list of item from multiple spinboxes

本文关键字:项目 列表 旋转 QT      更新时间:2023-10-16

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

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 个污染我的整个文件!

信号valueChanged(int)和插槽spinboxWrite(int, index)(请注意,索引在您的情况下甚至不是一种类型!)没有匹配的签名,因此connect不起作用。从文档中:

传递给 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. . .
}