Solved Can't set font on QSpinBox with setLineEdit a custom lineEdit
-
I have a QSpinBox that I wanted to select all text on single click. An answer on stackoverflow said that I need to first create a lineEdit subclass for the feature as well as a QSpinBox subclass. Then use
QSpinBox::setLineEdit
to set the lineEdit class onto the QSpinBox subclass. And finally in the ui I promoted a regular QSpinBox dummy into this new QSpinBox subclass.The intended feature was added but the problem is that I couldn't set font to this new spinbox class now. I tried setting it in the ui as well as manually setting it in code, both didn't work. I have tested on other settings like width and maximum/minumum, and they works.
myLineEdit.cpp
void MyLineEdit::mousePressEvent(QMouseEvent *a_pEvent) { if (a_pEvent->button() == Qt::LeftButton) { selectAll(); a_pEvent->accept(); return; } QLineEdit::mousePressEvent(a_pEvent); }
genericspinbox.cpp
GenericSpinBox::GenericSpinBox(QWidget *parent) { QLineEdit *lineEdit = new MyLineEdit(this); setLineEdit(lineEdit); }
mymainfile.cpp
m_ui.mySpinBox->setFont(QFont("Arial", 20));
My font didn't change.
-
Hi
GenericSpinBox::GenericSpinBox(QWidget *parent) { QLineEdit *lineEdit = new MyLineEdit(this); <<< should it no tbe this on you call setFont on ? setLineEdit(lineEdit); }
You seem to call setFont on the QSpinBox itself.
-
But
setFont
works when using only the base QSpinBox. -
@lansing
Ok thats a bit odd then as you seem to switch to your LineEdit in ctor so
if you later call setFont it should work the same. -
What do you mean?
-
Okay I somewhat figured it, I don't know if it count as a hack. I created a new
void setFont
in the spinbox subclass and do the setFont there to the lineEdit . I don't know if my pointer and reference thing are right though.// in the header public: void setFont(const QFont & a_font); private: MyLineEdit * lineEdit; GenericSpinBox::GenericSpinBox(QWidget *parent) { lineEdit = new MyLineEdit(this); setLineEdit(lineEdit); } void GenericSpinBox::setFont(const QFont & a_font) { lineEdit->setFont(a_font); }
-
@lansing
HI
It looks fine.
Not sure the & matters much as QFont is meant to be a class we copy but
won't hurt either.