November 17, 2011

PSI-lbc PSI-lbc
Lab Rat
39 posts

[sorta Solved] QDoubleSpinBox..How to clear selected/highlighted text?

 

When I click the arrow buttons of a QDoubleSpinBox the text is highlighted (selected?) at the click. This is probably normal, but the highlighting makes it harder to read the numbers in the attached QLineEdit

I have tried just about everything to get rid of the highlighting.

clearFocus()
change focus to another widget
access the pointer to the QLineEdit (QLineEdit * QAbstractSpinBox::lineEdit () const ) and then deselect()
clear() and then setValue()
tried setting all the different focusPolicy possibilites
tried changing the stylesheet highlight text color to not the blue highlight color

Is there something I’m missing?

4 replies

November 17, 2011

BilbonSacquet BilbonSacquet
Lab Rat
75 posts

Could give the parameters of your QDoubleSpinBox ?

November 17, 2011

BilbonSacquet BilbonSacquet
Lab Rat
75 posts

Anyway I reproduce it! :)

The selection is done through:

  1. myDoubleSpinBox->lineEdit()->selectAll();

And the solution is to call internally:
  1. this->lineEdit()->deselect();

(like you suggest)
Now the problem is ‘when’ you do this! If you do it in the slot which gets the values it’s too early, the selectAll() is done AFTER the signal valueChanged()!

If ‘only’ the steps ± are the problem, inherits the QDoubleSpinBox and redefine the stepBy() function like this:

  1. void MyDoubleSpinBox::stepBy(int steps)
  2. {
  3.     QDoubleSpinBox::stepBy(steps);
  4.  
  5.     lineEdit()->deselect();
  6. }

Pay attention that you regenerate/recompile too the .ui files which reference your double spinboxes.

November 18, 2011

PSI-lbc PSI-lbc
Lab Rat
39 posts

Thanks…

>>Could give (you) the parameters of your QDoubleSpinBox ?
They’re all the standard settings as the QDoubleSpinBox is dropped on a form using Qt Creator. The ui-> convention used below is the Creator generated code in the .h file

  1. private:
  2.     Ui::form *ui;

>>Now the problem is ‘when’ you do this! If you do it in the slot which gets the values it’s too early

Doh! That’s when I was trying everything…in the valueChanged slot…

  1. void form::on_dsb_valueChanged(double val)

I tried moving the deselect() call to the editingFinished slot for the dsb…

  1. void form::on_dsb_editingFinished()
  2. {
  3.    ui->dsb->lineEdit()->deselect();
  4. }

..but get the error..

/Library/Frameworks/QtGui.framework/Headers/qabstractspinbox.h:157: error: ‘QLineEdit* QAbstractSpinBox::lineEdit() const’ is protected within this context

I also tried mapping the editingFinished signal to a generic slot and casting the widget pointer to a dsb…same error as above..protected

  1. void form::dsbEditFinishedDeselect(QWidget *obj)
  2. {
  3.   #if DEBUG_SYNCH
  4.   qDebug() << "*form::dsbEditFinishedDeselect " ;
  5.   #endif
  6.  
  7.   QDoubleSpinBox *dsb = qobject_cast<QDoubleSpinBox *>(obj);
  8.   dsb->lineEdit()->deselect();
  9. }

Not sure that it matters, but this is on a Mac..haven’t tried it on my Win7 dev box, yet.

November 18, 2011

PSI-lbc PSI-lbc
Lab Rat
39 posts

Cheesy, but this works…

  1. void form::dsbEditFinishedDeselect(QWidget *obj)
  2. {
  3.   #if DEBUG_SYNCH
  4.   qDebug() << "*form::dsbEditFinishedDeselect " ;
  5.   #endif
  6.  
  7.   QDoubleSpinBox *dsb = qobject_cast<QDoubleSpinBox *>(obj);
  8.   QString nohighlights =  "color: black; background: white; selection-color: black; selection-background-color: white;"  
  9.  
  10.   dsb->setStyleSheet( nohighlights );
  11.  
  12. }

 
  ‹‹ Configure on Ubuntu      Crash in void QWidgetBackingStore::sync() ››

You must log in to post a reply. Not a member yet? Register here!