Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:24:11+00:00 2026-06-12T00:24:11+00:00

I have QSpinBox which should take only odd numbers, so I’ve set initial value

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:24:13+00:00Added an answer on June 12, 2026 at 12:24 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
I have got a problem when I try to make following simple connections QSpinBox
Have two tables say ABC and XYZ and contain one column which data will
Have some code: using (var ctx = new testDataContext()) { var options = new
Have a simple one-off tasks which needs a progress bar. OpenSSL has a useful
Have converted devise new session from erb to Haml but doens't work, this is
Have a query that should hopefully be nice and simple to answer. I have
Have the following code: String s= v.request(engine/?key=, P4z72NmBa91&method=load); JSONParser parser = new JSONParser(); Object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.