将光标放在QLineEdit中

时间:2017-12-20 15:42:34

标签: python pyqt4

我想将光标放在空行编辑框中,而不必点击它。

我在这里查看了参考资料 http://ftp.ics.uci.edu/pub/centos0/ics-custom-build/BUILD/PyQt-x11-gpl-4.7.2/doc/html/qlineedit.html#selectionStart

我试着打电话

QLineEdit.home(True)

但是这并没有选择lineEdit这么说。

这是代码

的淡化版本
from PyQt4 import QtCore, QtGui
import sys
import os
import os.path

class Ui_Form1(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)


    def setupUi(self, Form):
        #init stuff
        Form.setObjectName("Form")
        Form.resize(794, 538)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.hLayout = QtGui.QHBoxLayout(Form)
        self.hLayout.setObjectName("hLayout")
        self.vLayout = QtGui.QVBoxLayout(Form)
        self.vLayout.setObjectName("vLayout")


        #label for information
        self.gridLayout.addLayout(self.hLayout, 0, 0)
        self.hLayout.addLayout(self.vLayout, 0)
        self.label = QtGui.QLabel(Form)
        self.label.setObjectName("label")

        #label pixmap
        self.label2 = QtGui.QLabel(Form)
        self.label2.setObjectName("label")

        #line edit
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
        self.gridLayout.addWidget(self.lineEdit, 3,0)

        self.list = QtGui.QListWidget(self)
        self.list.setObjectName("outlist")

        self.list.setMinimumHeight(150)
        self.vLayout.addWidget(self.list, 1)
        self.hLayout.addWidget(self.label, 1)
        self.vLayout.addWidget(self.label2, 0)
        #self.hLayout.addWidget(self.label2, 0)
        self.label2.setText('line edit')
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)


    def retranslateUi(self, Form):
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setText('Picture would go here')
        self.label2.setText('line edit')
        self.list.addItem('cursor will disappear when this is pressed')
        #press enter to update pic
        #self.lineEdit.returnPressed.connect(self.update_pic)

        #####sh



if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        ex = Ui_Form1()
        ex.show()
        sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:0)

self.lineEdit.setFocus(QtCore.Qt.StrongFocus)

答案 1 :(得分:0)

在 PyQt5 中:

self.lineEdit.setFocus()