P
P
pythono2017-05-31 09:48:06
Python
pythono, 2017-05-31 09:48:06

How to give focus to a specific widget in PyQt?

It is necessary to give focus to a particular widget when the window is activated. For example, if the user opened some other external application, worked in it and closed it, then when the window of our application is activated, the focus in it should automatically be set to a specific element (widget).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-06-01
@pythono

Here is an example where a PyQt5 window with a LineEdit widget picks up focus when the LineEdit window is activated

import sys
from PyQt5.QtWidgets import (QWidget, QLabel,
    QLineEdit, QApplication)


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        self.lbl = QLabel(self)
        qle = QLineEdit(self)

        qle.move(60, 100)
        self.lbl.move(60, 40)

        qle.textChanged[str].connect(self.onChanged)

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QLineEdit')
        self.show()


    def onChanged(self, text):

        self.lbl.setText(text)
        self.lbl.adjustSize()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question