Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Loop mathematics



  • I got a combo box representing values from 50-90 in a combo.

    However the combo lists every value between those two numbers e.i. 50,51,52,53,54.....

    How can i modify my code to have the box only show multiples of 5.

    This is my code at the moment

    for (int i = 50; i < 91; i++)
    {
        ui->comboBox_Alert->addItem(QString::number(i) + "%");
    }


  • I'd use a QSpinBox and setting the "singleStep" property accordingly.

    But if you want a Combobox, you could just modify your code like:
    @for (int i = 50; i <= 90; i += 5)
    {
    ui->comboBox_Alert->addItem(QString::number(i) + “%”);
    }@



  • i'm using a combo box but i'm sure that shouldn't change much.

    I'm very beginner at this i would not know where to start implementing that command



  • [quote author="davidrhcp" date="1399296936"]i'm using a combo box but i'm sure that shouldn't change much.[/quote]

    It does! The QSpinBox is made specifically for entering numbers. And it has a configurable “singleStep” property. Should be exactly what you need :-)

    See here:
    http://qt-project.org/doc/qt-4.8/qspinbox.html

    [quote author="davidrhcp" date="1399296936"]I'm very beginner at this i would not know where to start implementing that command[/quote]

    What command?



  • thanks, was a great help :)


Log in to reply