简体   繁体   English

在 PyQt5 中使用 Inputmask 将 Cursor 设置为 QLineEdit 的开头

[英]Setting Cursor To The Beginning of QLineEdit With Inputmask In PyQt5

I created a code for desktop software.我为桌面软件创建了一个代码。 That, In QLineEdit, I set an inputmask.那,在 QLineEdit 中,我设置了一个输入掩码。 When I run my program and clicked that lineedit, I saw my cursor position at the end of the line.当我运行我的程序并单击该行编辑时,我在行尾看到了我的 cursor position。 Below is my some code....下面是我的一些代码....

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(380, 191)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(80, 20, 211, 41))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.lineEdit.setFont(font)
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(80, 110, 211, 41))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_2.setInputMask("00.00")
        self.lineEdit_2.setCursorPosition(0)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I already mentioned that setCursorPosition(0) after that.在那之后我已经提到了 setCursorPosition(0) 。 But it does not works.但它不起作用。 I want that cursor at beginning of that lineedit when I click my lineEdit_2.当我单击我的 lineEdit_2 时,我希望 cursor 在该 lineedit 的开头。

self.lineEdit_2.setInputMask("00.00")
self.lineEdit_2.setCursorPosition(0)

You can install an event filter on the line edit and react to any mouse click event:您可以在行编辑上安装事件过滤器并对任何鼠标单击事件做出反应:

from PyQt5 import QtCore, QtWidgets
from ui_mainwindow import Ui_MainWindow

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.lineEdit_2.installEventFilter(self)

    def eventFilter(self, source, event):
        if source == self.lineEdit_2 and event.type() == QtCore.QEvent.MouseButtonPress:
            self.lineEdit_2.setFocus(QtCore.Qt.MouseFocusReason)
            self.lineEdit_2.setCursorPosition(0)
            return True
        return super().eventFilter(source, event)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Note that you should write the above in a separate script which you'll use as your main program;请注意,您应该将上述内容编写在一个单独的脚本中,并将其用作您的主程序; the code you posted, taken from the output of pyuic , should never be modified.您发布的代码取自 pyuic 的pyuic永远不应修改。 Read more about using Designer .阅读有关使用 Designer的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM