Domanda

I have QSpinBox which should take only odd numbers, so I've set initial value to 3 and step to 2.

QSpinBox* spinBox = new QSpinBox;
spinBox->setValue(3);
spinBox->setSingleStep(2);

When I'm using spin box arrows to modify value everything is ok. But when I input value from keyboard it can take not odd numbers to.

So is it possible to set validation which fulfills my requirements without inheriting QSpinBox and redefining its validate method?

My current solution is checking in slot if the value is odd:

void MyWidget::slotSetSpinBoxValue(int value)
{
    if(value%2 != 0)
    {
         //call function which takes only odd values
    }
    else
    {
        //here I want to show some kind off message that value can only be odd
        //call function with --value parameter
    }
}

Second question is how to show some tip for QSpinBox? I would like to show tip like tool tip is shown with message that QSpinBox value should be odd. I've found statusTip property in QWidget but cant find example how to use it.

È stato utile?

Soluzione

Well you can make a workaround using the valueChanged() slot:

void MainWindow::on_spinBox_valueChanged(int arg1)
{
    if( arg1 % 2 == 0)
    {
        //for even values, show a message 
        QMessageBox b;
        b.setText("Only odd values allowed!");
        b.exec();
        //and then decrease the value to make it odd 
        ui.spinBox->setValue( arg1 - 1 );
    }
}

Now if you want to keep the old value in case the used enters an even number, you will have to either inherit from QSpinBox, or use an event filter to catch key press events, and act before the value gets changed.

To show the message when the user hovers his/her mouse over the spinbox, you will need to set the box's toolTip, which holds the string that will be shown:

spinbox with tooltip

UPDATE: If you don't want a message box, you can:

  • use QStatusBar. It can display messages which only last for some amount of time (that you pass it). The downside of it is that the message will appear on the bar on the bottom of the window, instead of being close to the spinbox.
  • Place a label under the spinbox. Set the label's text to something like "only odd values are allowed" when the user enters an invalid value, and set an empty string when the user enters a good value. You could also do this dynamically:
    1. The user inputs a wrong value
    2. Create a QLabel with the warning text, and set Qt::WA_DeleteOnClose flag, so the label will delete itself when closed.
    3. Create a QTimer with singleShot, and set it to fire after a couple of seconds (when you want the message to dissapear)
    4. Connect the timer's signal to the label's close() slot. When the timer will expire, the label will be closed, and, thanks to WA_DeleteOnClose, will be deleted.

Altri suggerimenti

You can connect to the editingFinished signal and fix it:

void Obj::onSpinEditFinished()
{
    int val = ui->spinPoints->value();
    if(val % 2 == 0)
        ui->spinPoints->setValue(val-1);
}

With respect to Richard's comment, I think that ctrl->setKeyboardTracking(false) would get around the checking that would otherwise happen on each keystroke, and allow the validation only to happen at the end.

I think the correct answer doesn't work perfectly. It makes you unable to input values like "12", because the value changed singal will be triggered when "1" was input and it will be corrected to "0" as 1 is an odd numer.

The fix could be using a timer to correct the values in the spinbox. E.g. we restart a timer with (500ms) timout once we received the valueChanged signal(The timer will only triggered once if you type quickly enough). And we check and correct the input in the timers timeout slot.

In python I solved this as follows using the editingFinished property of the spinbox. This only fires a signal when you hit enter or move the focus away from the box. In python I had to pass the function references or the value passed is not the updated one but the one at initialization.:

class SpinboxExample:
    def __init__(self)
        spinbox = QSpinBox()
        spinbox.editingFinished.connect(
            lambda value=spinbox.value, set_spinbox_val=spinbox.setValue:
            self.value_changed_spinbox(value,set_spinbox_val, set_slider_val)
            )

I then had a callback function which checked whether number was odd and if not altered the value in the spinbox to make it odd.

    def value_changed_spinbox(self, get_value,set_spinbox_value):
        value=get_value()
        if value % 2 == 0:
            value += 1
            set_spinbox_value(value)

Hope that helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top