Ale*_*han 3 c++ qt qt5 qdoublespinbox
如果 QDoubleSpinbox 中的值为正,则不显示任何符号。
如果该值更改为负数,它会自动添加“-”号。
如果前缀强制为“+”,则正数将显示为带符号
doubleSB->setPrefix("+");
Run Code Online (Sandbox Code Playgroud)
但是“+”会留在那里,当值设置为负时不会自动删除
有没有办法始终显示正确的符号?
一个可能的解决方案是覆盖该textFromValue()方法并在必要时添加该字符:
#include <QApplication>
#include <QDoubleSpinBox>
class DoubleSpinBox: public QDoubleSpinBox
{
public:
using QDoubleSpinBox::QDoubleSpinBox;
QString textFromValue(double value) const override
{
QString text = QDoubleSpinBox::textFromValue(value);
if(value > 0)
text.prepend(QChar('+'));
return text;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DoubleSpinBox w;
w.setMinimum(-100);
w.setSuffix("%");
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)