Python Forum
QLabel is not clearing before updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QLabel is not clearing before updating
#1
Hi All

I have never had this issue before, but I am opening a Window with a QLabel on and setting the text to a paricular value. However, when I open the window a few different times, the text seems to not clear before updating itself with the new text in. See image.

'lblTEST' is the name of the label I've been trying to get working because lblUsersWorkflow wasn't working

def updateUsersPermissionsControls(self):
        # First filter by the users role
        permissionsDF = self.maintainResourcesWt.dfUserPermissions.copy()
        roleDf = self.maintainResourcesWt.dfRoles.copy()

        # Get the RoleID where the role is Unallocated
        roleDf = roleDf[roleDf['RoleShortName'] == 'Unallocated']
        roleDf.reset_index(drop=True, inplace=True)
        unallocatedRoleID = roleDf.at[0, 'RoleID']
        if not self.lblSessionRoleID.text().isnumeric():
            roleID = unallocatedRoleID
        else:
            roleID = int(self.lblSessionRoleID.text())

        permissionsDF = permissionsDF[permissionsDF['RoleID'] == roleID]
        permissionsDF.reset_index(drop=True, inplace=True)

        # Users_EditUsers Permission
        permissionLevelDf = permissionsDF[permissionsDF['PermissionType'] == 'Users_EditUsers']
        permissionLevelDf.reset_index(drop=True, inplace=True)
        permissionLevel = permissionLevelDf.at[0, 'PermissionTypeLevel']
        permissionRole = permissionLevelDf.at[0, 'PermissionRole']
        if permissionLevel == 'Denied':
            self.maintainUsersWindow.close()
            self.msgBox.setWindowTitle('Permission Required')
            self.msgBox.setText('Permissions are required to access this area')
            self.msgBox.setStandardButtons(QMessageBox.Abort)
            ret = self.msgBox.exec()
            return
        else:
            if permissionLevel == 'Viewer':
                self.maintainUsersUi.btnMaintainUsersUpdate.setEnabled(False)
                #self.maintainUsersUi.lblUsersWorkflow.setText('Read-Only')
                self.maintainUsersUi.lblTEST.setText('Read-Only')
            elif permissionLevel == 'Editor':
                if 'SelfApproval' in permissionRole:
                    #self.maintainUsersUi.lblUsersWorkflow.setText('Editor')
                    self.maintainUsersUi.lblTEST.setText('Editor')
                    self.maintainUsersUi.btnMaintainUsersUpdate.setEnabled(True)
                elif 'Requestor' in permissionRole:
                    #self.maintainUsersUi.lblUsersWorkflow.setText('Request')
                    self.maintainUsersUi.lblTEST.setText('Request')
                    self.maintainUsersUi.btnMaintainUsersUpdate.setEnabled(True)
                else:
                    #self.maintainUsersUi.lblUsersWorkflow.setText('Read-Only')
                    self.maintainUsersUi.lblTest.setText('Red-Only')
                    self.maintainUsersUi.btnMaintainUsersUpdate.setEnabled(False)

Attached Files

Thumbnail(s)
   
Reply
#2
You have 2 similarly named label objects, lblTest and lblTEST. Is that a problem? Should they be the same label?
Reply
#3
I see no reason why lblUsersWorkflow would not work. The error is not shown in the code you posted. Most likely the widget was not added to a layout.
Reply
#4
Genuinely I have no idea, seems strange.

Here is the code to setup the form GUI:

class Ui_dlgMaintainUsers(object):
    def setupUi(self, UsersDialogue):
        if not UsersDialogue.objectName():
            UsersDialogue.setObjectName(u"UsersDialogue")
        UsersDialogue.setWindowModality(Qt.WindowModality.ApplicationModal)
        UsersDialogue.resize(519, 523)
        self.btnMaintainUsersClose = QPushButton(UsersDialogue)
        self.btnMaintainUsersClose.setObjectName(u"btnMaintainUsersClose")
        self.btnMaintainUsersClose.setGeometry(QRect(410, 476, 91, 24))
        self.btnMaintainUsersDelete = QPushButton(UsersDialogue)
        self.btnMaintainUsersDelete.setObjectName(u"btnMaintainUsersDelete")
        self.btnMaintainUsersDelete.setGeometry(QRect(429, 400, 81, 31))
        icon = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.EditDelete))
        self.btnMaintainUsersDelete.setIcon(icon)
        self.btnMaintainUsersRefresh = QPushButton(UsersDialogue)
        self.btnMaintainUsersRefresh.setObjectName(u"btnMaintainUsersRefresh")
        self.btnMaintainUsersRefresh.setGeometry(QRect(400, 400, 31, 31))
        icon1 = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.ViewRefresh))
        self.btnMaintainUsersRefresh.setIcon(icon1)
        self.label_6 = QLabel(UsersDialogue)
        self.label_6.setObjectName(u"label_6")
        self.label_6.setGeometry(QRect(10, 10, 221, 16))
        self.tblMaintainUsers = QTableWidget(UsersDialogue)
        self.tblMaintainUsers.setObjectName(u"tblMaintainUsers")
        self.tblMaintainUsers.setGeometry(QRect(10, 30, 501, 371))
        font = QFont()
        font.setPointSize(9)
        font.setBold(True)
        self.tblMaintainUsers.setFont(font)
        self.tblMaintainUsers.setStyleSheet(u"border : 2px solid grey")
        self.tblMaintainUsers.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
        self.tblMaintainUsers.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        self.tblMaintainUsers.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectColumns)
        self.tblMaintainUsers.verticalHeader().setVisible(False)
        self.grpMaintainUsers = QGroupBox(UsersDialogue)
        self.grpMaintainUsers.setObjectName(u"grpMaintainUsers")
        self.grpMaintainUsers.setGeometry(QRect(10, 410, 361, 101))
        self.grpMaintainUsers.setStyleSheet(u"border : 1px solid grey")
        self.label_4 = QLabel(self.grpMaintainUsers)
        self.label_4.setObjectName(u"label_4")
        self.label_4.setGeometry(QRect(11, 25, 51, 16))
        self.label_4.setStyleSheet(u"border : 0px")
        self.label_3 = QLabel(self.grpMaintainUsers)
        self.label_3.setObjectName(u"label_3")
        self.label_3.setGeometry(QRect(11, 49, 59, 16))
        self.label_3.setStyleSheet(u"border : 0px")
        self.btnMaintainUsersUpdate = QPushButton(self.grpMaintainUsers)
        self.btnMaintainUsersUpdate.setObjectName(u"btnMaintainUsersUpdate")
        self.btnMaintainUsersUpdate.setGeometry(QRect(280, 66, 75, 24))
        icon2 = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.FolderNew))
        self.btnMaintainUsersUpdate.setIcon(icon2)
        self.txtMaintainUsersName = QLineEdit(self.grpMaintainUsers)
        self.txtMaintainUsersName.setObjectName(u"txtMaintainUsersName")
        self.txtMaintainUsersName.setGeometry(QRect(80, 20, 191, 21))
        self.txtMaintainUsersUsername = QLineEdit(self.grpMaintainUsers)
        self.txtMaintainUsersUsername.setObjectName(u"txtMaintainUsersUsername")
        self.txtMaintainUsersUsername.setGeometry(QRect(80, 44, 191, 21))
        self.txtMaintainUsersEmail = QLineEdit(self.grpMaintainUsers)
        self.txtMaintainUsersEmail.setObjectName(u"txtMaintainUsersEmail")
        self.txtMaintainUsersEmail.setGeometry(QRect(80, 68, 191, 21))
        self.label_5 = QLabel(self.grpMaintainUsers)
        self.label_5.setObjectName(u"label_5")
        self.label_5.setGeometry(QRect(10, 70, 59, 16))
        self.label_5.setStyleSheet(u"border : 0px")
        self.btnMaintainUsersClear = QPushButton(self.grpMaintainUsers)
        self.btnMaintainUsersClear.setObjectName(u"btnMaintainUsersClear")
        self.btnMaintainUsersClear.setGeometry(QRect(280, 40, 75, 24))
        icon3 = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.WindowClose))
        self.btnMaintainUsersClear.setIcon(icon3)
        self.lblUsersWorkflow = QLabel(self.grpMaintainUsers)
        self.lblUsersWorkflow.setObjectName(u"lblUsersWorkflow")
        self.lblUsersWorkflow.setGeometry(QRect(280, 20, 71, 16))
        self.lblUsersWorkflow.setStyleSheet(u"border : 0px")

        self.retranslateUi(UsersDialogue)

        QMetaObject.connectSlotsByName(UsersDialogue)

    def retranslateUi(self, UsersDialogue):
        UsersDialogue.setWindowTitle(QCoreApplication.translate("UsersDialogue", u"Available Users", None))
        self.btnMaintainUsersClose.setText(QCoreApplication.translate("UsersDialogue", u"Close", None))
        self.btnMaintainUsersDelete.setText(QCoreApplication.translate("UsersDialogue", u"Delete", None))
        self.btnMaintainUsersRefresh.setText("")
        self.label_6.setText(QCoreApplication.translate("UsersDialogue", u"Users", None))
        self.grpMaintainUsers.setTitle(QCoreApplication.translate("UsersDialogue", u"Maintain Users", None))
        self.label_4.setText(QCoreApplication.translate("UsersDialogue", u"Name", None))
        self.label_3.setText(QCoreApplication.translate("UsersDialogue", u"Username", None))
        self.btnMaintainUsersUpdate.setText(QCoreApplication.translate("UsersDialogue", u"Update", None))
        self.label_5.setText(QCoreApplication.translate("UsersDialogue", u"Email", None))
        self.btnMaintainUsersClear.setText(QCoreApplication.translate("UsersDialogue", u"Clear", None))
        self.lblUsersWorkflow.setText(QCoreApplication.translate("UsersDialogue", u"Name", None))
This is the code to call the Form from the main python code:
    def initialiseMaintainUsersDialogue(self):
        # Setup UI and add it to the Hierarchy Window
        self.maintainUsersUi.setupUi(self.maintainUsersWindow)

        # Connect the signals to the slots
        self.maintainUsersUi.tblMaintainUsers.clicked.connect(self.updateUserSelectionMaintainUsers)
        self.maintainUsersUi.btnMaintainUsersClear.clicked.connect(self.clearUserSelectionMaintainUsers)
        self.maintainUsersUi.btnMaintainUsersRefresh.clicked.connect(self.getUserList)
        self.maintainUsersUi.btnMaintainUsersUpdate.clicked.connect(self.addUserToUserList)
        self.maintainUsersUi.btnMaintainUsersDelete.clicked.connect(self.removeUserFromUserList)
        self.maintainUsersUi.btnMaintainUsersClose.clicked.connect(self.closeWinUserList)

        # Initialise First Tab in View Table
        self.initialiseUsersTable()

        # Show the Dialogue
        self.maintainUsersWindow.show()

        # Set the controls based on the users permissions
        self.updateUsersPermissionsControls()

        # Populate the first tab which is visible
        if self.maintainUsersWindow.isVisible():
            self.getUserList()
        else:
            self.theProgressBar.setVisible(False)
Didn't think you would really need to see this bit to create the overall containers but here it is (excerpt from my init):
        self.maintainResourcesWindow = QMainWindow()
        self.maintainUsersWindow = QMainWindow()
        self.maintainHierarchyWindow = QMainWindow()
        self.addHierarchyLocationWindow = QMainWindow()
        self.maintainProjectTypesWindow = QMainWindow()
        self.maintainUserRolesWindow = QMainWindow()
        self.maintainPermissionsWindow = QMainWindow()

        self.maintainResourcesUi = Ui_dlgMaintainResources()
        self.maintainUsersUi = Ui_dlgMaintainUsers()
        self.maintainHierarchyUi = Ui_dlgMaintainHierarchy()
        self.addHierarchyLocationUI = Ui_dlgAddLocationToHierarchy()
        self.maintainProjectTypesUi = Ui_dlgMaintainProjectTypes()
        self.maintainUserRolesUi = Ui_dlgMaintainUserRoles()
        self.maintainPermissionsUi = Ui_dlgMaintainPermissions()
You alredy have the piece of code i'm using to update a label so i think you have eveything that relates to this label not rendering proerly.

Any suggestions at all? Any help is greatly appreciated.
Reply
#5
This error only occurs when I reopen the window again and want to change the label text, so perhaps I am not closing the window properly or something, would that affect it at all?

I'm using the following close to close the Window:

    def closeWinUserList(self):
        self.maintainUsersWindow.close()
Reply
#6
Why don’t you use a layout manager? I’ve never seen user code call setGeometry(). Was the setupup method generated by a designer program?

I don't know what the behavior is if you set the size of a label and set text that is wider than the fixed label width. Does it write outside the bounds of the widget?
Reply
#7
I use the PySide6 Designer and open it from my Terminal by calling: pyside6-designer

See image

I don't think I've ever learned how to use a layout manager properly (embarrassingly)

Attached Files

Thumbnail(s)
   
Reply
#8
What happens when you reopen the window? Do you get an overlay effect as mentioned in your first post, or something different?
Reply
#9
I've tried several things and it just gives me this overlay affect and I can't seem to resolve it for some reason.

I've removed the Label now and I've taken a different approach, but thank you for your help anyway.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Clearing listboxes klatlap 3 4,229 Feb-01-2022, 04:50 PM
Last Post: deanhystad
  Scaling text QLabel following display mode windows '100%, 125% ...) VIGNEAUD 2 3,143 Jul-07-2021, 06:38 PM
Last Post: deanhystad
Question [PyQt] CSS Styling for a QLabel inside a QListWidget Alfalfa 2 6,790 Nov-30-2020, 02:59 AM
Last Post: Alfalfa
  [PyQt] Number of characters in QLabel hzcodec 2 5,824 Oct-13-2017, 10:11 AM
Last Post: hzcodec
  [Tkinter] Clearing Text in Tkinter icabero0225 2 27,257 May-31-2017, 09:38 PM
Last Post: icabero0225

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020