Setting focus on QlineEdit widget

后端 未结 1 1202
清酒与你
清酒与你 2020-12-20 23:18

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

相关标签:
1条回答
  • 2020-12-21 00:01

    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()
    
    0 讨论(0)
提交回复
热议问题