简体   繁体   English

Qt5 C ++:自定义Spinbox接受两个值

[英]Qt5 C++: Custom Spinbox accepting two values

I'm not really into qt but i would like to have spinbox accepting two values ie: 我不是很喜欢qt,但是我想让Spinbox接受两个值,即:

在此处输入图片说明

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: 您应该QAbstractSpinBox并实现这两个虚拟方法:

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). 步骤的负数(表示在任一方向上走了多少步)表示下降,而正数表示上升(即单击SpinBox的箭头)。 QSpinBox adds the value in steps to its value (so when negative it gets subtracted) for example. QSpinBox会将值steps添加到其值中(因此当它为负数时将被减去)。 Here you can also catch what part of the SpiBox's string the user selected and increment that appropriately with use of 在这里,您还可以捕获用户选择的SpiBox字符串的哪一部分,并使用

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. 当SpinBox需要知道它可以上升还是下降时,将调用第二种方法。 So basically here you check the boundaries (if there are any) and return the appropriate flags ( QAbstractSpinBox::StepUpEnabled or QAbstractSpinBox::StepDownEnabled or both). 因此,基本上,您在这里检查边界(如果有的话)并返回适当的标志( QAbstractSpinBox::StepUpEnabledQAbstractSpinBox::StepDownEnabled或两者)。

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: 最好在SpinBox的构造函数中,可以将QValidator应用于其内部QLineEdit以在用户手动输入值时仅接受某些格式的值,例如:

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 . 最后,您可以微调SpinBox以在出现无效值时显示文本,也可以使用QAbstractSpinBox::fixup对其进行修复,并使用名称为QAbstractSpinBox::fixup validate的输入进行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 : 为了真正启动SpinBox,您还可以重新实现其上下文菜单和操作,您首先从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; QWidget::contextMenuEvent并根据需要添加/修改它,然后使用menu->exec(event->globalPos())显示它,然后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. 但是QAbstractSpinBox为您完成了大部分基础工作,因此仅实现上述两个虚拟方法就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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