I am trying to put together a simple widget where the focus is automatically put to a QLineEdit widget (It is for a barcode scanner input, and I don\'t want the end users to
You must use setFocus() after the widget is displayed.
from qtmodern import styles, windows
from qtpy import QtGui, QtCore
from qtpy.QtWidgets import *
from qtpy.QtCore import Qt
class MyGui(QWidget):
def __init__(self, parent=None):
super(MyGui, self).__init__(parent)
layout = QVBoxLayout(self)
self.lineEdit = QLineEdit(self)
self.lineEdit.setPlaceholderText('foobar')
self.lineEdit.setFocusPolicy(Qt.StrongFocus)
layout.addWidget(self.lineEdit)
def main():
app = QApplication([])
styles.dark(app)
gui = MyGui()
g = windows.ModernWindow(gui)
g.resize(350,100)
location = (50, 100)
g.move(*location)
g.setWindowTitle('SmallTest')
g.show()
gui.lineEdit.setFocus()
app.exec_()
if __name__ == '__main__':
main()