Ejaz
Ejaz

Reputation: 1632

Hide or unhide a QLabel at the same location

Login Page

I want to display the 'Invalid Username' and 'Invalid Password' QLabels only if the username and password are incorrect respectively, else I want to keep them hidden.

I tried using the hide() and show() methods, but the QLabels are displayed in a separate pop-up window with the show() method, instead of displaying in the position shown above (in black boxes).

I am writing the code in Python.

Please suggest.

Libraries imported:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

code is below:

#Labels
self.ErrorUsrName = QLabel("Invalid Username")
self.ErrorPasswd = QLabel("Invalid Password")

#Buttons
self.Lbutton = QPushButton("Login")
self.Cbutton = QPushButton("Cancel")

#Line Edits
self.UsrName = QLineEdit("Username")
self.Passwd = QLineEdit("Password")
self.UsrName.selectAll()

#Grid Layout
self.grid = QGridLayout()
self.grid.addWidget(self.UsrName,1,0)
self.grid.addWidget(self.ErrorUsrName.hide(),2,0)
self.grid.addWidget(self.Passwd,3,0)
self.grid.addWidget(self.ErrorPasswd.hide(),4,0)
self.grid.addWidget(self.Lbutton,5,0)
self.grid.addWidget(self.Cbutton,6,0)
self.setLayout(self.grid)

self.UsrName.setFocus()

#Signals
self.connect(self.Cbutton,SIGNAL("clicked()"), self, SLOT("reject()"))
self.connect(self.Lbutton,SIGNAL("clicked()"),self.login)

The Login Function for buttons

def login(self):

    if (self.UsrName.text() == "Ejaz" and self.Passwd.text() == "test"):
        print "Login Successful!"
    elif self.UsrName.text() <> "Ejaz":
        self.ErrorUsrName.show()
    elif self.Passwd.text() <> "test":
        self.ErrorPasswd.show()

Upvotes: 0

Views: 3605

Answers (1)

miindlek
miindlek

Reputation: 3553

Your problem are the following lines:

self.grid.addWidget(self.ErrorUsrName.hide(),2,0)
...
self.grid.addWidget(self.ErrorPasswd.hide(),4,0)

The hide function doesn't return the QLabel objects, so you aren't adding them to the grid. You should call the hide() function somewhere else, for example:

#Labels
self.ErrorUsrName = QLabel("Invalid Username")
self.ErrorUsrName.hide()
self.ErrorPasswd = QLabel("Invalid Password")
self.ErrorPasswd.hide()
...
self.grid.addWidget(self.ErrorUsrName,2,0)
self.grid.addWidget(self.ErrorPasswd,4,0)

This will solve your problem.

Upvotes: 1

Related Questions