SpinBox and virtual Keyboard
-
Hi community,
I'm kind of a QT noob. I need a virtual keyboard, and it should be Qt widgets (customer want it that way).
Now I found an easy to use and reuse approach from TI (http://processors.wiki.ti.com/index.php/Qt_Keyboard_Template) they open it using the
selectionChanged()slot of aQLineEdit.Now what' the most QT way to open it when clicking into any SpinBox?
Cheers Tillo
-
Hi
The very right way would be via
http://doc.qt.io/qt-5/qinputmethod.html#details
for true integration. ( as far as i know. not tried that class)However, if you only need for SpinBox, then maybe event filter to catch the click
(pseudo code - yours is most likely MainWindow and not QDialog)in ctor ui->spinBox->installEventFilter(this); ... bool Dialog::eventFilter(QObject *o, QEvent *e) { if (o == spinBox && e->type() == QEvent::MouseButtonPress ) { e->ignore(); // pop keyboard return true; } return QDialog::eventFilter(o, e); }alternative
you can get its lineEdit and maybe do the same as sample ? QLineEdit *LE= ui->spinBox->findChild<QLineEdit*>(); if (LE) { connect like sample } -
@mrjj Actually the alternative had a flaw. Since it uses
selectionChanged()and the text in a SpinBox get selected when clicking on one of the arrows...The solution I came up with, using
customContextMenuRequested():spinBox->setContextMenuPolicy(Qt::CustomContextMenu); connect(spinBox,&QLineEdit::customContextMenuRequested,this,&dialog::openKbLineEdit);and the fitting slot
void dialog::openKbLineEdit() { QLineEdit *line; if(typeid(sender()) != typeid(QLineEdit)){ line = sender()->findChild<QLineEdit*>(); } else{ line = (QLineEdit *)sender(); } lineEditkeyboard->setLineEdit(line); lineEditkeyboard->show(); }Oh, yeah no .ui involved here.
-
Super
but i wish you would use
qobject_cast
and not c cast. in
line = (QLineEdit *)sender();But im just old school :)
As c cast are bad and qobject_cast is the way to cast Qt types. -
@mrjj it's not old school,
qobject_castis the right tool when casting QObject based objects.C style cast doesn't offer any guarantee that you are casting anything compatible.