I have several checkboxes which control various procedures, once the code has run all checkboxes which were successful are coloured green e.g. self.cbMarkerDetection.setStyleSheet("color: green;") as it goes to the next job I want to reset the colour back to black. Obviously I could do self.cbMarkerDetection.setStyleSheet("color: black;") for every checkbox... but I'm hoping there is some way to do something like:
for checkbox in allcheckboxes:
self.checkbox.setStyleSheet("color: black;")
A simple coded example, two check boxes, when selected it goes green in colour, when I click the other check box I want all to change back to black, imagine checkboxes a-g, when checkbox (or a button if you like) is selected/checked the selected goes green when checkbox z (or a button) is selected/clicked all the existing check boxes turn black. As stated above, I could list all the checkboxes and set them black, but I'm looking for a quicker way with less code:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QCheckBox, QWidget
from PyQt5.QtCore import QSize
class ExampleWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(140, 40))
self.setWindowTitle("Checkbox")
self.a = QCheckBox("box1",self)
self.a.stateChanged.connect(self.clickBox)
self.a.move(20,20)
self.a.resize(320,40)
self.b = QCheckBox("box2",self)
self.b.stateChanged.connect(self.clickBox)
self.b.move(20,80)
self.b.resize(320,40)
def clickBox(self, state):
if state == QtCore.Qt.Checked:
print('Checked')
self.checkbox.setStyleSheet("color: green;")
else:
print('Unchecked')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ExampleWindow()
mainWin.show()
sys.exit( app.exec_() )
The simplest solution is to use findChildren():
for checkbox in self.findChildren(QCheckBox, options=Qt.FindDirectChildrenOnly):
checkbox.setStyleSheet("color: black;")
If you have other checkboxes in your widget that should not included in the list, you can create an empty subclass and use that for the filter:
class MyCheckBox(QCheckBox): pass
# ...
self.a = MyCheckBox("box1", self)
# etc...
Then you use findChildren with the custom class name:
for checkbox in self.findChildren(MyCheckBox, options=Qt.FindDirectChildrenOnly):
checkbox.setStyleSheet("color: black;")
Note that using fixed geometries is discouraged, and you should prefer layout managers instead.
Related
what i want to achieve
I am learning how to use pyqt5 with this project of mine.
I tried downloading something called BlurWindow but it kept giving me a parameter error so switched back to trying to use QGraphicBlurEffect but it blurs everything inside my MainWindow
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys
from PyQt5.uic import loadUi
from BlurWindow.blurWindow import blur
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi(r'D:\Workspace\Qt Designer\blur bg\blurtest.ui',self)
self.setAttribute(Qt.WA_TranslucentBackground)
hWnd = self.winId()
print(hWnd)
blur(hWnd)
self.setStyleSheet("background-color: rgba(0, 0, 0, 0)")
app=QApplication(sys.argv)
mainwindow=MainWindow()
widget=QtWidgets.QStackedWidget()
widget.setWindowOpacity(0.5)
widget.addWidget(mainwindow)
widget.setFixedHeight(600)
widget.setFixedWidth(800)
widget.show()
sys.exit(app.exec_())
BlurWindow uses system features to set the background of a top level window.
Your problem is that you're applying it to the wrong widget, which is a child widget, not a top level one. The top level has no "glass effect" set, so the result is that it won't have any effect applied on it.
The solution is simple: apply the effect to the top level window.
import sys
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
from BlurWindow.blurWindow import blur
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi(r'D:\Workspace\Qt Designer\blur bg\blurtest.ui', self)
self.setStyleSheet("""
MainWindow {
background-color: rgba(0, 0, 0, 0);
}
""")
app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(600)
widget.setFixedWidth(800)
blur(widget.winId()) # <---
widget.show()
sys.exit(app.exec_())
Note that:
QMainWindow is not supposed to be used as a child widget. You should switch to a basic QWidget (or other container widgets like QFrame), meaning that you should create a new "widget" in Designer and copy/paste the content of your previous window to it, otherwise loadUi() will throw an exception;
you should never apply generic style sheet properties to parent widgets, as you would get unexpected results (especially with complex widgets, like scroll areas); you should always use proper selectors (as I did above);
How do I create a drop-down widget, such as a drop-down QLabel, drop-down QTextBrowser, etc.?
For example, I log information in a QTextBrowser, but I don't want it taking up space on the screen. So I want to be able to click a QToolbutton and have a scrollable QTextBrowser drop-down. (A QComboBox would work too, but I can't just add each event as a separate item - I need the text to wrap, not be elided. Thus a drop-down QTextBrowser.)
Or, for example, I want a drop-down QLabel containing a picture, etc...
Create a QWidgetAction for the drop-down widget, and add it to the tool-button's menu:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QHBoxLayout(self)
self.button = QtGui.QToolButton(self)
self.button.setPopupMode(QtGui.QToolButton.MenuButtonPopup)
self.button.setMenu(QtGui.QMenu(self.button))
self.textBox = QtGui.QTextBrowser(self)
action = QtGui.QWidgetAction(self.button)
action.setDefaultWidget(self.textBox)
self.button.menu().addAction(action)
layout.addWidget(self.button)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(100, 60)
window.show()
sys.exit(app.exec_())
I have a QListWidget containing one item, I want the item's texts to be partially bold, so I created a QItemWidget for it. I use a QLabel in it to display the partially bold texts, the code is as follows:
ItemWidget.py
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QHBoxLayout, QLayout, QLabel
class ItemWidget(QtWidgets.QWidget):
def __init__(self):
super(ItemWidget, self).__init__()
self.setLayout(self.__get_layout())
def __get_layout(self) -> QHBoxLayout:
label = QLabel("<b>bold</b> texts")
widget_layout = QHBoxLayout()
widget_layout.addWidget(label)
widget_layout.setSizeConstraint(QLayout.SetFixedSize)
widget_layout.setContentsMargins(0, 0, 0, 0)
return widget_layout
Main.py
from PyQt5.QtWidgets import QListWidget, QWidget, QVBoxLayout, QApplication, QListWidgetItem
from ItemWidget import ItemWidget
class Window(QWidget):
def __init__(self):
super().__init__()
list_widget = QListWidget()
list_widget.itemClicked.connect(self.on_item_clicked)
vbox = QVBoxLayout()
vbox.addWidget(list_widget)
item_widget = ItemWidget()
item = QListWidgetItem()
list_widget.addItem(item)
list_widget.setItemWidget(item, item_widget)
self.setLayout(vbox)
def on_item_clicked(self):
print("Item clicked")
app = QApplication([])
window = Window()
window.show()
app.exec_()
This is the UI:
When I click on the item in the list, Item clicked should be printed to the output console. But the problem is, it only works if I don't click on the text:
The problem seems to disappear if I give a plain text to the QLabel in ItemWidget, such as label = QLabel("text"), why? What's the problem here?
Setting rich text content causes the mouse button release event to be always accepted (I suppose it's due to the fact that rich text can contain links).
The view must receive a mouse release event in order to call its mouseReleaseEvent() and eventually emit the clicked signal, but since the event was already accepted by the label, this won't happen.
If you don't need links in that text, just reset the text interaction flags:
label.setTextInteractionFlags(Qt.NoTextInteraction)
The following PyQt program produces a window containing a QTableView with a margin space to its bottom and right (but not top and left) - even though the main-window is told to adjust itself to its contents size:
I was expecting to see:
If I increase the size of the window from the original position, I see:
What is the purpose of that region? Can it be eliminated? If so, how?
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QVBoxLayout, QWidget, QApplication, QMainWindow, QTableView
class TableModel(QtCore.QAbstractTableModel):
def __init__(self):
super().__init__()
def data(self, index, role=None):
if role == Qt.DisplayRole:
return 42
def rowCount(self, index):
return 3
def columnCount(self, index):
return 4
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.table = QTableView()
self.model = TableModel()
self.table.setModel(self.model)
self.table.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
self.setCentralWidget(self.table)
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
The blank areas are normal and expected if you don't specify how the elements of the table should be resized. There are many different configurations to suit different use-cases, so you may need to experiment a little to get the exact behaviour you want.
The initial margins along the right and bottom edges are there to allow for scrollbars. If you don't want scrollbars, you can switch them off like this:
self.table.setHorizontalScrollBarPolicy(
QtCore.Qt.ScrollBarAlwaysOff)
self.table.setVerticalScrollBarPolicy(
QtCore.Qt.ScrollBarAlwaysOff)
However, the blank areas will still be there when the window is resized - but you can deal with that by setting the section resize mode, like this:
self.table.horizontalHeader().setSectionResizeMode(
QtWidgets.QHeaderView.Stretch)
self.table.verticalHeader().setSectionResizeMode(
QtWidgets.QHeaderView.Stretch)
This might result in an unexpected initial size for the window, so it may be advisable to set an appropriate default:
self.resize(400, 200)
For further details, see the documentation for QTableView and QHeaderView.
I have a very basic QMainWindow application that contains a menubar and a statusbar. When I hover over the menu the status message disappears. More precisely, the status message is cleared. I have no idea what is causing this behavior but it's resulting in a very difficult workaround for what I hoped to be trivial behavior.
This is problematic for the following reason:
I can make the message permanent by adding a QLabel widget to the QStatusBar, but then I get the awkward border. I don't want the border. The only way I know how to remove the border is via QStatusBar.setStyleSheet(). I am using a palette for my color scheme as opposed to a stylesheet so modifying the stylesheet messes up other colors. I also can't restore the original statusBar QLabel color when I make a modification via the stylesheet. I'm not the best at using stylesheets.
Is there a way to prevent the menu interaction from clearing the status message? If not, is there a way to remove the border from the StatusBar when adding a QLabel widget while preserving my palette (maybe not via stylesheets)?
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class win(QMainWindow):
def __init__(self,parent=None):
super(win,self).__init__(parent)
self.menubar = QMenuBar(self)
self.fileMenu = QMenu("File")
self.exitAction = QAction("Exit",self)
self.fileMenu.addAction(self.exitAction)
self.menubar.addMenu(self.fileMenu)
self.statusBar().showMessage("Hello")
self.connect(self.exitAction,SIGNAL("triggered()"), self.close)
if __name__ == "__main__":
app = QApplication(sys.argv)
GUI = win()
GUI.show()
app.exec_()
I got the same problem, and I found another way which is creating a new QLabel
self.myMessage = QtGui.QLabel()
self.myMessage.setText("Hello")
and add it as an widget to the status bar on the left
self.statusBar.addWidget(self.myMessage)
or on the right
self.statusBar.addPermanentWidget(self.myMessage)
Basically, each widget you hover over sets the status bar text to their statusTip property even when that property is an empty string.
For QMenu, the text is stored in the menuAction action status tip, so, you can have a text instead of just clearing the status bar with something like this:
self.fileMenu.menuAction().setStatusTip("File Menu is hovered")
To prevent anything to change the status bar, you can probably install an eventFilter on the status bar and filter out all QStatusTipEvent.
Just to update Lazywii's answer regarding using a QLabel. That code didn't work exactly as is so maybe there have been some changes since 2016 but what did work in 2020 on PyQt5 is:
self.myMessage = QtWidgets.QLabel()
self.myMessage.setText("My message not affected by tooltips from hovering")
self.statusbar.addWidget(self.myMessage)
One complete example
# Created by BaiJiFeiLong#gmail.com at 2022/2/15 22:27
from PySide2 import QtWidgets, QtCore, QtGui
class StatusTipFilter(QtCore.QObject):
def eventFilter(self, watched: QtCore.QObject, event: QtCore.QEvent) -> bool:
if isinstance(event, QtGui.QStatusTipEvent):
return True
return super().eventFilter(watched, event)
app = QtWidgets.QApplication()
window = QtWidgets.QMainWindow()
window.menuBar().addMenu("File")
window.statusBar().showMessage("Ready")
window.menuBar().installEventFilter(StatusTipFilter(window))
window.show()
app.exec_()
And to answer the portion about removing the border from the statusbar: self.statusbar().setStyleSheet("QStatusBar::item{ border: 0px solid black };") does the trick. It is important to setStyleSheet only on the statusbar object and not the entire application.