Home > Software engineering >  Text of label in UI file can't be changed
Text of label in UI file can't be changed

Time:10-28

I tried it.

from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore
from PySide2.QtUiTools import QUiLoader
from maya.app.general.mayaMixin import MayaQWidgetBaseMixin
import shiboken2 as shiboken
import os

UIFILEPATH = 'D:/MAYA/pyside_pick/ui/PicsTest5.ui'

class MainWindow(MayaQWidgetBaseMixin,QtWidgets.QMainWindow):
    def __init__(self,parent=None):
        super(MainWindow,self).__init__(parent)
        
        self.UI = QUiLoader().load(UIFILEPATH)
        
        self.setWindowTitle(self.UI.windowTitle())
        
        self.setCentralWidget(self.UI)
        #image
        img = QtGui.QPixmap('D:/MAYA/pyside_pick/images/imgKohaku.png')
        self.scene = QtWidgets.QGraphicsScene(self)
        item = QtWidgets.QGraphicsPixmapItem(img)
        self.scene.addItem(item)
        self.UI.graphicsView_char_1.setScene(self.scene)

        #filter
        self._filter = Filter()
        self.installEventFilter(self._filter)
        
        self.UI.pSphere1.installEventFilter(self._filter)
        
        
        #primary
        self.UI.label.setStyleSheet("QLabel {color : white;}")
        self.UI.label.setText("A")
        
    def labelTest(self):
        
        self.UI.label.setStyleSheet("QLabel {color : red;}")
        self.UI.label.setText("B")
        print('D')
        
        return False
     
    
    
class Filter(QtCore.QObject):
    
    def eventFilter(self, widget, event):
        win = MainWindow()
        
        if event.type() == QtCore.QEvent.MouseButtonPress:
            print(widget.objectName())
            cmds.select(widget.objectName())
            win.labelTest()
        return False
        
    
def main():
    
    win = MainWindow()
    win.show()
    
        
if __name__ == '__main__':
    main()
        

I clicked the button that 'pSphere1', but self.UI.label.setStyleSheet("QLabel {color : red;}") self.UI.label.setText("B") were look like it's not working. I can change it inside define with UI loaded, but can't I do setText from outside?

How can I change the label of an imported UI file? I find this, but I really do not understand. I couldn't find any mention of them beyond this page. Change comboBox values in Qt .ui file with PySide2 If you know, I also want you to tell me where to put them.

CodePudding user response:

Your issue is within the eventFilter(), and specifically the first line:

    win = MainWindow()

This will create a new main window instance, which clearly doesn't make sense, since you obviously want to interact with the existing one.

While you could add the instance as an argument in the filter constructor in order to get a reference to the instance and directly call the function, that wouldn't be very good from the OOP point of view, as objects should never directly access attributes of their "parents".

A better and more correct approach would be to use a custom signal instead, and connect it from the main window instance:

class Filter(QtCore.QObject):
    testSignal = QtCore.Signal()

    def eventFilter(self, widget, event):
        if event.type() == QtCore.QEvent.MouseButtonPress:
            print(widget.objectName())
            cmds.select(widget.objectName())
            self.testSignal.emit()
        return False


class MainWindow(MayaQWidgetBaseMixin, QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        # ...
        self._filter.testSignal.connect(self.labelTest)

Note that widgets could accept events and prevent propagation (for instance, buttons or graphics views that have selectable or movable items), so you might not receive the event in the filter in those cases.

  • Related