0

I may have found the solution to my own question but I have another problem. It turns out that the user can just hit "Esc" and close the form. How can I disable the escape key? I'm looking for some python code, but I'm at a loss.

Edit

Below is my code, if it helps.

# -*- coding: iso-8859-15 -*-
from PyQt4 import QtCore, QtGui
import psycopg2
import hashlib


def authentication():
    conn_string = "host='host' dbname='dbname' user='username' password=pass"
    conn = psycopg2.connect(conn_string)
    global userText
    username = str(userText.text())
    global passText
    hashpassword = hashlib.md5(str(passText.text())).hexdigest()
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM users WHERE username=%s AND password=%s', (username, hashpassword))
    if cursor.rowcount == 1:
        cursor.close ()
        conn.close ()
        global myDialog
        myDialog.accept()
    else:
        cursor.close ()
        conn.close ()
        msgBox = QtGui.QMessageBox()
        msgBox.setWindowTitle(QtGui.QApplication.translate("loginform", "Σφάλμα", None, QtGui.QApplication.UnicodeUTF8))
        msgBox.setText(QtGui.QApplication.translate("msgBox", "Λάθος Όνομα Χρήστη ή Συνθηματικό", None, QtGui.QApplication.UnicodeUTF8))
        msgBox.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint);
        msgBox.exec_()

class Login (QtGui.QDialog):
    dlg = QtGui.QDialog()
    dlg.setObjectName("loginform")
    dlg.setWindowTitle(QtGui.QApplication.translate("dlg", "Φόρμα σύνδεσης", None, QtGui.QApplication.UnicodeUTF8))
    dlg.setEnabled(True)
    dlg.resize(400, 203)
    dlg.setMaximumSize(QtCore.QSize(400, 203))
    dlg.setSizeGripEnabled(False)
    dlg.setModal(True)
    dlg.setWindowFlags(QtCore.Qt.FramelessWindowHint); 
    event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_Escape, QtCore.Qt.NoModifier)

    font = QtGui.QFont()
    font.setPointSize(10)

    username = QtGui.QLineEdit(dlg)
    username.setEnabled(True)
    username.setGeometry(QtCore.QRect(190, 19, 151, 21))
    username.setFont(font)
    username.setObjectName("username")
    global userText
    userText = username
    userlabel = QtGui.QLabel(dlg)
    userlabel.setEnabled(True)
    userlabel.setGeometry(QtCore.QRect(70, 20, 91, 16))
    userlabel.setFont(font)
    userlabel.setLayoutDirection(QtCore.Qt.LeftToRight)
    userlabel.setObjectName("userlabel")
    userlabel.setText(QtGui.QApplication.translate("dlg", "Όνομα Χρήστη", None, QtGui.QApplication.UnicodeUTF8))
    userlabel.setBuddy(username)

    password = QtGui.QLineEdit(dlg)
    password.setGeometry(QtCore.QRect(190, 59, 151, 21))
    password.setFont(font)
    password.setEchoMode(QtGui.QLineEdit.Password)
    password.setPlaceholderText("")
    password.setObjectName("password")
    global passText
    passText = password
    passlabel = QtGui.QLabel(dlg)
    passlabel.setGeometry(QtCore.QRect(70, 60, 91, 16))
    passlabel.setFont(font)
    passlabel.setMargin(0)
    passlabel.setObjectName("passlabel")
    passlabel.setText(QtGui.QApplication.translate("loginform", "Συνθηματικό", None, QtGui.QApplication.UnicodeUTF8))
    passlabel.setBuddy(password)

    btnLogin = QtGui.QPushButton(dlg)
    btnLogin.setGeometry(QtCore.QRect(160, 170, 75, 23))
    btnLogin.setDefault(False)
    btnLogin.setFlat(False)
    btnLogin.setObjectName("btnLogin")
    btnLogin.setText(QtGui.QApplication.translate("loginform", "Σύνδεση", None, QtGui.QApplication.UnicodeUTF8))
    btnLogin.clicked.connect(authentication)

    global myDialog
    myDialog = dlg

    def keyPressEvent(self, event):
        msg = QtGui.QMessageBox()
        msg.setText("You Pressed: "+ event.text())
        msg.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint);
        msg.exec_()

    dlg.show()
7
  • 1
    I don't see a problem here. If the user does not want to enter the information, they can close the box and use QGIS without the plugin? Isn't this a good thing? Feb 13, 2014 at 12:36
  • No, because the plugin is a login form for user authentication. The user must have the apropriate credentials to use QGIS
    – GsetIT
    Feb 13, 2014 at 12:40
  • 2
    Isn't this normally done with user-rights within the operating system? Can you tell us why you are using this approach? Feb 13, 2014 at 12:43
  • I've been asked to do this by my boss. So, no questions asked there... :)
    – GsetIT
    Feb 13, 2014 at 12:46
  • 1
    I would also add that someone can just open your plugin in a text file and add return in authentication and it will bypass.
    – Nathan W
    Feb 13, 2014 at 13:42

1 Answer 1

4

If you really want to do this (although see comments about the reasons why it might not be a good idea) you can do it this way:

from ui_mydialog import myDialog

class LoginDialog(myDialog, QDialog):
    def __init__(self, parent=None):
        super(LoginDialog, self).__init__(parent)
        self.setupUi(self)

        self.btnLogin.clicked.connect(self.authentication)

    def closeEvent(self, event):
        if not self.authenticated:
            event.ignore()

    def keyPressEvent(self, event):
        if not event.key() == Qt.Key_Escape:
            super(LoginDialog, self).keyPressEvent(event)

    def authentication(self):
        # Do stuff
        self.authenticated = True
5
  • Thanks, but this doesn't work. I can't seem to be able to override the inheritted functions from the QGIS Python console. If I try this from outside QGIS (ie. IDLE) it works fine. Why is that?
    – GsetIT
    Feb 13, 2014 at 14:05
  • Well you are creating a dialog inside your dialog class. My be best to brush up on some python and pyqt first
    – Nathan W
    Feb 13, 2014 at 14:27
  • I just copy-pasted the code generated from a .ui file with pyuic4. Nevertheless, using pieces of code I found online I had the same results. They run correctly in IDLE but don't show anything (which they're suppoded to) from within QGIS.
    – GsetIT
    Feb 13, 2014 at 14:49
  • I have updated my answer however this is more a general Qt question and has nothing to do with QGIS. Also your code in your question is still really wrong, your dialog should look like mine. You should generate a .py file from your .ui file and import that. There are other ways but this is the recommended and the others do the same thing just in different ways.
    – Nathan W
    Feb 13, 2014 at 23:17
  • Thanks. I don't really know anything about python. I'm just learning by trial-and-error.
    – GsetIT
    Feb 14, 2014 at 7:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.