qlabel可以选中吗_retranslateUi()清除QLabel文本

My qt4-based application dynamically changes the GUI language by calling retranslateUi() function. It works like a charm, but when it processes the QLabel which text changes by the program, it tries to translate original text - the one, specified in Qt Designer, not the one, set by my program.

I understand it happens because retranslateUi() is auto-generated during build process so it couldn't possibly know about my changes, but I'd prefer to skip such QLabels completely. Is there a way to tell Qt to skip certain widgets in retranslateUi()? Alternatively, maybe I can disable content change for QLabel in my program?

I know I can sub-class QLabel and by overriding setText() solve this problem, but I prefer to use standard QLabel to be able to manipulate it using Qt Designer.

解决方案

As I remember, in Designer you can uncheck on QLabel should it be translated. By default it is. Choose label you don't want to be translated, in property editor scroll to "text" property, expand it and uncheck translate checkbox. Then you should generate ui_*.h file again. After that your label shouldn't be in retranslateUi code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'untitled.ui' # # Created by: PyQt5 UI code generator 5.15.4 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. import sys from ui import ui from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): super().__init__() Form.setObjectName("Form") Form.resize(633, 434) self.label = QtWidgets.QLabel(Form) self.test = ui() self.label.setGeometry(QtCore.QRect(110, 50, 54, 12)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(110, 80, 54, 12)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(110, 110, 54, 12)) self.label_3.setObjectName("label_3") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(440, 70, 75, 23)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Form) self.pushButton_2.setGeometry(QtCore.QRect(440, 110, 75, 23)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.clicked.connect(self.tz) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "温度:xxx")) self.label_2.setText(_translate("Form", "湿度:xxx")) self.label_3.setText(_translate("Form", "光照:xxx")) self.pushButton.setText(_translate("Form", "控制LED1")) self.pushButton_2.setText(_translate("Form", "历史记录")) def tz(self): self.test.show() def back(self): self.test.hide() self.show() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = Ui_Form() main = QtWidgets.QMainWindow() w.setupUi(main) main.show() app.exec_()
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值