4

I cannot find a way to activate a cursor inside a QTextEdit without clicking inside the actual widget. What I want to be able to do is, type something in side the QTextEdit window, click on a QPushButton and have the cursor stay active inside the QTextEdit without having to click in the window again.

Ideas?

JonnyCplusplus
  • 881
  • 3
  • 12
  • 22
  • Nope, I have also tried activateWindow() which seams like it should work but it doesn't. I may need to call something after activateWindow() to get the cursor active too? – JonnyCplusplus May 14 '13 at 14:40
  • myTextEdit->setFocus(); check link http://qt-project.org/doc/qt-4.8/qwidget.html#setFocus – Sanoob May 14 '13 at 14:40
  • In case `setFocus()` doesn't work for you, see http://stackoverflow.com/a/43383330/3697870 – Heath Raftery Apr 13 '17 at 04:08

3 Answers3

3

When the user clicks the button, you should give the focus back to the text edit using setFocus():

myTextEdit->setFocus();
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
1

In the button press handler, call your QTextEdit's setFocus() command.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
darron
  • 4,284
  • 3
  • 36
  • 44
1

There is a better way. Select your button and change focuspolicy to NoFocus.

Then you can click on your button and it will not pull the focus from your your TextEdit window, and you can run the code that your button does and it will leave the cursor in the edit window.

Thomas Williams
  • 1,528
  • 1
  • 18
  • 37
  • By setting the focuspolicy of the button to NoFocus, you're breaking accessibility of the application as the button is no longer accessible through the TAB-key. – huysentruitw Jul 10 '21 at 09:21