arturo2307
arturo2307

Reputation: 31

How to know which QLineEdit is focused?

I'm new in QT. Could someone help me with this?

I'm coding a widget application (with QMainWindow) with three QLineEdits widgets, I used setText to set a random text and connect the timeout() signal to a slot. When the timer "interrupts" I want to know in which QLineEdit the cursor is.

MainWindow constructor:

ui->setupUi(this);
timer = new QTimer(this);
timer -> start(1000); // 1 sec
connect(timer, SIGNAL(timeout()), this, SLOT(FinishTimer()));
ui->lineEdit1->setText("Line1");
ui->lineEdit2->setText("Line2");
ui->lineEdit1->setFocus();

In finish timer function I want to move the cursor but first I need to know in which QLineEdit I am and the position of the cursor.

FinishTimer slot:

QString debug;
debug = this->focusWidget()->objectName(); //this is a debug line, it works fine 
// this line doesn't work
int position = this->focusWidget()->cursorPosition();

cursorPosition is a method of QLineEdit, the error is "class QWidget has no member named 'cursorPosition'". I hope you will understand me.

Upvotes: 1

Views: 1082

Answers (1)

arturo2307
arturo2307

Reputation: 31

I used qobject_cast and it works really well. Thanks

position=qobject_cast<QLineEdit*>(this->focusWidget())->cursorPosition();

Upvotes: 1

Related Questions