简体   繁体   中英

Qt5 C++: Custom Spinbox accepting two values

I'm not really into qt but i would like to have spinbox accepting two values ie:

在此处输入图片说明

It should work like this: select value to change with mouse fe second value click arrow button and change this value.

Is there any possibility to do that, not creating a new own custom widget?

Short answer is: no

Long answer is:

You should subclass QAbstractSpinBox and implement these two virtual methods:

virtual void stepBy(int steps) override;
virtual StepEnabled stepEnabled() const override;

Keep in mind that you will need to provide your own data store and data manipulation!

The first function determines what happens when the step in either direction is requested. The negative number for steps (that tells how many steps to go in either direction) means go down and positive means up (ie clicking on the arrows of the SpinBox). QSpinBox adds the value in steps to its value (so when negative it gets subtracted) for example. Here you can also catch what part of the SpiBox's string the user selected and increment that appropriately with use of

lineEdit()->selectionStart(); 
lineEdit()->selectedText();

and when you are done you set the correct text back with:

lineEdit()->setText(myModifedValueText); //note that your internally stored value does not need to be QString, you just need to create it from your value in this method to set it to the internal QLineEdit so it can be displayed

The second method is called when the SpinBox needs to know if it can go up or down. So basically here you check the boundaries (if there are any) and return the appropriate flags ( QAbstractSpinBox::StepUpEnabled or QAbstractSpinBox::StepDownEnabled or both).

Optinally in the constructor of your SpinBox you can apply QValidator to its internal QLineEdit to accept only certain format of values when the user inputs them by hand, eg:

QRegExpValidator *validator = new QRegExpValidator(this);
validator->setRegExp(...); //create a RegExp for your value, you may use any Online regexp validator/creator for this to get the right one
lineEdit()->setValidator(validator);

Finally you can fine-tune your SpinBox to show a text when there is an invalid value or you can fix it yourself using QAbstractSpinBox::fixup and validate the input with the namesake QAbstractSpinBox::validate .

For really pimped out SpinBox you could also re-implement its context menu and actions where you first get the standard menu from QLineEdit :

QMenu *menu = lineEdit()->createStandardContextMenu();

in the QWidget::contextMenuEvent and add/modify it as you need before you show it with menu->exec(event->globalPos()) and then delete menu; .

However QAbstractSpinBox does most of the ground job for you so you really should be fine with implementing just the above two virtual methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM