本文介紹了將焦點放在 QlineEdit 小部件上的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試組合一個簡單的小部件,其中焦點自動放在 QLineEdit 小部件上(它用于條形碼掃描儀輸入,我不希望最終用戶必須單擊小部件).
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 have to click in the widget).
相關代碼如下:
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.lineEdit = QLineEdit(self)
self.lineEdit.setPlaceholderText('foobar')
self.lineEdit.setFocusPolicy(Qt.StrongFocus)
self.lineEdit.setFocus()
layout.addWidget(self.lineEdit)
self.setLayout(layout)
self.show()
def main():
app = QApplication([])
styles.dark(app)
g = windows.ModernWindow(MyGui())
g.resize(350,100)
location = (50, 100)
g.move(location[0], location[1])
g.setWindowTitle('SmallTest')
g.show()
app.exec_()
if __name__ == '__main__':
main()
它不想將焦點放在 lineEdit 小部件上.我猜這是我錯過的非常明顯的東西,但我已經挖了一段時間試圖找到它,但還沒有偶然發現它.
And it doesn't want to put the focus on the lineEdit widget. I'm guessing its something really obvious that I've missed, but I've been digging around for a while trying to find it, and haven't stumbled upon it yet.
推薦答案
你必須使用 setFocus()
小部件顯示后.
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()
這篇關于將焦點放在 QlineEdit 小部件上的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!