Linebreak in QTableView - python

I have a QTableView in my GUI in which I want to have some table cells in which I can insert line breaks using something like \n or <br>.
So far I have tried to set an QLabel as the IndexWidget:
l = QLabel(val[2])
self.setRowHeight(i, int(l.height() / 8))
l.setAutoFillBackground(True)
self.setIndexWidget(QAbstractItemModel.createIndex(self.results_model, i, 2), l)
The Problem with this approach is the code is not very clean and cannot just be done in the AbstractTableModel without this code to replace the cell with a widget. The second problem is, that when selecting a row with a widget in it the blue highlighting doesn't apply to the cell. Another problem is that the resizeRowsToContents() method doesn't take the height of this widget in to consideration.
Any Ideas would be very appreciated, thanks!

One way to implement this task is to use an HtmlDelegate, in this case the line break will be given by <br>:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class HTMLDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
opt = QStyleOptionViewItem(option)
self.initStyleOption(opt, index)
painter.save()
doc = QTextDocument()
doc.setHtml(opt.text)
opt.text = "";
style = opt.widget.style() if opt.widget else QApplication.style()
style.drawControl(QStyle.CE_ItemViewItem, opt, painter)
painter.translate(opt.rect.left(), opt.rect.top())
clip = QRectF(0, 0, opt.rect.width(), opt.rect.height())
doc.drawContents(painter, clip)
painter.restore()
def sizeHint(self, option, index ):
opt = QStyleOptionViewItem(option)
self.initStyleOption(opt, index)
doc = QTextDocument()
doc.setHtml(opt.text);
doc.setTextWidth(opt.rect.width())
return QSize(doc.idealWidth(), doc.size().height())
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QTableView()
model = QStandardItemModel(4, 6)
delegate = HTMLDelegate()
w.setItemDelegate(delegate)
w.setModel(model)
w.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
w.show()
sys.exit(app.exec_())

Related

Force QTabBar tabs to stay as small as possible and ignore sizeHint

I'm trying to have a + button added to a QTabBar. There's a great solution from years ago, with a slight issue that it doesn't work with PySide2. The problem is caused by the tabs auto resizing to fill the sizeHint, which in this case isn't wanted as the extra space is needed. Is there a way I can disable this behaviour?
I've tried QTabBar.setExpanding(False), but according to this answer, the property is mostly ignored:
The bad news is that QTabWidget effectively ignores that property, because it always forces its tabs to be the minimum size (even if you set your own tab-bar).
The difference being in PySide2, it forces the tabs to be the preferred size, where I'd like the old behaviour of minimum size.
Edit: Example with minimal code. The sizeHint width stretches the tab across the full width, whereas in older Qt versions it doesn't do that. I can't really override tabSizeHint since I don't know what the original tab size should be.
import sys
from PySide2 import QtCore, QtWidgets
class TabBar(QtWidgets.QTabBar):
def sizeHint(self):
return QtCore.QSize(100000, super().sizeHint().height())
class Test(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
layout = QtWidgets.QVBoxLayout()
self.setLayout(layout)
tabWidget = QtWidgets.QTabWidget()
tabWidget.setTabBar(TabBar())
layout.addWidget(tabWidget)
tabWidget.addTab(QtWidgets.QWidget(), 'this shouldnt be stretched')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
test = Test()
test.show()
sys.exit(app.exec_())
I think there may be an easy solution to your problem (see below). Where the linked partial solution calculated absolute positioning for the '+' button, the real intent with Qt is always to let the layout engine do it's thing rather than trying to tell it specific sizes and positions. QTabWidget is basically a pre-built amalgamation of layouts and widgets, and sometimes you just have to skip the pre-built and build your own.
example of building a custom TabWidget with extra things across the TabBar:
import sys
from PySide2 import QtWidgets
from random import randint
class TabWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
#layout for entire widget
vbox = QtWidgets.QVBoxLayout(self)
#top bar:
hbox = QtWidgets.QHBoxLayout()
vbox.addLayout(hbox)
self.tab_bar = QtWidgets.QTabBar()
self.tab_bar.setMovable(True)
hbox.addWidget(self.tab_bar)
spacer = QtWidgets.QSpacerItem(0,0,QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
hbox.addSpacerItem(spacer)
add_tab = QtWidgets.QPushButton('+')
hbox.addWidget(add_tab)
#tab content area:
self.widget_stack = QtWidgets.QStackedLayout()
vbox.addLayout(self.widget_stack)
self.widgets = {}
#connect events
add_tab.clicked.connect(self.add_tab)
self.tab_bar.currentChanged.connect(self.currentChanged)
def add_tab(self):
tab_text = 'tab' + str(randint(0,100))
tab_index = self.tab_bar.addTab(tab_text)
widget = QtWidgets.QLabel(tab_text)
self.tab_bar.setTabData(tab_index, widget)
self.widget_stack.addWidget(widget)
self.tab_bar.setCurrentIndex(tab_index)
def currentChanged(self, i):
if i >= 0:
self.widget_stack.setCurrentWidget(self.tab_bar.tabData(i))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
test = TabWidget()
test.show()
sys.exit(app.exec_())
All that said, I think the pre-built QTabWidget.setCornerWidget may be exactly what you're looking for (set a QPushButton to the upper-right widget). The example I wrote should much easier to customize, but also much more effort to re-implement all the same functionality. You will have to re-implement some of the signal logic to create / delete / select / rearrange tabs on your own. I only demonstrated simple implementation, which probably isn't bulletproof to all situations.
Using the code from Aaron as a base to start on, I managed to implement all the functionality required to work with my existing script:
from PySide2 import QtCore, QtWidgets
class TabBar(QtWidgets.QTabBar):
def minimumSizeHint(self):
"""Allow the tab bar to shrink as much as needed."""
minimumSizeHint = super(TabBar, self).minimumSizeHint()
return QtCore.QSize(0, minimumSizeHint.height())
class TabWidgetPlus(QtWidgets.QWidget):
tabOpenRequested = QtCore.Signal()
tabCountChanged = QtCore.Signal(int)
def __init__(self, parent=None):
self._addingTab = False
super(TabWidgetPlus, self).__init__(parent=parent)
# Main layout
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
# Bar layout
self._tabBarLayout = QtWidgets.QHBoxLayout()
self._tabBarLayout.setContentsMargins(0, 0, 0, 0)
self._tabBarLayout.setSpacing(0)
layout.addLayout(self._tabBarLayout)
self._tabBar = TabBar()
self._tabBarLayout.addWidget(self._tabBar)
for method in (
'isMovable', 'setMovable',
'tabsClosable', 'setTabsClosable',
'tabIcon', 'setTabIcon',
'tabText', 'setTabText',
'currentIndex', 'setCurrentIndex',
'currentChanged', 'tabCloseRequested',
):
setattr(self, method, getattr(self._tabBar, method))
self._plusButton = QtWidgets.QPushButton('+')
self._tabBarLayout.addWidget(self._plusButton) # TODO: Find location to insert
self._plusButton.setFixedWidth(20)
self._tabBarLayout.addStretch()
# Content area
self._contentArea = QtWidgets.QStackedLayout()
layout.addLayout(self._contentArea)
# Signals
self.currentChanged.connect(self._currentChanged)
self._plusButton.clicked.connect(self.tabOpenRequested.emit)
# Final setup
self.installEventFilter(self)
#QtCore.Slot(int)
def _currentChanged(self, i):
"""Update the widget."""
if i >= 0 and not self._addingTab:
self._contentArea.setCurrentWidget(self.tabBar().tabData(i))
def eventFilter(self, obj, event):
"""Intercept events until the correct height is set."""
if event.type() == QtCore.QEvent.Show:
self.plusButton().setFixedHeight(self._tabBar.geometry().height())
self.removeEventFilter(self)
return False
def tabBarLayout(self):
return self._tabBarLayout
def tabBar(self):
return self._tabBar
def plusButton(self):
return self._plusButton
def tabAt(self, point):
"""Get the tab at a given point.
This takes any layout margins into account.
"""
offset = self.layout().contentsMargins().top() + self.tabBarLayout().contentsMargins().top()
return self.tabBar().tabAt(point - QtCore.QPoint(0, offset))
def addTab(self, widget, name=''):
"""Add a new tab.
Returns:
Tab index as an int.
"""
self._addingTab = True
tabBar = self.tabBar()
try:
index = tabBar.addTab(name)
tabBar.setTabData(index, widget)
self._contentArea.addWidget(widget)
finally:
self._addingTab = False
return index
def insertTab(self, index, widget, name=''):
"""Inserts a new tab.
If index is out of range, a new tab is appended.
Returns:
Tab index as an int.
"""
self._addingTab = True
tabBar = self.tabBar()
try:
index = tabBar.insertTab(index, name)
tabBar.setTabData(index, widget)
self._contentArea.insertWidget(index, widget)
finally:
self._addingTab = False
return index
def removeTab(self, index):
"""Remove a tab."""
tabBar = self.tabBar()
self._contentArea.removeWidget(tabBar.tabData(index))
tabBar.removeTab(index)
if __name__ == '__main__':
import sys
import random
app = QtWidgets.QApplication(sys.argv)
test = TabWidgetPlus()
test.addTab(QtWidgets.QPushButton(), 'yeah')
test.insertTab(0, QtWidgets.QCheckBox(), 'what')
test.insertTab(1, QtWidgets.QRadioButton(), 'no')
test.removeTab(1)
test.setMovable(True)
test.setTabsClosable(True)
def tabTest():
name = 'Tab ' + str(random.randint(0, 100))
index = test.addTab(QtWidgets.QLabel(name), name)
test.setCurrentIndex(index)
test.tabOpenRequested.connect(tabTest)
test.tabCloseRequested.connect(lambda index: test.removeTab(index))
test.show()
sys.exit(app.exec_())
The one difference is if you're using tabWidget.tabBar().tabAt(point), this is no longer guaranteed to be correct as it doesn't take any margins into account. I set the margins to 0 so this shouldn't be an issue, but I also included those corrections in TabWidgetPlus.tabAt.
I only copied a few methods from QTabBar to QTabWidget as some may need extra testing.

How to manage visibility of data in PyQt5 QTableWidgetItem

I want to make some data hidden in QTableWidget until a specific cell is clicked. Right now I only manage to display "*" instead of actual value. I think I could connect somehow with action when the specific cell is clicked and replaced the value of the clicked cell. I also know that there is a function setData() that can be invoked on QTableWidgetItem and give me wanted behavior. But I cannot find any useful example for Python implementation of qt. What is the best solution to this problem?
def setTableValues(self):
self.table.setRowCount(len(self.tableList))
x = 0
for acc in self.tableList:
y = 0
for val in acc.getValuesAsList():
if isinstance(val,Cipher):
val = "*"*len(val.getDecrypted())
item = QTableWidgetItem(val)
self.table.setItem(x,y,item)
y += 1
x += 1
self.table.resizeRowsToContents()
self.table.resizeColumnsToContents()
You can associate a flag with a role that indicates the visibility of the text and then use a delegate to hide the text. That flag will change when the items are pressed:
from PyQt5 import QtCore, QtGui, QtWidgets
VisibilityRole = QtCore.Qt.UserRole + 1000
class VisibilityDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
if not index.data(VisibilityRole):
option.text = "*" * len(option.text)
class TableWidget(QtWidgets.QTableWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
delegate = VisibilityDelegate(self)
self.setItemDelegate(delegate)
self.visibility_index = QtCore.QModelIndex()
self.pressed.connect(self.on_pressed)
#QtCore.pyqtSlot(QtCore.QModelIndex)
def on_pressed(self, index):
if self.visibility_index.isValid():
self.model().setData(self.visibility_index, False, VisibilityRole)
self.visibility_index = index
self.model().setData(self.visibility_index, True, VisibilityRole)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = TableWidget(10, 4)
for i in range(w.rowCount()):
for j in range(w.columnCount()):
it = QtWidgets.QTableWidgetItem("{}-{}".format(i, j))
w.setItem(i, j, it)
w.show()
w.resize(640, 480)
sys.exit(app.exec_())

align pixmap in column's center with QTreeView and QAbstractItemModel

How do I make a pixmap's alignment centered to its column in a QTreeView? I have 2 columns with icons that are aligned to left, but I want one of them centered, so this needs to work on a single column and not force the whole table to one alignment.
I'm using a QTreeView with QAbstractItemModel as its model. On one column I flagged it as QtCore.Qt.DecorationRole and return a pixmap in the model's data() method so that it displays images along that column.
All works well, except that the images all align left, and for the life of me I can't get them centered horizontally.
In the data() method, I tried returning QtCore.Qt.AlignCenter if the role was QtCore.Qt.TextAlignmentRole, but that seems to only effect text (duh!).
Is there another way to achieve this? I'm not interested in taking the route of delegates if possible.
A possible solution is to overwrite the delegate's initStyleOption() method:
from PySide2 import QtCore, QtGui, QtWidgets
class IconCenterDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(IconCenterDelegate, self).initStyleOption(option, index)
option.decorationAlignment = (
QtCore.Qt.AlignHCenter | QtCore.Qt.AlignCenter
)
option.decorationPosition = QtWidgets.QStyleOptionViewItem.Top
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QTreeView()
model = QtGui.QStandardItemModel(w)
w.setModel(model)
delegate = IconCenterDelegate(w)
w.setItemDelegateForColumn(1, delegate)
icons = [
"SP_TitleBarMinButton",
"SP_TitleBarMenuButton",
"SP_TitleBarMaxButton",
"SP_TitleBarCloseButton",
"SP_TitleBarNormalButton",
"SP_TitleBarShadeButton",
"SP_TitleBarUnshadeButton",
"SP_TitleBarContextHelpButton",
"SP_MessageBoxInformation",
"SP_MessageBoxWarning",
"SP_MessageBoxCritical",
"SP_MessageBoxQuestion",
"SP_DesktopIcon",
]
parent = model.invisibleRootItem()
for icon_name in icons:
icon = QtWidgets.QApplication.style().standardIcon(
getattr(QtWidgets.QStyle, icon_name)
)
its = []
for _ in range(3):
it = QtGui.QStandardItem()
it.setIcon(icon)
its.append(it)
parent.appendRow(its)
model.appendRow(it)
w.resize(640, 480)
w.expandAll()
w.show()
sys.exit(app.exec_())
If you want the icons of all the columns to be aligned centrally then you could overwrite the viewOptions() method of the view:
class TreeView(QtWidgets.QTreeView):
def viewOptions(self):
option = super().viewOptions()
option.decorationAlignment = (
QtCore.Qt.AlignHCenter | QtCore.Qt.AlignCenter
)
option.decorationPosition = QtWidgets.QStyleOptionViewItem.Top
return option

How can I make lines of a Qt table bold with PyQt?

In the PyQt creator I made a 9*9 Table looking like this:
In that table I want to make every third line separating the rows and every third line separating columns and the the border lines bold. Is that possible to do in PyQt?
If yes how?
Use a delegate for that:
class Delegate(QStyledItemDelegate):
def paint(self, painter, option, index):
super().paint(painter, option, index)
if ((1+index.row()) % 3 == 0): # Every third row
painter.setPen(QPen(Qt.red, 3))
painter.drawLine(option.rect.bottomLeft(), option.rect.bottomRight())
if ((1+index.column()) % 3 == 0): # Every third column
painter.setPen(QPen(Qt.red, 3))
painter.drawLine(option.rect.topRight(), option.rect.bottomRight())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
v = QTableView()
model = QStandardItemModel(9, 9)
v.setModel(model)
v.show()
v.setItemDelegate(Delegate(v))
sys.exit(app.exec_())

How can i have a transparent background in a pyside QWidget, but keep its children opaque

I have an widget that contain a lot of children , i want to set its background transparent , but keep its children opaque.
I'am using PySide 1.2.1 win7
I have try something like this:
self.setAttribute(Qt.WA_TranslucentBackground,True);
self.setStyleSheet("background-color:rgba(40,40,150,150);border:0px;")
but it dosen't work for me . It make the whole widget transparent . I want to keep the children opaque. Can someone help me ? Thank you so much. Here is my code , thanks for help.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class M_SCROLL_GRID_WIDGET(QWidget):
# This is a common ui class .
def __init__(self,column_count):
super(M_SCROLL_GRID_WIDGET, self).__init__()
self.column_count = column_count
self.visible_widget_list = []
self.scroll_widget = QWidget()
self.scroll_area = QScrollArea()
self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scroll_area.setWidgetResizable(True)
self.scroll_area.setWidget(self.scroll_widget)
self.main_layout = QGridLayout()
self.main_layout.setSpacing(0)
self.scroll_widget.setLayout(self.main_layout)
self.main_layout.setAlignment(Qt.AlignTop)
master_layout = QVBoxLayout(self)
master_layout.addWidget(self.scroll_area)
self.setLayout(master_layout)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setStyleSheet("background-color:rgba(40,40,150,150);border:0px;")
def Get_Widget_Count(self):
return len(self.visible_widget_list)
def Add_Widget_To_Vis_List(self, widget):
self.visible_widget_list.append(widget)
def Remove_Widget_To_Vis_List(self, widget):
self.visible_widget_list.remove(widget)
def Remove_All_Widgets(self):
for i in reversed(range(self.main_layout.count())):
widget = self.main_layout.itemAt(i).widget()
widget.setParent(None)
def Clean_Visible_List(self):
self.visible_widget_list = []
def Refresh(self):
for widget_index in xrange(len(self.visible_widget_list)):
r_position = widget_index / self.column_count
c_position = (widget_index + 1) % self.column_count
if c_position == 0:
c_position = self.column_count
self.main_layout.addWidget(self.visible_widget_list[widget_index], r_position, c_position)
if __name__ == '__main__':
app = QApplication(sys.argv)
caofan = M_SCROLL_GRID_WIDGET(3)
btn_list = []
caofan.Remove_All_Widgets()
for a in xrange(50):
btn = QPushButton(str(a+1))
btn_list.append(btn)
caofan.Add_Widget_To_Vis_List(btn)
caofan.Refresh()
caofan.show()
app.exec_()
I suspect the real problem is that the style sheet you specify is propagating to the child widgets.
So, remove...
self.setStyleSheet("background-color:rgba(40,40,150,150);border:0px;")
and override QWidget::paintEvent with something like...
def paintEvent(self, event):
painter = QPainter(self)
painter.fillRect(self.rect(), QColor(40, 40, 150, 150))

Categories

Resources