QLineEdit python方式中的大写输入 [英] Uppercase input in QLineEdit python way

查看:45
本文介绍了QLineEdit python方式中的大写输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QT Designer 绘制了一个 UI,但发现没有参数可以让我将 QLineEdit 输入设置为大写.

I had drawn up an UI using the QT Designer but found out that there are no parameters for me to set QLineEdit inputs to be uppercase.

经过一些网上搜索,我只看到了少数满足我需求的结果,但都是用 Qt 编码的.例如,这个 link

After doing some online searching, I have only seen a very few handful of results that cater to my needs, however all are coded in Qt. Example, this link

那么,我有没有办法以 Pythonic 的方式做到这一点?

And so, are there ways for me to do this in the pythonic way?

推荐答案

试试这个,我相信这符合您的目的.我不会把它称为 pythonic.更像是 PyQt 覆盖.

Try this, I believe this serves your purpose. I won't call it much pythonic. More like PyQt override.

#minor 代码编辑

#minor code edit

from PyQt4 import QtGui
import sys
#===============================================================================
# MyEditableTextBox-  
#===============================================================================
class MyEditableTextBox(QtGui.QLineEdit):
#|-----------------------------------------------------------------------------|
# Constructor  
#|-----------------------------------------------------------------------------|

    def __init__(self,*args):
        #*args to set parent
        QtGui.QLineEdit.__init__(self,*args)

#|-----------------------------------------------------------------------------|
# focusOutEvent :- 
#|-----------------------------------------------------------------------------|
    def focusOutEvent(self, *args, **kwargs):
        text = self.text()
        self.setText(text.__str__().upper())
        return QtGui.QLineEdit.focusOutEvent(self, *args, **kwargs)


#|--------------------------End of focusOutEvent--------------------------------|
#|-----------------------------------------------------------------------------| 
# keyPressEvent
#|-----------------------------------------------------------------------------|
    def keyPressEvent(self, event):
        if not self.hasSelectedText():
            pretext = self.text()
            self.setText(pretext.__str__().upper())
        return QtGui.QLineEdit.keyPressEvent(self, event)

#|--------------------End of keyPressEvent-------------------------------------|

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    lay = QtGui.QHBoxLayout()
    w.setLayout(lay)
    le1 = MyEditableTextBox()
    lay.addWidget(le1)
    le2 = MyEditableTextBox()
    lay.addWidget(le2)
    w.show()
    sys.exit(app.exec_())

这篇关于QLineEdit python方式中的大写输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆