bluejambo
bluejambo

Reputation: 221

Is there a way to automatically have the cursor be in a QLineEdit without having to select it first?

So I am currently making a project in QTCreator where I use a RFID Scanner as a way of authentication and with my scanner the RFID gets sent like it was typed by a keyboard. Now what I want to do, is have a QLineEdit grab the RFID, which already works, save the RFID and afterwards clear the space again. All of the above already works with the code below. Now my question is, is there a way to have the cursor already in QLineEdit, and therefore ready to accept the RFID, without me selecting the QLineEdit first. I am open to changing the widget that I am using, as long as I have a signal for a when the enter button was pressed because that is the way the chip ends a RFID.

I tried setting the position of the cursor after displaying it but that didn't select the QLineEdit. I also looked for solutions using QTextEdit but no results there either, besides that it not even having the returnPressed signal.

A few side infos, I am using QTCreator 4.12 with Qt 5.12.7 and I am on a Virtual Machine running openSUSE Leap 15.2, any help or different widget I could use is very much appreciated.

QLineEdit *testLineEdit = new QLineEdit;

connect(testLineEdit, SIGNAL(returnPressed()), this, SLOT(scanTest()));

void MainWindow::scanTest(){
  QString RFID = testLineEdit->text();
  testLineEdit->clear();
} 

Upvotes: 0

Views: 146

Answers (2)

bluejambo
bluejambo

Reputation: 221

So the answer of Maitai didn't work for me, for some reason, but it did guide me to what I needed in my example:

testLineEdit->grabKeyboard();

I am aware that this only works because I only have this scanner as an input and only one input field but I don't need more here.

Upvotes: 1

Maitai
Maitai

Reputation: 42

testLineEdit->setFocus(Qt::OtherFocusReason);

https://doc.qt.io/qt-5/qwidget.html#setFocus

Upvotes: 0

Related Questions