I have a QScrollArea Widget, which starts empty;
It has a vertical layout, with a QGridLayout, and a vertical spacer to keep it at the top, and prevent it from stretching over the whole scroll area;
Elsewhere in the program, there is a QTextEdit, which when changed, has its contents scanned for "species" elements, and then they are added to the QGridLayout. Any species elements which have been removed are removed too. This bit works;
I have turned the vertical scrollbar on all the time, so that when it appears it does not sit on top of the other stuff in there. Note that the scroll bar is larger than the scroll box already though, despite not needing to be.
This is the problem. The scroll area seems to be preset, and i cannot change it. If i add more rows to the QGridLayout, the scroll area doesn't increase in size.
Instead, it stays the same size, and squeezes the QGridLayout, making it look ugly (at first);
And then after adding even more it becomes unusable;
Note that again, the scroll bar is still the same size as in previous images. The first two images are from Qt Designer, the subsequent 3 are from the program running.
If I resize the window so that the QScrollArea grows, then I see this:
Indicating that there's some layout inside the scroll area that is not resizing properly.
My question is; what do I need to do to make the scrollable area of the widget resize dynamically as I add and remove from the QGridLayout?
If you're coming here from Google and not having luck with the accepted answer, that's because you're missing the other secret invocation: QScrollArea::setWidget. You must create and explicitly identify a single widget which is to be scrolled. It's not enough to just add the item as a child! Adding multiple items directly to the ScrollArea will also not work.
This script demonstrates a simple working example of QScrollArea:
from PySide.QtGui import *
app = QApplication([])
scroll = QScrollArea()
scroll.setWidgetResizable(True) # CRITICAL
inner = QFrame(scroll)
inner.setLayout(QVBoxLayout())
scroll.setWidget(inner) # CRITICAL
for i in range(40):
b = QPushButton(inner)
b.setText(str(i))
inner.layout().addWidget(b)
scroll.show()
app.exec_()
The documentation provide an answer :
widgetResizable : bool
This property holds whether the scroll area should resize the view widget.
If this property is set to false (the default), the scroll area honors the size of its widget.
Set it to true.
Why don't you use a QListView for your rows, it will manage all the issues for you? Just make sure that after you add it you click on the Class (top right window of designer) and assign a layout or it wont expand properly.
I use a QLIstWidget inside a QScrollArea to make a scrollable image list
Try this for adding other objects to the list, this is how I add an image to the list.
QImage& qim = myclass.getQTImage();
QImage iconImage = copyImageToSquareRegion(qim, ui->display_image->palette().color(QWidget::backgroundRole()));
QListWidgetItem* pItem = new QListWidgetItem(QIcon(QPixmap::fromImage(iconImage)), NULL);
pItem->setData(Qt::UserRole, "thumb" + QString::number(ui->ImageThumbList->count())); // probably not necessary for you
QString strTooltip = "a tooltip"
pItem->setToolTip(strTooltip);
ui->ImageThumbList->addItem(pItem);
Update on Artfunkel's answer:
Here's a PySide6 demo that uses a "Populate" button to run the for loop adding items to the scroll area. Each button will also delete itself when clicked.
from PySide6.QtWidgets import *
app = QApplication([])
scroll = QScrollArea()
scroll.setWidgetResizable(True) # CRITICAL
inner = QFrame(scroll)
inner.setLayout(QVBoxLayout())
scroll.setWidget(inner) # CRITICAL
def on_remove_widget(button):
button.deleteLater()
def populate():
for i in range(40):
b = QPushButton(inner)
b.setText(str(i))
b.clicked.connect(b.deleteLater)
inner.layout().addWidget(b)
b = QPushButton(inner)
b.setText("Populate")
b.clicked.connect(populate)
inner.layout().addWidget(b)
scroll.show()
app.exec()
I am using PyQt based on Qt4. My Editor is PyCharm 2017.3 and my python version is 3.4. I am scraping some text from a website. I am trying to align that text to the center of the cell in a QTableWidget.
item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter)
self.tableWidget.setItem(x, 2,item)
Therefore while putting the item in the cell, I am trying to align it as per the documentation. The problem is that the data is not showing up.
It did show up when I removed setTextAlignment method as shown below
item = QTableWidgetItem(scraped_age)
self.tableWidget.setItem(x, 2,item)
This line of code:
item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter)
will not work properly, because it throws away the item it creates before assigning it to the variable. The variable will in fact be set to None, which is the return value of setTextAlignment(). Instead, you must do this:
item = QTableWidgetItem(scraped_age) # create the item
item.setTextAlignment(Qt.AlignHCenter) # change the alignment
This didn't work for me, and I'm not sure if it is because I'm using PyQt5 or it i did something wrong. I was trying to find something similar but for the whole table, and i finally stumbled upon something that worked and lets you center every cells or just one column at a time.
You have to use the delegate method:
#You're probably importing QtWidgets to work with the table
#but you'll also need QtCore for the delegate class
from PyQt5 import QtCore, QtWidgets
class AlignDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(AlignDelegate, self).initStyleOption(option, index)
option.displayAlignment = QtCore.Qt.AlignCenter
After implementing this in your code, you can add the following to your main window class or wherever the table is defined:
delegate = AlignDelegate(self.tableWidget)
self.tableWidget.setItemDelegateForColumn(2, delegate) #You can repeat this line or
#use a simple iteration / loop
#to align multiple columns
#If you want to do it for all columns:
#self.tableWidget.setItemDelegate(delegate)
Know this is an old question, but hope it can help someone else.
Bit late to the party but for those of you wondering how to do this on pyqt5
table = QTableWidgetItem() #QTWidgets.QTableWidgetItem() if importing QWidget from PyQt5
table.setTextAlignment(number)
setTextAlignment takes an int for the argument (alignment). Put the number in to get the result:
0:left
1:left
2:right
3:right
4:centre
I created my own CustomDelegate class derived from QStyledItemDelegate:
class CustomDelegate(QStyledItemDelegate):
def __init__(self, parent):
super().__init__(parent)
def createEditor(self, parent, option, index):
editor = QWidget(parent)
editor_hlayout = QHBoxLayout(editor)
button = QPushButton()
line_edit = QLineEdit()
editor_hlayout.addWidget(button)
editor_hlayout.addWidget(line_edit)
return editor
def setEditorData(self, editor, index):
model_data = index.model().data(index, Qt.EditRole)
editor.layout().itemAt(1).widget().setText(model_data) # Set line_edit value
def setModelData(self, editor, model, index):
editor_data = editor.layout().itemAt(1).widget().text() # Get line_edit value
model.setData(index, editor_data, Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
I set it for the last column of the QTableWidget my_table :
my_delegate = CustomDelegate(my_window)
my_table.setItemDelegateForColumn(my_table.columnCount()-1, my_delegate)
To be precise, my goal is to edit the table widget item size when double-clicking it so that it fits the editor size and properly displays it, then edit the table widget item size right after exiting editor mode so that it fits its text size back again.
To that end, I added the lines index.model().setData(index, editor.sizeHint(), Qt.SizeHintRole) in createEditor method and model.setData(index, QTableWidgetItem(editor_data).sizeHint(), Qt.SizeHintRole) in setModelData method.
The problem is that QTableWidgetItem(editor_data).sizeHint() returns (-1, -1) size. I also tried with QTextDocument(editor_data).size() but it does not fit the text width (it is slightly smaller).
How to get the good text size hint ?
Also, somebody told me about reducing the editor minimum size hint to avoid editing the QTableWidgetItem size at all. Does it solve my problem and how to perform it ?
The editor of a item view should not change the size of its index. The only cases for which this is considered valid is for persistent editors and index widgets, but due to their nature it makes sense: they are expected to persist on the view, and not only it's acceptable that they require the view to eventually expand their row or column, but also necessary in order to avoid editors hiding other items (or editors).
Any change in the section sizes can be potentially very demanding to the view, especially if it has lots of data and any of the header uses the ResizeToContents mode, that's why the default factory editors (QLineEdit, QDate/TimeEdit and Q[Double]SpinBox) don't update the index sizes but eventually extend their geometry temporarily.
I would suggest to follow this practice, and eventually update the geometry in updateEditorGeometry() according to the editor position.
In order to optimize the available space, you could use some precautions:
explicitly set 0 margins for the layout;
minimize the spacing between widgets;
disable the frame of the QLineEdit (as the default editor does);
use QToolButton instead of QPushButton, since it can usually be made smaller than the latter; this also makes it easier some focus aspects, as QToolButton doesn't accept focus by clicking;
Also note that:
the editor should use setAutoFillBackground(True) otherwise parts of the underlying items might be visible;
the line edit should probably have the focus on default, you can use setFocusProxy() on the editor, so that the line edit gets focused when the parent does;
you should not use the layout to access the line edit, instead create a reference for it in the editor object;
if you still want to make the editor border visible, use a proper stylesheet;
def createEditor(self, parent, option, index):
editor = QWidget(parent)
editor.setAutoFillBackground(True)
editor_hlayout = QHBoxLayout(editor)
editor_hlayout.setContentsMargins(0, 0, 0, 0)
editor_hlayout.setSpacing(1)
button = QToolButton()
editor.line_edit = QLineEdit(frame=False)
editor_hlayout.addWidget(button)
editor_hlayout.addWidget(editor.line_edit)
editor.setFocusProxy(editor.line_edit)
# eventually (see note)
editor.setObjectName('delegateEditor')
editor.setStyleSheet('''
#delegateEditor {
border: 1px solid palette(mid);
background: palette(base);
}
''')
return editor
def setEditorData(self, editor, index):
model_data = index.model().data(index, Qt.EditRole)
editor.line_edit.setText(model_data)
def setModelData(self, editor, model, index):
editor_data = editor.line_edit.text()
model.setData(index, editor_data, Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
super().updateEditorGeometry(editor, option, index)
editor.resize(editor.sizeHint().width(), editor.height())
rect = editor.geometry()
parentRect = editor.parent().rect()
if not parentRect.contains(rect):
if rect.right() > parentRect.right():
rect.moveRight(parentRect.right())
if rect.x() < parentRect.x():
rect.moveLeft(parentRect.x())
if rect.bottom() > parentRect.bottom():
rect.moveBottom(parentRect.bottom())
if rect.y() < parentRect.y():
rect.moveTop(parentRect.y())
editor.setGeometry(rect)
Note: if you do use stylesheets, it's possible that the line edit will partially draw over the border; in that case, use editor_hlayout.setContentsMargins(0, 0, 1, 0).
That said, if you really want to update the view sizes, you still can, but it can be a bit tricky.
The trick is to keep track of the editor and its index, manually resize the sections in order to fit its size, and then restore those sizes when the editor is destroyed.
class CustomDelegate(QStyledItemDelegate):
def __init__(self, parent):
self.editorData = {}
super().__init__(parent)
def createEditor(self, parent, option, index):
editor = QWidget(parent)
editor.setAutoFillBackground(True)
editor_hlayout = QHBoxLayout(editor)
editor_hlayout.setContentsMargins(0, 0, 0, 0)
editor_hlayout.setSpacing(1)
button = QToolButton()
editor.line_edit = QLineEdit(frame=False)
editor_hlayout.addWidget(button)
editor_hlayout.addWidget(editor.line_edit)
editor.setFocusProxy(editor.line_edit)
view = option.widget
# store the editor, the view and also the current sizes
self.editorData[index] = (editor, view,
view.horizontalHeader().sectionSize(index.column()),
view.verticalHeader().sectionSize(index.row())
)
# THEN, resize the row and column, which will call sizeHint()
view.resizeColumnToContents(index.column())
view.resizeRowToContents(index.row())
# delay a forced scroll to the index to ensure that the editor is
# visible after the sections have been resized
QTimer.singleShot(1, lambda: view.scrollTo(index))
return editor
def sizeHint(self, opt, index):
if index in self.editorData:
# sizeHint doesn't provide access to the editor
editor, *_ = self.editorData[index]
return editor.sizeHint()
return super().sizeHint(opt, index)
def destroyEditor(self, editor, index):
super().destroyEditor(editor, index)
if index in self.editorData:
editor, view, width, height = self.editorData.pop(index)
view.horizontalHeader().resizeSection(index.column(), width)
view.verticalHeader().resizeSection(index.row(), height)
Note that if the index is near the edge (last row or column), when the editor is destroyed it's possible that the view won't properly adjust its scroll bars if the scroll mode is ScrollPerItem. AFAIK there's no easy workaround for this.
Remember what said above, though. As Ian Malcolm would say, whether this could be done or not, you should stop and think if you should.
Update about focus issues
As pointed out in comments, if the user clicks somewhere else when the editor is active, the editor doesn't close itself as expected.
By default, the delegate's event filter closes the editor whenever it loses focus, but the filter is set on the editor, not its children. When the line edit receives focus, the event filter recognizes that the new focus widget is a child of the editor and will not close it; this is important, because if the editor has child widgets, you don't want it to close just because the internal focus has changed; the side effect of this is that when clicking outside of the line edit, it is the line edit that will receive the FocusOut event, not the editor, so the event filter won't know nothing about it; note that this also happens even when using setFocusProxy(), as Qt automatically sends focus events to the proxy.
There are at least two possible solutions for this, depending on the editor type.
If there is going to be just one main child widget that should accept focus like in this case, the solution is to do what other complex widgets (like QComboBox and QAbstractSpinBox): set the editor as focus proxy of the widget, and post the related events to the widget in the event filter:
def createEditor(self, parent, option, index):
# ...
editor.line_edit.setFocusProxy(editor)
def eventFilter(self, editor, event):
if event.type() in (
event.FocusIn, event.FocusOut,
event.KeyPress, event.KeyRelease,
event.ShortcutOverride, event.InputMethod,
event.ContextMenu
):
editor.line_edit.event(event)
if event.type() != event.FocusOut:
return event.isAccepted()
return super().eventFilter(editor, event)
For more complex situations, we need to ensure that the default implementation of the delegate event filter receives a focus out event whenever the focus is completely lost by the editor or any of its children.
In order to achieve this, a possible solution is to create a custom object that will become an event filter for all child widgets of the editor and eventually posts a FocusOut event for the editor whenever the focused widget is not itself or one of its children (including grandchildren).
class FocusOutFilter(QObject):
def __init__(self, widget):
super().__init__(widget)
self.widget = widget
self.install(widget)
def install(self, widget):
widget.installEventFilter(self)
for child in widget.findChildren(QWidget):
child.installEventFilter(self)
def eventFilter(self, obj, event):
if (event.type() == event.FocusOut and
not self.widget.isAncestorOf(QApplication.focusWidget())):
obj.removeEventFilter(self)
self.deleteLater()
return QApplication.sendEvent(self.widget, event)
elif event.type() == event.ChildAdded:
self.install(event.child())
return super().eventFilter(obj, event)
class CustomDelegate(QStyledItemDelegate):
# ...
def createEditor(self, parent, option, index):
editor = QWidget(parent)
editor.filter = FocusOutFilter(editor)
# ...
There is a catch. The problem is that focusWidget() will return the widget even if the event results in opening a menu. While this is fine for any context menu of the editor widgets (including the edit menu of QLineEdit), it could create some issues when the view implements the context menu event (showing a menu in contextMenuEvent(), within a ContextMenu event or with CustomContextMenu policy). There is no final solution for this, as there is no definite way to check what created the menu: a proper implementation expects that a QMenu is created with the parent that created it, but that cannot be certain (for simplicity, dynamically created menus could be created without any parent, which makes it impossible to know what created them). In these cases, the simplest solution is to implement the above conditions by first checking the return value of isPersistentEditorOpen() and ensure that widgetAt() properly returns the table's viewport().
I found no references about this in the documentation.
You cannot easily style this "extension" button because that symbol is actually an icon.
You can however access the QToolButton widget to set the icon to whatever you like. In PyQt4 you get to it with menubar.children()[0]. This should be consistent with PyQt5. Looking at the Qt5 source code, it appears that the extension icon is always created first, even if it is not shown, and the children() method returns objects in the order in which they were created (this the index of 0).
Once you have a reference to the QToolButton, you can then set the icon to whatever you like with menubar.children()[0].setIcon(my_qicon) (or similar).
Since this is one of the top items on google for modifying the "show more" icon:
Another option is to use a QToolbar. You can do the same thing except that the first child is a layout, second is the QToolButton that you want:
from qtpy import QtWidgets, QtGui
import sys
def call_back():
print('pressed')
app = QtWidgets.QApplication([])
widget = QtWidgets.QWidget()
layout = QtWidgets.QGridLayout(widget)
toolbar = QtWidgets.QToolBar()
layout.addWIdget(toolbar)
# add some actions
for i in range(10):
toolbar.addAction('test_{}'.format(i), call_back)
# change the icon, the first child is a layout!, the second it the toolbtn we want!
toolbar.children()[1].setIcon(QtGui.QIcon('path/to/image.png'))
widget.show()
app.exec_()
sys.exit()
I am having issues re-writing one of the default logo scripts in GIMP(using Script-fu based on scheme). For one thing the alpha layer is not shown in the layer browser after the image is shown. I am re-writing the Create Neon Logo script(neon-logo.scm) and I want it to do the following before it displays the new image:
add an alpha channel
change black(background color) to transparent via colortoalpha
return the generated image as an object to be used in another python script(using for loops to generate 49 images)
I have tried modifying the following code to the default script:
(gimp-image-undo-disable img)
(apply-neon-logo-effect img tube-layer size bg-color glow-color shadow) *Generates neon logo
(set! end-layer (car (gimp-image-flatten img))) *Flattens image
(gimp-layer-add-alpha end-layer) *Adds alpha layer as last layer in img(img=the image)
(plug-in-colortoalpha img 0 (255 255 255)) *Uses color to alpha-NOT WORKING
(gimp-image-undo-enable img) *Enables undo
(gimp-display-new img) *Displays new image
For number 3 my python code is this:
for str1 in list1:
for color1 in list3:
img = pdb.script_fu_neon_logo(str1,50,"Swis721 BdOul BT",(0,0,0),color1,0)
But img is a "Nonetype" object. I would like to make it so that instead of displaying the generated image in a new window, it just returns the generated image for use with my python script.
Can anyone help?
Maybe to keep everything more managaeable and readable, you should translate theoriginal script into Python - that way you willhaveno surprises on otherwiser trivial things as variable assignment, picking elements from sequences and so on.
1 and 2) your calls are apparantly correct to flaten an "add an alpha channel " (not "alpha layer",a s you write, please) to the image - but you are calling color-to-alpha to make White (255 255 255) transparemt not black. Trey changing that to (0 0 0) - if it does not work, make]
each of the calls individually, either on script-fu console or on python console, and check what is wrong.
3) Script-fu can't return values to the caller (as can be seen by not having a "return value type" parameter to the register call. That means that scripts in scheme in GIMP can only render thigns on themselves and not be used to compose more complex chains.
That leaves you with 2 options: port the original script to Python-fu (and them just register it to return a PF-IMAGE) - or hack around the call like this, in Python:
create a set with all images opened, call your script-fu, check which of your images currently open is not on the set of images previously opened - that will be your new image:
The tricky part with this is that: there is no unique identifier to an image when you see it from Python-fu - so you 'dhave to compose a value like (name, number_of_layers, size) to go on those comparison sets and even that might not suffice - or youg could juggle with "parasites" (arbitrary data that can be attached to an image). As you can see, having the original script-fu rewriten in Python, since all the work is done by PDB calls, and these translate 1:1, is preferable.