PyQT5 - setUnifiedTitleAndToolBarOnMac - pyqt

I have a question about setUnifiedTitleAndToolBarOnMac . I understand this is only available for the QMainWindow.
Is there a way to propagate this to other widgets. With below code , the tile and toolbar of the QWidget is still displayed(twice).
class Ui_MainWindow(object):
def show_form(self):
self.EditLabel = QtWidgets.QWidget()
self.EditLabel.setWindowTitle('Form Title')
self.EditLabel.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
MainWindow.setWindowTitle("MainWindow")
MainWindow.setUnifiedTitleAndToolBarOnMac(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setToolTip("")
self.centralwidget.setObjectName("centralwidget")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(80, 210, 113, 32))
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.pushButton_2.clicked.connect(self.show_form)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.pushButton_2.setText(_translate("MainWindow", "show"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Looks like i can solve this by setting
self.EditLabel.setWindowFlags(Qt.Dialog)
That's good enough for what i wanted.

Related

How to close QDialog Window with pushButton?

i am currently working on this gui project. and i create a settings window but i cant make it close after pushing the buttons. i tried use self.close(), self.accept(), self.reject() methods. When i click the button nothing happens. Here is my code, which is simplified :
from PyQt5.QtWidgets import (QLineEdit, QWidget, QMenu, QMenuBar, QAction,
QMainWindow, QApplication, QDialog, QPushButton, QVBoxLayout)
from PyQt5.QtCore import QSize, QRect, QMetaObject, QCoreApplication, QObject, Qt
class Ui_Dialog(QDialog):
def __init__(self):
super(Ui_Dialog, self).__init__()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(100, 100)
self.verticalLayout= QVBoxLayout(Dialog)
self.lineEdit = QLineEdit(Dialog)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.pushButton = QPushButton(Dialog)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton_2 = QPushButton(Dialog)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2)
self.pushButton.clicked.connect(self.close)
self.pushButton_2.clicked.connect(self.save)
self.retranslateUi(Dialog)
QMetaObject.connectSlotsByName(Dialog)
def save(self):
global setting
setting = self.lineEdit.text()
self.done(1)
self.accept()
self.deleteLater()
self.destroy()
self.reject()
self.close()
def retranslateUi(self, Dialog):
_translate = QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Settings"))
self.pushButton_2.setText(_translate("Dialog", "Save"))
self.pushButton.setText(_translate("Dialog", "Cancel"))
class Ui_MainWindow(QObject):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1250, 609)
MainWindow.setMinimumSize(QSize(833, 600))
self.centralWidget = QWidget(MainWindow)
MainWindow.setCentralWidget(self.centralWidget)
self.centralWidget.setObjectName("centralwidget")
self.menubar = QMenuBar(MainWindow)
self.menubar.setGeometry(QRect(0, 0, 906, 21))
self.menubar.setObjectName("menubar")
self.menuOptions = QMenu(self.menubar)
self.menuOptions.setObjectName("menuOptions")
MainWindow.setMenuBar(self.menubar)
self.actionSettings = QAction(MainWindow)
self.actionSettings.setObjectName("actionSettings")
self.menuOptions.addAction(self.actionSettings)
self.menubar.addAction(self.menuOptions.menuAction())
self.actionSettings.triggered.connect(self.preferences)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "closinDialogSample"))
self.menuOptions.setTitle(_translate("MainWindow", "Options"))
self.actionSettings.setText(_translate("MainWindow", "Settings"))
def preferences(self):
self.preferences_dialog = QDialog()
self.preferences_dialog.ui = Ui_Dialog()
self.preferences_dialog.ui.setupUi(self.preferences_dialog)
self.preferences_dialog.setAttribute(Qt.WA_DeleteOnClose)
self.preferences_dialog.setModal(True)
self.preferences_dialog.show()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Thank you for reading.

How to make Qlistwidgetitems checked automatically if same items are in the specified list?

Below is the example code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(394, 356)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(60, 30, 241, 192))
self.listWidget.setObjectName("listWidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(130, 250, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 394, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
players = ["Player 1","Player 2","Player 3","Player 4"]
for i in players:
item = QtWidgets.QListWidgetItem(i)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.listWidget.addItem(item)
self.selected_list = ["Player 1", "Player 4"]
self.pushButton.clicked.connect(self.find_the_list)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
def find_the_list(self):
print("OKAY")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The players = ["Player 1","Player 2","Player 3","Player 4"] list has been added in to the QListWidgetItem as UserCheckable. And the second list self.selected_list = ["Player 1", "Player 4"] is have tow items and the second list is changeable. Now I want to check the boxes of QlistWidgetitems if the second list have the matched items.
You could do something like this:
def find_the_list(self):
for i in range(self.listWidget.count()):
item = self.listWidget.item(i)
if item.text() in self.selected_list:
item.setCheckState(QtCore.Qt.Checked)
else:
item.setCheckState(QtCore.Qt.Unchecked)

Drag&Drop from lineEdit to pushbutton in PyQt5 [duplicate]

This question already has an answer here:
PyQt4 - Dragging and dropping files into QPushButton
(1 answer)
Closed 3 years ago.
I want to drag plain text from lineEdit widget to drop pushbutton label in PyQt5. Here is the code:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(873, 663)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton2= QtWidgets.QPushButton(self.centralwidget)
self.pushButton2.setGeometry(QtCore.QRect(280, 40, 93, 28))
self.pushButton2.setObjectName("pushButton2")
self.pushButton2.setAcceptDrops(True)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(120, 200, 113, 22))
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.lineEdit.setDragEnabled(True)
self.retranslateUi(MainWindow)
class pushButton2(QtWidgets.QPushButton):
def __init__(self, title, parent):
super().__init__(title, parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasFormat('text/plain'):
event.accept()
else:
event.ignore()
def dropEvent(self, event):
self.setText(event.mimeData().text())
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton2.setText(_translate("MainWindow", "PushButton2"))
self.lineEdit.setText(_translate("MainWindow", "k"))
if __name__ == "__main__":
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
app.exec_()
I would expect to drag text from lineEdit to show as pushbutton label but it did not accepted , why ?
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# class pushButton2(QtWidgets.QPushButton):
class PushButton(QtWidgets.QPushButton): # <---
def __init__(self, title, parent):
super().__init__(title, parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()
def dropEvent(self, e):
self.setText(e.mimeData().text())
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(873, 663)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# self.pushButton2= QtWidgets.QPushButton(self.centralwidget)
self.pushButton2 = PushButton("PushButton2", self.centralwidget) # <---
self.pushButton2.setGeometry(QtCore.QRect(120, 140, 93, 28))
self.pushButton2.setObjectName("pushButton2")
# self.pushButton2.setAcceptDrops(True)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(120, 200, 113, 22))
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.lineEdit.setDragEnabled(True)
self.retranslateUi(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton2.setText(_translate("MainWindow", "PushButton2"))
self.lineEdit.setText(_translate("MainWindow", "k"))
class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
MainWindow = ExampleApp()
MainWindow.show()
app.exec_()

PyQt5 set Mainwindow background transparent

I am attempting to make the top border of my app semi transparent (rounded edges) by using the partially transparent PNG below:
This does not work and ends up having the corners filled in when the app is launched. A simplified code is below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(819, 682)
MainWindow.setMinimumSize(QtCore.QSize(819, 682))
MainWindow.setMaximumSize(QtCore.QSize(819, 682))
MainWindow.setWindowOpacity(1.0)
MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.TopBarFrame = QtWidgets.QFrame(self.centralWidget)
self.TopBarFrame.setGeometry(QtCore.QRect(-1, -1, 821, 31))
self.TopBarFrame.setStyleSheet("background-image:url(\"TopBar.png\")")
self.TopBarFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.TopBarFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.TopBarFrame.setObjectName("TopBarFrame")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Using the .setAttribute(QtCore.Qt.WA_TranslucentBackground) only makes the whole window black. All help appreciated.
Why do you useMainWindow.setWindowOpacity(1.0)?This completely changes window as well as all widget opacity(transparency).And if you want round corner then dont use any image just writeborder-radius:10px;(for example)this do the same.And
MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground) works if you set your window to frameless(borderless) otherwise just a black screen appears.After some modification in your code this is final result
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(819, 682)
MainWindow.setMinimumSize(QtCore.QSize(819, 682))
MainWindow.setMaximumSize(QtCore.QSize(819, 682))
MainWindow.setWindowFlags(
QtCore.Qt.FramelessWindowHint
| QtCore.Qt.WindowStaysOnTopHint )
MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.TopBarFrame = QtWidgets.QFrame(self.centralWidget)
self.TopBarFrame.setGeometry(QtCore.QRect(-1, -1, 821, 31))
self.TopBarFrame.setStyleSheet('''background:red;
border-top-left-radius:15px;
border-top-right-radius:15px;''')
self.TopBarFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.TopBarFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.TopBarFrame.setObjectName("TopBarFrame")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
let me know if you have any problem.

Pyqt5: builtins.AttributeError: 'QDialog' object has no attribute 'setCentralWidget'

I converted ui file from Pyqt5 designer:
Testing1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(380, 180, 112, 34))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Test1.py < is meant to add methods etc. etc.
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from testing1 import Ui_MainWindow
class myprog(Ui_MainWindow):
def __init__ (self, dialog):
Ui_MainWindow.__init__(self)
self.setupUi(dialog)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
test1 = myprog(dialog)
dialog.show()
sys.exit(app.exec_())
If I run Testin1.py it's all ok, but if run second script, test1.py, i get following message:
> builtins.AttributeError: 'QDialog' object has no attribute
> 'setCentralWidget'
I am really confused what to do, I would really really appreciate, if you could help me solve this problem. Any help is more than welcome.
In Testing1.py file, you create a MainWindow.
So in your Test1.py file, you should change the code
dialog = QtWidgets.QDialog()
to
dialog = QtWidgets.QMainWindow()

Resources