I want to change border radius of my PushButton lesson1
Tried to create a class Button with setStyleSheet in __init__ and create an object.
import sys
from pyto import *
from PyQt5 import QtCore, QtGui, QtWidgets
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.lesson1 = Button()
class Button(QtWidgets.QPushButton):
def __init__(self, parent = None):
super(Button, self).__init__(parent)
self.setStyleSheet('border-radius: 15px;')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())
No errors, but setStyleSheet doesn't work.
The code self.ui.lesson1 = Button() does not replace the self.ui.lesson1 created by Qt Designer, it only causes the new Button() to be assigned to the name self.ui.lesson1. So if you want to set the stylesheet it is not necessary to create another class:
# ...
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# self.ui.lesson1 = Button()
self.ui.lesson1.setStyleSheet('border-radius: 15px; background-color: red;')
# ...
I have added the background color to make the radius visible.
Related
I have a project with following structure:
main.py
from . import dialog_util
from . import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.btn_new.clicked.connect(self.openDialogBox)
def openDialogBox(self):
self.dialog = dialog_util.CustomDialog()
self.dialog.exec()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
sys.exit(app.exec())
dialog_util.py
from . import Ui_CustomDialog
class CustomDialog(QDialog, Ui_CustomDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
UI for main window and dialog box are in Ui_MainWindow.py and Ui_CustomDialog.py respectively. How can I add a shadow effect to the dialog box?
I found some code on here that shows an example of how you can get the window to resize when the widget is hidden, and it works for me. Here is the code:
from PyQt4 import QtCore, QtGui
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
super(MainWindow, self).__init__()
self.button = QtGui.QPushButton('Show/Hide')
self.button.setCheckable(True)
self.frame = QtGui.QFrame()
self.frame.setFixedHeight(100)
self.layout = layout = QtGui.QVBoxLayout()
layout2 = QtGui.QVBoxLayout()
self.setLayout(layout)
self.frame.setLayout(layout2)
layout.addWidget(self.button)
layout.addWidget(self.frame)
layout.addStretch(1)
layout2.addWidget(QtGui.QLabel('Yoyoyo'))
self.button.toggled.connect(self.clickAction)
def startup(self):
self.show()
sys.exit(self.app.exec_())
def clickAction(self):
checked = self.button.isChecked()
if checked:
self.frame.show()
else:
self.frame.hide()
QtCore.QTimer.singleShot(0, self.resizeMe)
def resizeMe(self):
self.resize(self.minimumSizeHint())
if __name__ == "__main__":
myApp = MainWindow()
myApp.startup()
I then tried to modify this to match my existing code by separating the mainWindow class and the widget class. Here is the code that does that.
from PySide import QtGui,QtCore
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.w = testW(self)
self.setCentralWidget(self.w)
self.show()
class testW(QtGui.QWidget):
def __init__(self,parent):
super(testW,self).__init__()
self.parent = parent
self.button = QtGui.QPushButton('Show/Hide')
self.button.setCheckable(True)
self.button.setChecked(True);
self.frame = QtGui.QFrame()
self.frame.setFixedHeight(100)
self.layout = layout = QtGui.QVBoxLayout()
layout2 = QtGui.QVBoxLayout()
self.setLayout(layout)
self.frame.setLayout(layout2)
layout.addWidget(self.button)
layout.addWidget(self.frame)
layout.addStretch(1)
layout2.addWidget(QtGui.QLabel('Yoyoyo'))
self.button.toggled.connect(self.clickAction)
def clickAction(self):
checked = self.button.isChecked()
if checked:
self.frame.show()
else:
self.frame.hide()
QtCore.QTimer.singleShot(0, self.resizeMe)
def resizeMe(self):
self.resize(self.minimumSizeHint())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myApp = MainWindow()
sys.exit(app.exec_())
#time.sleep(1)
Running the first code does what I want it to. After I hide the widget, the window resizes to the correct size. The second implementation of the code does not shrink and expand the window when I hide and show the widget. Is this because the MainWindow is in a separate class?
Use size policies for your widgets. For your example you can change UI creation code as follows:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.w = testW(self)
self.w.setSizePolicy(
QtWidgets.QSizePolicy.MinimumExpanding,
QtWidgets.QSizePolicy.MinimumExpanding
)
self.setCentralWidget(self.w)
self.show()
Please note new setSizePolicy call which say Qt layout engine how to change the size of your widget according to its content.
Unfortunately QMainWindow does not respect sizeHint automatically, but it is calculated properly, so you can adjustSize manually:
def clickAction(self):
checked = self.button.isChecked()
if checked:
self.frame.show()
else:
self.frame.hide()
QtCore.QTimer.singleShot(0, self.parent.adjustSize)
You do not need to resize your widget itself, because it will be resized according to the policy. Even sizeHint will be calculated automatically so you need only to call adjustSize of QMainWindow.
PS: I used PySide2 instead of PySide so the imports are different a little bit:
from PySide2 import QtWidgets, QtCore
I have an UI designed in QT Designer. It is formed from three tabs:
1 - Test; 2 - Train Haar; 3 - Train Hog;
In every tabs I have some buttons, or some lines, or some widgets, but when I create the code to add functions to those buttons or widgets I want to have 3 classes for every tabs, one class only for first tab, one class only for second tab and so on..
And a fourth class who call all three classes and compose my UI. I do not know how to do this, I need every classes to inherit from QMainWindow? I need to setupUi in every class?
This is my current code:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from Qt_designer_UI import Ui_MainWindow
class Test(QtWidgets.QMainWindow):
def __init__(self):
super(Test, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Train_Haar(QtWidgets.QMainWindow):
def __init__(self):
super(Train_Haar, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Train_HOG(QtWidgets.QMainWindow):
def __init__(self):
super(Train_HOG, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Compose_UI(QtWidgets.QMainWindow):
def __init__(self):
super(Compose_UI, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
Test()
Train_Haar()
Train_HOG
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Compose_UI()
window.show()
sys.exit(app.exec_())
I need every classes to inherit from QMainWindow?
No, it is not necessary since it is a widget that will be inside the tabs, and that widget can be of any type.
I need to setupUi in every class?
It is only obligatory to call setupUi if you use a generated class with the help of pyuic and Qt Designer, in your case the widgets that are in each tab are generated with Qt Designer? I see that I do not
Keeping in mind that your .ui is
a possible solution is:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from Qt_designer_UI import Ui_MainWindow
class Test(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Test"))
class Train_Haar(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Train_Haar, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Train_Haar"))
class Train_HOG(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Train_HOG, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Train_HOG"))
class Compose_UI(QtWidgets.QMainWindow):
def __init__(self):
super(Compose_UI, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
test = Test()
train_haar = Train_Haar()
train_hog = Train_HOG()
for w, tab in zip(
(test, train_haar, train_hog), (self.ui.tab1, self.ui.tab2, self.ui.tab3)
):
lay = QtWidgets.QVBoxLayout(tab)
lay.addWidget(w)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Compose_UI()
window.show()
sys.exit(app.exec_())
I open the dialog from the main window, where by clamping the keys, I fill the line with their names. The problem is that I can not understand where you need to do a cycle of checking all the keys on their state. Maybe there is another way to get the keys pressed? Or where you need to listen to the clamping so that the dialog box does not hang and the string is updated.
MainWindow:
def showBindings(self, param):
from dialogs import KeyBindingsDialog
self.dialog = KeyBindingsDialog()
self.dialog.show()
Dialog:
class KeyBindingsDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(KeyBindingsDialog, self).__init__(parent)
self.ui = KeyBindings()
self.ui.setupUi(self)
Use QKeySequenceEdit:
from PyQt5 import QtCore, QtGui, QtWidgets
class KeySequenceEdit(QtWidgets.QKeySequenceEdit):
def keyPressEvent(self, event):
super(KeySequenceEdit, self).keyPressEvent(event)
seq_string = self.keySequence().toString(QtGui.QKeySequence.NativeText)
if seq_string:
last_seq = seq_string.split(",")[-1].strip()
le = self.findChild(QtWidgets.QLineEdit, "qt_keysequenceedit_lineedit")
self.setKeySequence(QtGui.QKeySequence(last_seq))
le.setText(last_seq)
self.editingFinished.emit()
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self._keysequenceedit = KeySequenceEdit(editingFinished=self.on_editingFinished)
button = QtWidgets.QPushButton("clear", clicked=self._keysequenceedit.clear)
hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self._keysequenceedit)
hlay.addWidget(button)
#QtCore.pyqtSlot()
def on_editingFinished(self):
sequence = self._keysequenceedit.keySequence()
seq_string = sequence.toString(QtGui.QKeySequence.NativeText)
print("sequence: ", seq_string)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Due to some restrictions image has to be in the class QMainWindow and scrollbars have to be the QGraphicsView class.
This means that I have to add image in QGraphicsView class through QMainWindow class. "exit.png" exists in the folder from where I run this code.
What is the proper way to add this picture?
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene = QtGui.QGraphicsScene(self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)
self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
print "sdsads"
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
centralwidget = Window()
self.setCentralWidget(centralwidget)
centralwidget.scene.image = QtGui.QImage("exit.png")
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
For this case the solution is to use a QGraphicsPixmapItem:
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
centralwidget = Window()
self.setCentralWidget(centralwidget)
centralwidget.scene.addPixmap(QtGui.QPixmap("exit.png"))
# or
# item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap("exit.png"))
# centralwidget.scene.addItem(item)