我无法在setWindowFlags
小部件上使用QDialog
。正在尝试隐藏关闭按钮或删除整个框架,以使用户无法使用默认方法将其关闭。
我尝试为我的课程在setupUi
函数内添加这两行之一:
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
和
self.setWindowFlag(QtCore.Qt.WindowCloseButtonHint, False)
,但都给了我同样的错误。
这是我的代码,可以进行更好的检查:
import os
import sys
import psycopg2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMessageBox
import backoffice
class Ui_Dialog(object):
def setupUi(self, Dialog):
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
Dialog.setObjectName("Dialog")
Dialog.resize(400, 274)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(self.resource_path("./images/pos_logo_small.png")), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
Dialog.setLayoutDirection(QtCore.Qt.RightToLeft)
Dialog.setStyleSheet("background:#a1d0f4;")
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setHorizontalSpacing(4)
self.formLayout.setObjectName("formLayout")
self.label_2 = QtWidgets.QLabel(Dialog)
font = QtGui.QFont()
font.setFamily("Shorooq_N1")
font.setPointSize(18)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.label_3 = QtWidgets.QLabel(Dialog)
font = QtGui.QFont()
font.setFamily("Shorooq_N1")
font.setPointSize(18)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
self.lineEdit.setSizePolicy(sizePolicy)
self.lineEdit.setStyleSheet("background:white;")
self.lineEdit.setObjectName("lineEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit_2.sizePolicy().hasHeightForWidth())
self.lineEdit_2.setSizePolicy(sizePolicy)
self.lineEdit_2.setStyleSheet("background:white;")
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_2.setObjectName("lineEdit_2")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)
self.closeToolButton = QtWidgets.QToolButton(Dialog)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.closeToolButton.sizePolicy().hasHeightForWidth())
self.closeToolButton.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Shorooq_N1")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.closeToolButton.setFont(font)
self.closeToolButton.setStyleSheet("background:red;color:white;")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(self.resource_path("./images/close-window-256.ico")), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
self.closeToolButton.setIcon(icon1)
self.closeToolButton.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.closeToolButton.setObjectName("closeToolButton")
self.closeToolButton.clicked.connect(self.exit_app)
self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.closeToolButton)
self.logInToolButton = QtWidgets.QToolButton(Dialog)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.logInToolButton.sizePolicy().hasHeightForWidth())
self.logInToolButton.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Shorooq_N1")
font.setPointSize(16)
self.logInToolButton.setFont(font)
self.logInToolButton.setStyleSheet("background:#0a74c4;color:white;")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(self.resource_path("./images/check-mark-11-256.ico")), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
self.logInToolButton.setIcon(icon2)
self.logInToolButton.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.logInToolButton.setObjectName("logInToolButton")
self.dialog = Dialog
self.logInToolButton.clicked.connect(self.log_in)
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.logInToolButton)
self.gridLayout.addLayout(self.formLayout, 1, 1, 1, 1)
self.label = QtWidgets.QLabel(Dialog)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 1, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def exit_app(self):
QtCore.QCoreApplication.instance().quit()
def log_in(self):
print('activated')
try:
connection = psycopg2.connect(user="*******",
password="********",
host="127.0.0.1",
port="5432",
database="********")
cursor = connection.cursor()
postgreSQL_select_Query = "select * from users"
cursor.execute(postgreSQL_select_Query)
print("Selecting rows from users table using cursor.fetchall")
user_records = cursor.fetchall()
print("Print each row and it's columns values")
input_username = self.lineEdit.text()
input_password = self.lineEdit_2.text()
for row in user_records:
if row[1] == input_username:
if row[2] == input_password:
backoffice.isAuthenticated = True
print('logged in ')
self.dialog.close()
else:
pass
print(input)
except (Exception, psycopg2.Error) as error:
print("Error while fetching data from PostgreSQL", error)
finally:
# closing database connection.
connection = psycopg2.connect(user="*********",
password="*******",
host="127.0.0.1",
port="5432",
database="*******")
cursor = connection.cursor()
if (connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Vision Store Manager"))
self.label_2.setText(_translate("Dialog", "إسم المستخدم"))
self.label_3.setText(_translate("Dialog", "كلمة المرور"))
self.closeToolButton.setText(_translate("Dialog", "خروج"))
self.logInToolButton.setText(_translate("Dialog", "تسجيل الدخول"))
self.label.setText(_translate("Dialog",
"<html><head/><body><p><img src=\":/images/images/logo_small.png\"/></p></body></html>"))
@staticmethod
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
import login_rcs_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
执行以下行时得到错误AttributeError: 'Ui_Dialog' object has no attribute 'setWindowFlags'
:self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
UPDATE
我改变了:
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
到
the_login = QtWidgets.QDialog
the_login.setWindowFlags(QtCore.Qt.FramelessWindowHint)
现在的错误是:TypeError: setWindowFlags(self, Union[Qt.WindowFlags, Qt.WindowType]): first argument of unbound method must have type 'QWidget'
答案 0 :(得分:1)
您继承错误。
声明中应如何显示?
类Ui_Dialog
应该继承QtWidgets.QDialog
的所有属性。您的Dialog
变量应为self
。您不应该使用Dialog
变量来调用函数。
您的主要错误:
class Ui_Dialog => class Ui_Dialog(QtWidgets.QDialog)
self.gridLayout = QtWidgets.QGridLayout(Dialog) => self.gridLayout = QtWidgets.QGridLayout(self)
#all should be changed like this
ui.setupUi(Dialog)=>ui.setupUi()
代码:
class Ui_Dialog(QtWidgets.QDialog):
def setupUi(self):
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setObjectName("Dialog")
self.resize(400, 274)
self.gridLayout = QtWidgets.QGridLayout(self)
... #your stuff
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
#Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi()
ui.show()
sys.exit(app.exec_())