I am trying to create a window with a scroll area with widgets. This works. I tried to add a simple filter function to this window. This also works. The only problem is that the widgets inside the scroll area don't keep their size, when some are hidden. Is there a way to make sure the widgets in the scroll area maintain their size?
from PyQt5 import QtWidgets, QtCore, QtGui
import sys
# ----------------------------------------------------------------------------
class test(QtWidgets.QDialog):
def __init__(self, *args, **kwargs):
super(test, self).__init__(*args, **kwargs)
self.main_layout = QtWidgets.QVBoxLayout(self)
self.label_widget = QtWidgets.QWidget()
self.label_layout = QtWidgets.QHBoxLayout()
self.label_widget.setLayout(self.label_layout)
self.main_layout.addWidget(self.label_widget)
self.filter_field = QtWidgets.QLineEdit()
self.label_layout.addWidget(self.filter_field)
self.refresh_pbutton = QtWidgets.QPushButton("Refresh")
self.label_layout.addWidget(self.refresh_pbutton)
self.scroll_area = QtWidgets.QScrollArea()
self.main_layout.addWidget(self.scroll_area)
self.refresh_pbutton.clicked.connect(self.refresh)
self.filter_field.textChanged.connect(self.filter)
self.populate()
# ----------------------------------------------------------------------------
def populate(self, *args, **kwargs):
self.widgets = []
self.scroll_widget = QtWidgets.QWidget()
self.scroll_widget.setAutoFillBackground(True)
self.scroll_widget.setStyleSheet("background-color:red;")
self.scroll_layout = QtWidgets.QVBoxLayout()
self.scroll_widget.setLayout(self.scroll_layout)
for i in range(1, 11):
widget = smallWidget(str(i))
self.widgets.append(widget)
self.scroll_layout.addWidget(widget)
self.scroll_area.setWidget(self.scroll_widget)
self.filter_field.setText("")
def refresh(self):
self.populate()
def filter(self):
filter_text = str(self.filter_field.text())
for widget in self.widgets:
if filter_text in widget.name:
widget.show()
else:
widget.hide()
class smallWidget(QtWidgets.QWidget):
def __init__(self, name, *args, **kwargs):
super(smallWidget, self).__init__(*args, **kwargs)
self.name = name
self.main_layout = QtWidgets.QVBoxLayout(self)
self.name_label = QtWidgets.QLabel(self.name)
self.main_layout.addWidget(self.name_label)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
a = test()
a.show()
sys.exit(app.exec_())
You should not add the widgets directly to scroll_layout but create another layout, and in that layout add the widgets and use addStretch() so that it does not occupy the height of the first layout but the necessary one.
def populate(self):
self.widgets = []
self.scroll_widget = QtWidgets.QWidget()
self.scroll_widget.setAutoFillBackground(True)
self.scroll_widget.setStyleSheet("background-color:red;")
self.scroll_layout = QtWidgets.QVBoxLayout()
self.scroll_widget.setLayout(self.scroll_layout)
lay = QtWidgets.QVBoxLayout() # <---
self.scroll_layout.addLayout(lay) # <---
self.scroll_layout.addStretch() # <---
for i in range(1, 11):
widget = smallWidget(str(i))
self.widgets.append(widget)
lay.addWidget(widget) # <---
self.scroll_area.setWidget(self.scroll_widget)
Update:
If you want the size of scroll_widget to be adjusted you must call adjustSize() an instant later with a QTimer since the geometry changes are not applied instantly
def filter(self, text):
for widget in self.widgets:
widget.setVisible(text in widget.name)
QtCore.QTimer.singleShot(0, self.scroll_widget.adjustSize)
Related
I would like to know is it possible to have a signal or something to know when the scroll is available and when it is no longer available each time the window is resized?
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Widget(QWidget):
def __init__(self, parent= None):
super(Widget, self).__init__()
widget = QWidget()
layout = QVBoxLayout(self)
for _ in range(10):
btn = QPushButton()
layout.addWidget(btn)
widget.setLayout(layout)
scroll = QScrollArea()
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scroll.setWidget(widget)
vLayout = QVBoxLayout(self)
vLayout.addWidget(scroll)
self.setLayout(vLayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Widget()
dialog.show()
app.exec_()
Since a QScrollBar is usually visible only when its range maximum is greater than 0, you can just check that value.
class Widget(QWidget):
def __init__(self, parent= None):
# ....
# note that the scroll area is now an instance member
self.scroll = QScrollArea()
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scroll.setWidget(widget)
self.scroll.verticalScrollBar().rangeChanged.connect(self.checkScrollBarRange)
vLayout = QVBoxLayout(self)
vLayout.addWidget(self.scroll)
self.setLayout(vLayout)
# the next is important when resizing the widget containing the scroll area
self.scroll.installEventFilter(self)
def eventFilter(self, source, event):
if source == self.scroll and event.type() == QEvent.Resize:
self.checkScrollBarRange()
return super().eventFilter(source, event)
def checkScrollBarRange(self):
if (self.scroll.verticalScrollBarPolicy() != Qt.ScrollBarAlwaysOff and
self.scroll.verticalScrollBar().maximum()):
print('vertical scroll bar IS visible')
else:
print('vertical scroll bar NOT visible')
This is a follow-up to this question of mine: Make QGroupBox selectable and clickable
My related question is how can I programmatically click on the first group upon startup?
I have tried to do this after implementing the solution provided in the original question using:
scrollLayout.itemAt(0).widget().click()
However, this did not work.
What am I missing?
Try it:
from PyQt5 import QtWidgets, QtGui, QtCore
class GroupBox(QtWidgets.QGroupBox):
clicked = QtCore.pyqtSignal(str, object)
def __init__(self, title):
super(GroupBox, self).__init__()
self.title = title
self.setTitle(self.title)
def mousePressEvent(self, event):
child = self.childAt(event.pos())
if not child:
child = self
self.clicked.emit(self.title, child)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
w = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
w.setLayout(layout)
self.setCentralWidget(w)
my_tree = QtWidgets.QTreeWidget()
layout.addWidget(my_tree)
alpha = QtWidgets.QTreeWidgetItem(my_tree, ['Alpha'])
beta = QtWidgets.QTreeWidgetItem(my_tree, ['Beta'])
alpha.addChild(QtWidgets.QTreeWidgetItem(['one']))
alpha.addChild(QtWidgets.QTreeWidgetItem(['two']))
beta.addChild(QtWidgets.QTreeWidgetItem(['first']))
beta.addChild(QtWidgets.QTreeWidgetItem(['second']))
my_tree.expandAll()
alpha.child(0).setSelected(True)
scroll = QtWidgets.QScrollArea()
layout.addWidget(scroll)
scrollLayout = QtWidgets.QVBoxLayout()
scrollW = QtWidgets.QWidget()
scroll.setWidget(scrollW)
scrollW.setLayout(scrollLayout)
scrollLayout.setAlignment(QtCore.Qt.AlignTop)
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll.setWidgetResizable(True)
for _ in range(5):
fooGroup = GroupBox(f'GroupBox_{_}')
fooGroup.setObjectName(f'fooGroup {_}')
fooGroup.clicked.connect(self.onFooGroupClick)
fooLayout = QtWidgets.QVBoxLayout()
fooGroup.setLayout(fooLayout)
fooItem1 = QtWidgets.QLabel("fooItem1", objectName="fooItem1")
fooItem1.setStyleSheet('background: #44ffff')
fooItem2 = QtWidgets.QLabel("fooItem2", objectName="fooItem2")
fooItem2.setStyleSheet('background: #ffff56;')
fooItem3 = QtWidgets.QLabel("fooItem3", objectName="fooItem3")
fooItem3.setStyleSheet('background: #ff42ff;')
fooLayout.addWidget(fooItem1)
fooLayout.addWidget(fooItem2)
fooLayout.addWidget(fooItem3)
scrollLayout.addWidget(fooGroup)
# scrollLayout.itemAt(0).widget().click() # ---
_fooGroup = scrollLayout.itemAt(0).widget() # +++
_fooGroup.clicked.emit('GroupBox_0', _fooGroup) # +++
def onFooGroupClick(self, title, obj):
print(f"Group: {title}; objectName=`{obj.objectName()}`")
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec_()
I'm having though time figuring out what kind of signal is emitted in following situation:
Basicly that's QScrollArea that holds multiple QTableWidgets:
class ScrollArea(QtGui.QScrollArea):
def __init__(self):
super(ScrollArea, self).__init__()
self.scroll_widget = QtGui.QWidget()
self.scroll_layout = QtGui.QVBoxLayout()
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setWidgetResizable(True)
self.__create_content()
self.setWidget(self._content_widget)
self.scroll_layout.addWidget(self)
self.scroll_widget.setLayout(self.scroll_layout)
def __create_content(self):
self._content_widget = QtGui.QWidget()
self._content_widget_layout = QtGui.QVBoxLayout()
self._content_widget.setLayout(self._content_widget_layout)
def add_item(self, item):
self._content_widget_layout.addWidget(item)
I'm using Plastique style for QApplication. As it can be seen from the above picture, when an item is clicked inside QScrollArea, blue border appears. What I would like to know is which signal is emitted when the border is drawn? I need this information so I can append a row to the selected QTableWidget whenever a button (on the left side) is clicked.
Also you can see that there is a 'x' inside each table, when 'x' is pressed that QTableWidget gets removed from QScrollArea. If there is a solution for previous problem, I could also remove QTableWidget depending on user selection rather than user clicking the 'x'.
To get the widget that has the focus you can use the focusChanged signal of QApplication:
from PyQt4 import QtCore, QtGui
class HorizontalHeader(QtGui.QHeaderView):
def __init__(self, parent=None):
super(HorizontalHeader, self).__init__(QtCore.Qt.Horizontal, parent)
self.button = QtGui.QToolButton(self, text="x")
self.sectionResized.connect(self.handleSectionResized)
def handleSectionResized(self):
last_ix = self.count() - 1
pos = QtCore.QPoint(self.sectionViewportPosition(last_ix) + self.sectionSize(last_ix) , 0)
self.button.move(pos)
def showEvent(self, event):
self.handleSectionResized()
super(HorizontalHeader, self).showEvent(event)
class TableView(QtGui.QTableView):
def __init__(self, *args, **kwargs):
super(TableView, self).__init__(*args, **kwargs)
header = HorizontalHeader(self)
header.button.clicked.connect(self.deleteLater)
self.setHorizontalHeader(header)
QtGui.qApp.focusChanged.connect(self.onFocusChanged)
def onFocusChanged(self, old, new):
if new == self:
self.deleteLater()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
scrollArea = QtGui.QScrollArea()
scrollArea.setWidgetResizable(True)
widget = QtGui.QWidget()
scrollArea.setWidget(widget)
lay = QtGui.QVBoxLayout(widget)
for i in range(10):
w = TableView()
model = QtGui.QStandardItemModel(4, 2, w)
w.setModel(model)
lay.addWidget(w)
scrollArea.show()
sys.exit(app.exec_())
I got the following Test-GUI.
There is a left layout and a right layout where i want to put buttons and other things onto. The button on the right should make a QFrame unhide or hide and all the widgets in it. This works.
But after the first two clicks, the layout is different.
The TableWidget on the left layout gets resized and the button is a bit more south.
Is there an easy way to fix this?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.main_widget = MainWidget()
self.setCentralWidget(self.main_widget)
self.show()
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.tab_widget = TabWidget()
self.debugger = Dialog()
self.layout.addWidget(self.tab_widget)
self.layout.addWidget(self.debugger)
self.setLayout(self.layout)
class TabWidget(QTabWidget):
def __init__(self):
super().__init__()
self.tab1 = Tab_1()
self.addTab(self.tab1, "Tab1")
class Tab_1(QWidget):
def __init__(self):
super().__init__()
# LEFT LAYOUT BEGIN
self.table = QTableWidget()
self.table.setRowCount(1)
self.table.setColumnCount(2)
self.table.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
self.table.resizeColumnsToContents()
left_hlayout = QHBoxLayout()
left_hlayout.addWidget(self.table)
# # LEFT LAYOUT END
#
# # RIGHT LAYOUT BEGIN
self.button_options = QPushButton('Options')
self.button_options.setCheckable(True)
self.button_options.toggled.connect(self.option_pressed)
right_vlayout = QVBoxLayout()
right_vlayout.addWidget(self.button_options)
# # RIGHT LAYOUT END
# MAIN LAYOUT BEGING
self.main_layout = QVBoxLayout()
self.horizontal_layout = QHBoxLayout()
self.horizontal_layout.addLayout(left_hlayout)
self.horizontal_layout.addLayout(right_vlayout)
self.main_layout.addLayout(self.horizontal_layout)
self.option = Options()
self.main_layout.addWidget(self.option)
self.setLayout(self.main_layout)
# MAIN LAYOUT END
def option_pressed(self):
if self.button_options.isChecked():
self.option.setVisible(True)
else:
self.option.setVisible(False)
class Options(QFrame):
def __init__(self):
super().__init__()
self.hide()
self.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
self.options_layout = QFormLayout()
self.options_label = QLabel('One')
self.options_lineedit = QLineEdit('Two')
self.options_layout.addRow(self.options_label, self.options_lineedit)
self.setLayout(self.options_layout)
class Dialog(QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedHeight(100)
pal = QPalette()
bgc = QColor(210, 210, 210)
pal.setColor(QPalette.Base, bgc)
self.setPalette(pal)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
can anybody tell me what's wrong in the following simple source code. In the picture below you can see my problem. I want a widget which consists of a menu and a QGLWidget, but the QGLWidget overlaid the menu. If I use QtGui.QWidget instead it works fine. How can I increase the space between these elements?
diagram 1:
code of QGLWidget :
class Profile(QtOpenGL.QGLWidget):
def __init__(self, parent = None):
super(Profile, self).__init__(parent)
def initializeGL(self):
GL.glClearColor(1.0, 1.0 , 1.0, 1.0)
def paintGL(self):
GL.glMatrixMode(GL.GL_MODELVIEW)
GL.glLoadIdentity()
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
code of test widget which works fine:
class TestWidget(QtGui.QWidget):
def __init__(self, parent = None):
super(TestWidget, self).__init__(parent)
editor = QtGui.QTextEdit()
grid = QtGui.QGridLayout()
grid.addWidget(editor, 1,1)
self.setLayout(grid)
code of main widget:
class ProfileDetectWidget(QtGui.QWidget):
def __init__(self, parent = None):
super(ProfileDetectWidget, self).__init__(parent)
self.ogl_widget = Profile()
# self.ogl_widget = TestWidget()
grid = QtGui.QGridLayout()
grid.addWidget(self.ogl_widget, 2,1)
self.createActions()
self.createMenus()
self.setLayout(grid)
self.resize(420,320)
def createActions(self):
self.openAct = QtGui.QAction('Open...', self)
def createMenus(self):
fileMenu = QtGui.QMenu("File", self)
fileMenu.addAction(self.openAct)
menubar = QtGui.QMenuBar(self)
menubar.addMenu(fileMenu)
if __name__ == '__main__':
app = QtGui.QApplication(["PyQt OpenGL"])
widget = ProfileDetectWidget()
widget.show()
app.exec_()
In the code of the main widget I changed
grid = QtGui.QGridLayout()
grid.addWidget(self.ogl_widget, 2,1)
to
space = QtGui.QSpacerItem(0,15)
grid = QtGui.QGridLayout()
grid.addItem(space)
grid.addWidget(self.ogl_widget, 2,1)
and it seems to work.