如何在 QSpinBox 中绘制破折号而不是空值?
How to draw dash instead of null in QSpinBox?
我需要在 QSpinBox
中绘制破折号而不是空值。我还需要使短划线键按下等于空键按下。
我该怎么做?
可以使用setSpecialValueText()
;
QSpinBox spinBox;
spinBox->setSpecialValueText(tr("-"));
然后您可以通过连接valueChanged(QString)
函数来检查是否选择了特殊值。注意这和valueChanged(int)
不同,然后可以在一个slot中检查传递的字符串的值,如果等于特殊文本,就可以做一些事情了。
main()
{
connect(spinBox, SIGNAL(valueChanged(QString)), this, SLOT(doSomething(QString)));
}
void doSomething(QString valueStr)
{
if(valueStr == spinBox->specialValueText())
// Do something
else
//Convert valueStr to int and do other stuff
}
或者你可以这样做:
main()
{
connect(spinBox, SIGNAL(valueChanged()), this, SLOT(doSomething()));
}
void doSomething()
{
if(spinBox->value() == 0)
// Do something with dash
else
//Do something with the value
}
对于您的其他问题,您需要创建一个keyPressEvent 并检查按下的键是否为破折号。如果它是破折号,你可以调用另一个函数来做某事。编辑:顺便说一句,specialValueText()
的索引是 0。
编辑: 或者您可以在主函数中创建一个QShortcut
。
new QShortcut(QKeySequence(Qt::Key_Minus), this, SLOT(doSomething()));
继续编辑:doSomething() 是一个槽函数。例如,将 void doSomething();
放入 header 文件的 private slots:
部分。并在 cpp 文件中定义一个类似这样的函数:
void MainWindow::doSomething()
{
ui->spinBox->setValue(0);
//This is the slot called when you press dash.
}
编辑仍在继续:
您需要像这样在 header 中声明一个 protected:
函数:
virtual void keyPressEvent(QKeyEvent *event);
那么你需要在你的cpp文件中定义这个函数。像这样:
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Minus)
ui->spinBox->setValue(0);
}
您不必为此功能连接任何信号或插槽。这是一个事件。
这意味着当按下破折号时ui->spinBox->setValue(0);
因此,您需要创建一个范围从 0 开始的旋转框。
spinBox->setRange(0, 100);
也就是说,
if(spinBox->value() == 0)
//Then specialValueText is selected.
我需要在 QSpinBox
中绘制破折号而不是空值。我还需要使短划线键按下等于空键按下。
我该怎么做?
可以使用setSpecialValueText()
;
QSpinBox spinBox;
spinBox->setSpecialValueText(tr("-"));
然后您可以通过连接valueChanged(QString)
函数来检查是否选择了特殊值。注意这和valueChanged(int)
不同,然后可以在一个slot中检查传递的字符串的值,如果等于特殊文本,就可以做一些事情了。
main()
{
connect(spinBox, SIGNAL(valueChanged(QString)), this, SLOT(doSomething(QString)));
}
void doSomething(QString valueStr)
{
if(valueStr == spinBox->specialValueText())
// Do something
else
//Convert valueStr to int and do other stuff
}
或者你可以这样做:
main()
{
connect(spinBox, SIGNAL(valueChanged()), this, SLOT(doSomething()));
}
void doSomething()
{
if(spinBox->value() == 0)
// Do something with dash
else
//Do something with the value
}
对于您的其他问题,您需要创建一个keyPressEvent 并检查按下的键是否为破折号。如果它是破折号,你可以调用另一个函数来做某事。编辑:顺便说一句,specialValueText()
的索引是 0。
编辑: 或者您可以在主函数中创建一个QShortcut
。
new QShortcut(QKeySequence(Qt::Key_Minus), this, SLOT(doSomething()));
继续编辑:doSomething() 是一个槽函数。例如,将 void doSomething();
放入 header 文件的 private slots:
部分。并在 cpp 文件中定义一个类似这样的函数:
void MainWindow::doSomething()
{
ui->spinBox->setValue(0);
//This is the slot called when you press dash.
}
编辑仍在继续:
您需要像这样在 header 中声明一个 protected:
函数:
virtual void keyPressEvent(QKeyEvent *event);
那么你需要在你的cpp文件中定义这个函数。像这样:
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Minus)
ui->spinBox->setValue(0);
}
您不必为此功能连接任何信号或插槽。这是一个事件。
这意味着当按下破折号时ui->spinBox->setValue(0);
因此,您需要创建一个范围从 0 开始的旋转框。
spinBox->setRange(0, 100);
也就是说,
if(spinBox->value() == 0)
//Then specialValueText is selected.