2

I need to draw dash instead of null in QSpinBox. Also I need to make dash key pressing equalling null key pressing.

How can I do this?

Ufx
  • 2,595
  • 12
  • 44
  • 83

1 Answers1

4

You can use setSpecialValueText();

 QSpinBox spinBox;
 spinBox->setSpecialValueText(tr("-"));

You can then check if the special value is selected by connecting valueChanged(QString) function. Note that this is different from valueChanged(int) You can then check the value of the passed string in a slot, and if it is equal to special text, you can do something.

 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
 }

Or you could do something like this:

 main()
 {
      connect(spinBox, SIGNAL(valueChanged()), this, SLOT(doSomething()));
 }

 void doSomething()
 {
     if(spinBox->value() == 0)
           // Do something with dash
     else
           //Do something with the value
 }

For your other question, you need to create a keyPressEvent and check if pressed key is dash or not. If it's dash you can call another function to do something. Edit: BTW, the index of specialValueText() is 0.

Edit: Or you can create a QShortcut in your main function.

 new QShortcut(QKeySequence(Qt::Key_Minus), this, SLOT(doSomething()));

Edit continued: doSomething() is a slot function. Put, for example void doSomething(); in the private slots: section of your header file. And in the cpp file define a function similar to this:

 void MainWindow::doSomething()
 {
     ui->spinBox->setValue(0);
     //This is the slot called when you press dash.
 }

Edit still continued: You need to declare a protected: function in the header like this:

 virtual void keyPressEvent(QKeyEvent *event);

Then you need to define this function in your cpp file. Like this:

 void MainWindow::keyPressEvent(QKeyEvent *event)
 {
     if(event->key() == Qt::Key_Minus)
         ui->spinBox->setValue(0);
 }

You don't have to connect any signals or slots for this function. It's an event.

That means when dash is pressed ui->spinBox->setValue(0);

Because of that, you need to create a spinBox with a range starting from 0.

 spinBox->setRange(0, 100);

That means,

 if(spinBox->value() == 0)
      //Then specialValueText is selected.
  • Thanks! `specialValueText` is exactly what I need. Now `QSpinBox` also accepts dash entered by keyboard! But when focus leaved (or also when something else is happening which must fix new value) value restores. Maybe there is better way to accept dash then use `keyPressEvent`? – Ufx Feb 07 '15 at 14:52
  • Are you using setValue()? –  Feb 07 '15 at 14:54
  • I mean you need to setValue(0); when dash is pressed. –  Feb 07 '15 at 14:56
  • I'm not sure that I understand you correctly. Currently `setValue` is not using by that `QSpinBox`. – Ufx Feb 07 '15 at 14:58
  • I mean the spinbox accepts dash, because it's one of the values. But if you want the changed value to not restore, you need to define a slot function that sets the value of spinbox to 0 when dash is pressed in your application. Or you might create a QShortcut of course. Edit: instead of keyPressEvent. Edit: At least this is what I think is the problem. –  Feb 07 '15 at 15:01
  • I can try to give an example for that. I mean if you need that. –  Feb 07 '15 at 15:02
  • How can I handle this event? This is what I did but this does not work. `private slots: void onKeyPress(QKeyEvent *event);` `connect(ui->spinBox, SIGNAL(keyPressEvent(QKeyEvent *)), this, SLOT(onKeyPress(QKeyEvent *)));` – Ufx Feb 07 '15 at 15:09
  • See my edit please. You can also create a QShortcut more easily. You need to connect it in your main function of your mainwindow class. Edit: To be more specific put that line into your mainwindow class' main function. –  Feb 07 '15 at 15:11
  • See **Edit continued** in my answer. –  Feb 07 '15 at 15:17
  • I edited once more. I forgot to put parentheses around tr() function. –  Feb 07 '15 at 15:19
  • `doSomething()` is not calling. I added `new QShortcut(QKeySequence(tr("-")), this, SLOT(doSomething()));` to window contructor, declared `void doSomething();` in `private slots` of window. – Ufx Feb 07 '15 at 15:27
  • It may be about dash not being a key sequence. I don't know. You might try the other solution I will edit into answer. See **Edit still continued** –  Feb 07 '15 at 15:29
  • Should I add a derived class for spinBox? – Ufx Feb 07 '15 at 15:42
  • No you don't need that. And I'm sorry I forgot that dash is actually `Qt::Key_Minus` I updated the codes. Should work now. –  Feb 07 '15 at 15:43
  • That's right. We created the shortcut for MainWindow. Hmm. You can always make `ui->spinBox->setFocusPolicy(Qt::NoFocus);` The spinbox would be not focusable. So if you need up and down keys for it, you can create shortcuts for them the same way. –  Feb 07 '15 at 15:48
  • I don't understand you. I thought that `new QShortcut(QKeySequence(Qt::Key_Minus), this, SLOT(doSomething()));` is another solution. – Ufx Feb 07 '15 at 15:57
  • You can use either keyPressEvent or QShortcut. –  Feb 07 '15 at 15:59