I am developing an app for memorizing text using PyQt4. I want to show all the words in bubbles so that you see how long the word is. But when I have all the bubbles in my QScrollArea, they are aligned one under the other. I would like to have them aligned side-by-side, but with word-wrap.
I got the bubbles to work using a QLabel with rounded borders. But now that I have the words in QLabel's, PyQt doesn't consider them as words - but as widgets. So PyQt puts one widget under the other. I would like the widgets to be aligned side-by-side until they reach the end of the line, and then they should wrap around to the next line - meaning the QLabel's should act like words in a text document.
Here is my code so far:
f = open(r'myFile.txt')
class Bubble(QtGui.QLabel):
def __init__(self, text):
super(Bubble, self).__init__(text)
self.word = text
self.setContentsMargins(5, 5, 5, 5)
def paintEvent(self, e):
p = QtGui.QPainter(self)
p.setRenderHint(QtGui.QPainter.Antialiasing,True)
p.drawRoundedRect(0,0,self.width()-1,self.height()-1,5,5)
super(Bubble, self).paintEvent(e)
class MainWindow(QtGui.QMainWindow):
def __init__(self, text, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.MainArea = QtGui.QScrollArea
self.widget = QtGui.QWidget()
vbox = QtGui.QVBoxLayout()
self.words = []
for t in re.findall(r'\b\w+\b', text):
label = Bubble(t)
label.setFont(QtGui.QFont('SblHebrew', 18))
label.setFixedWidth(label.sizeHint().width())
self.words.append(label)
vbox.addWidget(label)
self.widget.setLayout(vbox)
self.MainArea.setWidget(self.widget)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
myWindow = MainWindow(f.read(), None)
myWindow.show()
sys.exit(app.exec_())
When I run this I get:
But I would like the words (the Qlabel's containing the words) to be next to each other, not under each other, like this (photoshopped):
I've been doing a lot of research, but no answers help me align the widgets next to each other.
Here's a PyQt5 version of the Flow Layout demo script:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class FlowLayout(QtWidgets.QLayout):
def __init__(self, parent=None, margin=-1, hspacing=-1, vspacing=-1):
super(FlowLayout, self).__init__(parent)
self._hspacing = hspacing
self._vspacing = vspacing
self._items = []
self.setContentsMargins(margin, margin, margin, margin)
def __del__(self):
del self._items[:]
def addItem(self, item):
self._items.append(item)
def horizontalSpacing(self):
if self._hspacing >= 0:
return self._hspacing
else:
return self.smartSpacing(
QtWidgets.QStyle.PM_LayoutHorizontalSpacing)
def verticalSpacing(self):
if self._vspacing >= 0:
return self._vspacing
else:
return self.smartSpacing(
QtWidgets.QStyle.PM_LayoutVerticalSpacing)
def count(self):
return len(self._items)
def itemAt(self, index):
if 0 <= index < len(self._items):
return self._items[index]
def takeAt(self, index):
if 0 <= index < len(self._items):
return self._items.pop(index)
def expandingDirections(self):
return QtCore.Qt.Orientations(0)
def hasHeightForWidth(self):
return True
def heightForWidth(self, width):
return self.doLayout(QtCore.QRect(0, 0, width, 0), True)
def setGeometry(self, rect):
super(FlowLayout, self).setGeometry(rect)
self.doLayout(rect, False)
def sizeHint(self):
return self.minimumSize()
def minimumSize(self):
size = QtCore.QSize()
for item in self._items:
size = size.expandedTo(item.minimumSize())
left, top, right, bottom = self.getContentsMargins()
size += QtCore.QSize(left + right, top + bottom)
return size
def doLayout(self, rect, testonly):
left, top, right, bottom = self.getContentsMargins()
effective = rect.adjusted(+left, +top, -right, -bottom)
x = effective.x()
y = effective.y()
lineheight = 0
for item in self._items:
widget = item.widget()
hspace = self.horizontalSpacing()
if hspace == -1:
hspace = widget.style().layoutSpacing(
QtWidgets.QSizePolicy.PushButton,
QtWidgets.QSizePolicy.PushButton, QtCore.Qt.Horizontal)
vspace = self.verticalSpacing()
if vspace == -1:
vspace = widget.style().layoutSpacing(
QtWidgets.QSizePolicy.PushButton,
QtWidgets.QSizePolicy.PushButton, QtCore.Qt.Vertical)
nextX = x + item.sizeHint().width() + hspace
if nextX - hspace > effective.right() and lineheight > 0:
x = effective.x()
y = y + lineheight + vspace
nextX = x + item.sizeHint().width() + hspace
lineheight = 0
if not testonly:
item.setGeometry(
QtCore.QRect(QtCore.QPoint(x, y), item.sizeHint()))
x = nextX
lineheight = max(lineheight, item.sizeHint().height())
return y + lineheight - rect.y() + bottom
def smartSpacing(self, pm):
parent = self.parent()
if parent is None:
return -1
elif parent.isWidgetType():
return parent.style().pixelMetric(pm, None, parent)
else:
return parent.spacing()
class Bubble(QtWidgets.QLabel):
def __init__(self, text):
super(Bubble, self).__init__(text)
self.word = text
self.setContentsMargins(5, 5, 5, 5)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.drawRoundedRect(
0, 0, self.width() - 1, self.height() - 1, 5, 5)
super(Bubble, self).paintEvent(event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, text, parent=None):
super(MainWindow, self).__init__(parent)
self.mainArea = QtWidgets.QScrollArea(self)
self.mainArea.setWidgetResizable(True)
widget = QtWidgets.QWidget(self.mainArea)
widget.setMinimumWidth(50)
layout = FlowLayout(widget)
self.words = []
for word in text.split():
label = Bubble(word)
label.setFont(QtGui.QFont('SblHebrew', 18))
label.setFixedWidth(label.sizeHint().width())
self.words.append(label)
layout.addWidget(label)
self.mainArea.setWidget(widget)
self.setCentralWidget(self.mainArea)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow('Harry Potter is a series of fantasy literature')
window.show()
sys.exit(app.exec_())
I thought it might be possible to use html in a QTextBrowser widget for this, but Qt's rich-text engine doesn't support the border-radius CSS property which would be needed for the bubble labels.
So it looks like you need a PyQt port of the Flow Layout example. This can "word-wrap" a collection of widgets inside a container, and also allows the margins and horizontal/vertical spacing to be adjusted.
Here is a demo script that implements the FlowLayout class and shows how to use it with your example:
import sys
from PyQt4 import QtCore, QtGui
class FlowLayout(QtGui.QLayout):
def __init__(self, parent=None, margin=-1, hspacing=-1, vspacing=-1):
super(FlowLayout, self).__init__(parent)
self._hspacing = hspacing
self._vspacing = vspacing
self._items = []
self.setContentsMargins(margin, margin, margin, margin)
def __del__(self):
del self._items[:]
def addItem(self, item):
self._items.append(item)
def horizontalSpacing(self):
if self._hspacing >= 0:
return self._hspacing
else:
return self.smartSpacing(
QtGui.QStyle.PM_LayoutHorizontalSpacing)
def verticalSpacing(self):
if self._vspacing >= 0:
return self._vspacing
else:
return self.smartSpacing(
QtGui.QStyle.PM_LayoutVerticalSpacing)
def count(self):
return len(self._items)
def itemAt(self, index):
if 0 <= index < len(self._items):
return self._items[index]
def takeAt(self, index):
if 0 <= index < len(self._items):
return self._items.pop(index)
def expandingDirections(self):
return QtCore.Qt.Orientations(0)
def hasHeightForWidth(self):
return True
def heightForWidth(self, width):
return self.doLayout(QtCore.QRect(0, 0, width, 0), True)
def setGeometry(self, rect):
super(FlowLayout, self).setGeometry(rect)
self.doLayout(rect, False)
def sizeHint(self):
return self.minimumSize()
def minimumSize(self):
size = QtCore.QSize()
for item in self._items:
size = size.expandedTo(item.minimumSize())
left, top, right, bottom = self.getContentsMargins()
size += QtCore.QSize(left + right, top + bottom)
return size
def doLayout(self, rect, testonly):
left, top, right, bottom = self.getContentsMargins()
effective = rect.adjusted(+left, +top, -right, -bottom)
x = effective.x()
y = effective.y()
lineheight = 0
for item in self._items:
widget = item.widget()
hspace = self.horizontalSpacing()
if hspace == -1:
hspace = widget.style().layoutSpacing(
QtGui.QSizePolicy.PushButton,
QtGui.QSizePolicy.PushButton, QtCore.Qt.Horizontal)
vspace = self.verticalSpacing()
if vspace == -1:
vspace = widget.style().layoutSpacing(
QtGui.QSizePolicy.PushButton,
QtGui.QSizePolicy.PushButton, QtCore.Qt.Vertical)
nextX = x + item.sizeHint().width() + hspace
if nextX - hspace > effective.right() and lineheight > 0:
x = effective.x()
y = y + lineheight + vspace
nextX = x + item.sizeHint().width() + hspace
lineheight = 0
if not testonly:
item.setGeometry(
QtCore.QRect(QtCore.QPoint(x, y), item.sizeHint()))
x = nextX
lineheight = max(lineheight, item.sizeHint().height())
return y + lineheight - rect.y() + bottom
def smartSpacing(self, pm):
parent = self.parent()
if parent is None:
return -1
elif parent.isWidgetType():
return parent.style().pixelMetric(pm, None, parent)
else:
return parent.spacing()
class Bubble(QtGui.QLabel):
def __init__(self, text):
super(Bubble, self).__init__(text)
self.word = text
self.setContentsMargins(5, 5, 5, 5)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.drawRoundedRect(
0, 0, self.width() - 1, self.height() - 1, 5, 5)
super(Bubble, self).paintEvent(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self, text, parent=None):
super(MainWindow, self).__init__(parent)
self.mainArea = QtGui.QScrollArea(self)
self.mainArea.setWidgetResizable(True)
widget = QtGui.QWidget(self.mainArea)
widget.setMinimumWidth(50)
layout = FlowLayout(widget)
self.words = []
for word in text.split():
label = Bubble(word)
label.setFont(QtGui.QFont('SblHebrew', 18))
label.setFixedWidth(label.sizeHint().width())
self.words.append(label)
layout.addWidget(label)
self.mainArea.setWidget(widget)
self.setCentralWidget(self.mainArea)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow('Harry Potter is a series of fantasy literature')
window.show()
sys.exit(app.exec_())
Related
The basic idea is that I can't use a QGraphicsView class to draw while inside a QMainWindow. I can see the painEvent firing and that the information is flowing to the method that performs the drawing, but in the end nothing gets displayed. Here is the Class with the QGraphicsView:
class Display_Pixels(QGraphicsView):
def __init__(self, parent=None):
QGraphicsView.__init__(self, parent=parent)
#super().__init__()
self.initUI()
self.img = cv2.imread('roi.jpg')
def initUI(self):
self.setGeometry(100, 100, 650, 650)
#self.setWindowTitle('By Pixel')
#self.setMouseTracking(True)
#self.show()
res = 40
self.grid = np.array([ [-1] * res for n in range(res)]) # list comprehension
#print(self.grid.shape)
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawRectangles(qp)
qp.end()
def drawRectangles(self, qp, w = 16):
print("Drawing")
mode = 0
x,y = 0,0 # starting position
lr = 20
hr = 35
col = QColor(0, 0, 0)
col.setNamedColor('#d4d4d4')
qp.setPen(col)
#print(self.img.shape)
for g_row, img_row in zip(self.grid, self.img):
#print(img_row.shape)
for g_col, img_col in zip(g_row, img_row):
r, g, b = (img_col[0], img_col[1], img_col[2])
#print(r,g,b)
if g_col == 1:
if mode == 0:
r = int(math.log(r)*lr)
g = int(math.log(g)*hr)
b = int(math.log(b)*lr)
elif mode == 1:
if r+50 <= 220: r = r+50
if g+80 <= 255: g = g+80
if b+50 <= 220: b = b+50
else:
if r+70 <= 220: r = r+70
if g+140 <= 255: g = g+140
if b+70 <= 220: b = b+70
qp.setBrush(QColor(r, g, b))
qp.drawRect(x, y, w, w)
else:
qp.setBrush(QColor(r, g, b))
qp.drawRect(x, y, w, w)
#qp.setBrush(QColor(200, 0, 0))
#qp.drawRect(x, y, w, w)
x = x + w # move right
y = y + w # move down
x = 0 # rest to left edge
def mousePressEvent(self, QMouseEvent):
w = 16.0
#print("MOUSE:")
#print('(', int(QMouseEvent.x()/w), ', ', int(QMouseEvent.y()/w), ')')
#print (QMouseEvent.pos())
x = float(QMouseEvent.x())
y = float(QMouseEvent.y())
self.grid[int(y/w)][int(x/w)] = -1 * self.grid[int(y/w)][int(x/w)]
#print(img[int(y/w), int(x/w), :])
self.repaint()
#self.update()
And also the code for the main window:
class Window(QMainWindow):
def __init__(self, parent=None):
#This initializes the main window or form
super(Window,self).__init__(parent=parent)
self.setGeometry(1,31,900,900)
self.setWindowTitle("Pre-Alignment system")
def run():
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
GUI = Window()
view = Display_Pixels(GUI)
#view = MyView(GUI)
GUI.show()
sys.exit(app.exec_())
run()
QGraphicsView inherits from QAbstractScrollArea so the QPainter must set in the viewport(), that is:
def paintEvent(self, e):
qp = QPainter()
qp.begin(self.viewport())
self.drawRectangles(qp)
qp.end()
Although I would paint it is not the best since QGraphicsView has a paint layer that uses the items. In this case it is best to implement a custom item, also I have improved your algorithm.:
import sys
import numpy as np
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
class OpenCVItem(QtWidgets.QGraphicsItem):
def __init__(self, img, parent=None):
super(OpenCVItem, self).__init__(parent)
res = 40
self.grid = -np.ones((res, res))
self._img = img
height, width, channel = self._img.shape
bytesPerLine = 3 * width
self._qimage = QtGui.QImage(self._img.data,
width, height,
bytesPerLine,
QtGui.QImage.Format_RGB888).rgbSwapped()
def boundingRect(self):
w, h, _ = self._img.shape
return QtCore.QRectF(0, 0, w, h)
def paint(self, painter, option, widget):
painter.drawImage(0, 0, self._qimage)
self.drawRectangles(painter)
def drawRectangles(self, painter):
mode = 0
lr = 20
hr = 35
painter.save()
painter.setPen(QtGui.QPen(QtGui.QColor("#d4d4d4")))
w1, h1 = self.grid.shape
fw = self.boundingRect().width()/w1
fh = self.boundingRect().height()/h1
s = QtCore.QSizeF(fw, fh)
for idx, v in np.ndenumerate(self.grid):
if v == 1:
r_ = QtCore.QRectF(fw*QtCore.QPointF(*idx), s)
r_int = r_.toRect()
(r, g, b), _ = cv2.meanStdDev(self._img[r_int.left():r_int.right(),
r_int.top():r_int.bottom()])
if mode == 0:
r = np.log(r+1)*lr
g = np.log(g+1)*hr
b = np.log(b+1)*lr
elif mode == 1:
if r+50 <= 220: r = r+50
if g+80 <= 255: g = g+80
if b+50 <= 220: b = b+50
else:
if r+70 <= 220: r = r+70
if g+140 <= 255: g = g+140
if b+70 <= 220: b = b+70
painter.setBrush(QtGui.QColor(*(int(x) for x in (r, g, b))))
painter.drawRect(r_)
painter.restore()
def mousePressEvent(self, event):
w1, h1 = self.grid.shape
fw = self.boundingRect().width()/w1
fh = self.boundingRect().height()/h1
xi = int(event.pos().x()/fw)
yi = int(event.pos().y()/fh)
self.grid[xi][yi] = -self.grid[xi][yi]
self.update()
super(OpenCVItem, self).mousePressEvent(event)
class Display_Pixels(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(Display_Pixels, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
self.setScene(scene)
item = OpenCVItem(cv2.imread("roi.jpg"))
scene.addItem(item)
class Window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Window,self).__init__(parent=parent)
self.setGeometry(1,31,900,900)
self.setWindowTitle("Pre-Alignment system")
def run():
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
view = Display_Pixels(GUI)
GUI.setCentralWidget(view)
GUI.show()
sys.exit(app.exec_())
run()
I have QTablewidget and I want to use a mousemoveevent on a particular cell B. When mouse moves over this cell B, a message would appear or be printed. I have created the constructor, but it really does not work. Every thing is allright expect from those lines of code.
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
Keep in mind that Qtooltip is assigned when the cell is clicked and works. But I want to do this by MouseMoveevent. Maybe my constructor of MouseEvent code is not right.
Expecting to behave.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
def copy_widget(w):
if isinstance(w, QtWidgets.QWidget):
new_w = type(w)()
if isinstance(w, QtWidgets.QComboBox):
vals = [w.itemText(ix) for ix in range(w.count())]
new_w.addItems(vals)
return new_w
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(1, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
combox_lay = QtWidgets.QComboBox(self)
combox_lay.addItems(["I","II"])
self.setCellWidget(0, 4, combox_lay)
self.cellChanged.connect(self._cellclicked)
#QtCore.pyqtSlot(int, int)
def _cellclicked(self, r, c):
it = self.item(r, c)
it.setTextAlignment(QtCore.Qt.AlignCenter)
n_it = self.item(r,1)
n_it.setToolTip('Test')
#QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
#QtCore.pyqtSlot()
def _removerow(self):
if self.rowCount() > 0:
self.removeRow(self.rowCount()-1)
#QtCore.pyqtSlot()
def _copyrow(self):
r = self.currentRow()
if 0 <= r < self.rowCount():
cells = {"items": [], "widgets": []}
for i in range(self.columnCount()):
it = self.item(r, i)
if it:
cells["items"].append((i, it.clone()))
w = self.cellWidget(r, i)
if w:
cells["widgets"].append((i, copy_widget(w)))
self.copy(cells, r+1)
def copy(self, cells, r):
self.insertRow(r)
for i, it in cells["items"]:
self.setItem(r, i, it)
for i, w in cells["widgets"]:
self.setCellWidget(r, i, w)
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
def onHovered(self):
print("Works")
class ThirdTabLoads(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ThirdTabLoads, self).__init__(parent)
table = LoadTable()
add_button = QtWidgets.QPushButton("Add")
add_button.clicked.connect(table._addrow)
delete_button = QtWidgets.QPushButton("Delete")
delete_button.clicked.connect(table._removerow)
copy_button = QtWidgets.QPushButton("Copy")
copy_button.clicked.connect(table._copyrow)
button_layout = QtWidgets.QVBoxLayout()
button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)
button_layout.addWidget(copy_button, alignment=QtCore.Qt.AlignTop)
tablehbox = QtWidgets.QHBoxLayout()
tablehbox.setContentsMargins(10, 10, 10, 10)
tablehbox.addWidget(table)
grid = QtWidgets.QGridLayout(self)
grid.addLayout(button_layout, 0, 1)
grid.addLayout(tablehbox, 0, 0)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = ThirdTabLoads()
w.show()
sys.exit(app.exec_())
The itemEntered signal must be used, but to do this, the mouseTracking must be enabled in addition to the item. When a row is added it does not imply that the items for each box exist so I have modified it to create it.
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(0, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
self.setMouseTracking(True)
self.itemEntered.connect(self.on_itemEntered)
self._addrow()
def on_itemEntered(self, it):
QtWidgets.QToolTip.hideText()
if it.column() == 1:
r = self.visualItemRect(it)
p = self.viewport().mapToGlobal(QtCore.QPoint(r.center().x(), r.top()))
QtWidgets.QToolTip.showText(p, "Insert")
#QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
for c in range(self.columnCount()):
self.setItem(rowcount, c, QtWidgets.QTableWidgetItem())
# ...
I've been playing around with customising PyQt widgets using paint events. I've been trying to customise the QSlider widget and have had some success, mostly with CSS styling. However, I'm having difficulty making it curved with a QPainterPath as it always seems flat. Is this something that is beyond the capability of this widget (which would surprise me)? The below is my most recent attempt of many with no success. I tried path points instead of cubicTo() with the same. Can anyone help or point me in the right direction?
from PyQt5 import QtGui, QtWidgets, QtCore
class slider(QtWidgets.QSlider):
def __init__(self, parent=None):
super(slider, self).__init__(parent)
self.parent = parent
self.setMinimum(10)
self.setMaximum(30)
self.setValue(20)
self.setTickPosition(QtWidgets.QSlider.TicksBelow)
self.setTickInterval(5)
def paintEvent(self, event):
qp = QtGui.QPainter()
qp.begin(self)
qp.setRenderHint(QtGui.QPainter.Antialiasing)
self.drawCurve(qp)
qp.end()
def drawCurve(self, qp):
path = QtGui.QPainterPath()
path.moveTo(30, 30)
path.cubicTo(30, 30, 200, 350, 200, 30)
qp.drawPath(path)
To have the sensation of depth it is only to choose the correct colors, for this QPainterPathStroker is also used. On the other hand I added the functionality that the QPainterPath is scaled:
from PyQt5 import QtCore, QtGui, QtWidgets
class PathSlider(QtWidgets.QAbstractSlider):
def __init__(self, path=QtGui.QPainterPath(), *args, **kwargs):
super(PathSlider, self).__init__(*args, **kwargs)
self._path = path
self.stroke_path = self._path
self.scale_path = self._path
def setPath(self, path):
path.translate(-path.boundingRect().topLeft())
self._path = path
self.update()
def path(self):
return self._path
path = QtCore.pyqtProperty(QtGui.QPainterPath, fget=path, fset=setPath)
def paintEvent(self, event):
border = 10
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
sx, sy = (self.rect().width() -2*border)/self.path.boundingRect().width(), \
(self.rect().height() -2*border)/self.path.boundingRect().height()
tr = QtGui.QTransform()
tr.translate(border, border)
tr.scale(sx, sy)
self.scale_path = tr.map(self.path)
stroker = QtGui.QPainterPathStroker()
stroker.setCapStyle(QtCore.Qt.RoundCap)
stroker.setWidth(4)
stroke_path = stroker.createStroke(self.scale_path).simplified()
painter.setPen(QtGui.QPen(self.palette().color(QtGui.QPalette.Shadow), 1))
painter.setBrush(QtGui.QBrush(self.palette().color(QtGui.QPalette.Midlight)))
painter.drawPath(stroke_path)
stroker.setWidth(20)
self.stroke_path = stroker.createStroke(self.scale_path).simplified()
percentage = (self.value() - self.minimum())/(self.maximum() - self.minimum())
highlight_path = QtGui.QPainterPath()
highlight_path.moveTo(self.scale_path.pointAtPercent(0))
n_p = int((self.maximum() + 1 - self.minimum())/self.singleStep())
for i in range(n_p+1):
d = i*percentage/n_p
p = self.scale_path.pointAtPercent(d)
highlight_path.lineTo(p)
stroker.setWidth(3)
new_phighlight_path = stroker.createStroke(highlight_path).simplified()
activeHighlight = self.palette().color(QtGui.QPalette.Highlight)
painter.setPen(activeHighlight)
painter.setBrush(QtGui.QBrush(QtGui.QColor(activeHighlight)))
painter.drawPath(new_phighlight_path)
opt = QtWidgets.QStyleOptionSlider()
r = self.style().subControlRect(QtWidgets.QStyle.CC_Slider, opt, QtWidgets.QStyle.SC_SliderHandle, self)
pixmap = QtGui.QPixmap(r.width() + 2*2, r.height() + 2*2)
pixmap.fill(QtCore.Qt.transparent)
r = pixmap.rect().adjusted(2, 2, -2, -2)
pixmap_painter = QtGui.QPainter(pixmap)
pixmap_painter.setRenderHint(QtGui.QPainter.Antialiasing)
pixmap_painter.setPen(QtGui.QPen(self.palette().color(QtGui.QPalette.Shadow), 2))
pixmap_painter.setBrush(self.palette().color(QtGui.QPalette.Base))
pixmap_painter.drawRoundedRect(r, 4, 4)
pixmap_painter.end()
r.moveCenter(p.toPoint())
painter.drawPixmap(r, pixmap)
def minimumSizeHint(self):
return QtCore.QSize(15, 15)
def sizeHint(self):
return QtCore.QSize(336, 336)
def mousePressEvent(self, event):
self.update_pos(event.pos())
super(PathSlider, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
self.update_pos(event.pos())
super(PathSlider, self).mouseMoveEvent(event)
def update_pos(self, point):
if self.stroke_path.contains(point):
n_p = int((self.maximum() + 1 - self.minimum())/self.singleStep())
ls = []
for i in range(n_p):
p = self.scale_path.pointAtPercent(i*1.0/n_p)
ls.append(QtCore.QLineF(point, p).length())
j = ls.index(min(ls))
val = int(j*(self.maximum() + 1 - self.minimum())/n_p)
self.setValue(val)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
s1 = PathSlider(minimum=0, maximum=100)
s2 = PathSlider(minimum=0, maximum=100)
s = QtWidgets.QSlider(minimum=0, maximum=100)
s.valueChanged.connect(s1.setValue)
s.valueChanged.connect(s2.setValue)
s1.valueChanged.connect(s.setValue)
s2.valueChanged.connect(s.setValue)
c1 = QtCore.QPointF(5, -15)
c2 = QtCore.QPointF(220, -15)
path = QtGui.QPainterPath(QtCore.QPointF(5, 100))
path.cubicTo(c1, c2, QtCore.QPointF(235, 100))
s1.setPath(path)
c1 = QtCore.QPointF(5, 15)
c2 = QtCore.QPointF(220, 15)
path = QtGui.QPainterPath(QtCore.QPointF(5, -100))
path.cubicTo(c1, c2, QtCore.QPointF(235, -100))
s2.setPath(path)
lay = QtWidgets.QHBoxLayout(w)
lay.addWidget(s1)
lay.addWidget(s2)
lay.addWidget(s)
w.show()
sys.exit(app.exec_())
Now I want that when a user clicks in a cell of the grid, it becomes editable and he can edit the cell values/images and update the data accordingly.
I don't wish to use the in-built edit, delete and update buttons of grid. How can I do that?please guide me.Thank you in advance
given bellow is my code:
import sys
from PyQt4 import QtCore, QtGui
class Setting:
WIDTH = 80
HEIGHT = 80
X, Y = 7, 5
class QS(QtGui.QGraphicsScene):
def __init__(self, parent=None):
super(QS, self).__init__(QtCore.QRectF(0, 0, X * Setting.WIDTH, Y * Setting.HEIGHT), parent)
def drawBackground(self, painter, rect):
width = X * Setting.WIDTH
height = Y * Setting.HEIGHT
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))
for _ in range(Y+1):
painter.drawLine(l)
l.translate(0, Setting.HEIGHT)
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))
for _ in range(X+1):
painter.drawLine(l)
l.translate(Setting.WIDTH, 0)
pixmap = QtGui.QPixmap("checkmark.png").scaled(Setting.WIDTH,
Setting.HEIGHT,
QtCore.Qt.IgnoreAspectRatio,
QtCore.Qt.SmoothTransformation)
p = QtCore.QPointF()
for i in range(X):
p = QtCore.QPointF(Setting.WIDTH*i, 0)
for j in range(Y):
painter.drawPixmap(p, pixmap)
p += QtCore.QPointF(0, Setting.HEIGHT)
def mousePressEvent(self,evnt):
# print event
# print(dir(evnt))
print evnt.screenPos().x()
print evnt.screenPos().y()
self.ix = int(evnt.screenPos().x()/Setting.WIDTH)
self.ix = int(evnt.screenPos().y()/Setting.HEIGHT)
print self.ix,self.iy
class QV(QtGui.QGraphicsView):
pass
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QS(self)
view = QV(scene)
self.setCentralWidget(view)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
The most common way to establish options is to use a context menu, in it you create a menu where you add the QActions, and depending on the selection of the corresponding QAction will return, depending on the choice you must change the image or delete it as shown below (Take in when to draw the figures I used items):
import sys
from PyQt4 import QtCore, QtGui
class Setting:
WIDTH = 80
HEIGHT = 80
X, Y = 7, 5
class QS(QtGui.QGraphicsScene):
def __init__(self, parent=None):
super(QS, self).__init__(QtCore.QRectF(0, 0, X * Setting.WIDTH, Y * Setting.HEIGHT), parent)
pixmap = QtGui.QPixmap("checkmark.png").scaled(Setting.WIDTH, Setting.HEIGHT,
QtCore.Qt.IgnoreAspectRatio,
QtCore.Qt.SmoothTransformation)
for i in range(X):
p = QtCore.QPointF(Setting.WIDTH*i, 0)
for j in range(Y):
item = self.addPixmap(pixmap)
item.setPos(p)
p += QtCore.QPointF(0, Setting.HEIGHT)
def drawBackground(self, painter, rect):
width = X * Setting.WIDTH
height = Y * Setting.HEIGHT
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))
for _ in range(Y+1):
painter.drawLine(l)
l.translate(0, Setting.HEIGHT)
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))
for _ in range(X+1):
painter.drawLine(l)
l.translate(Setting.WIDTH, 0)
class QV(QtGui.QGraphicsView):
def __init__(self, parent=None):
super(QV, self).__init__(parent)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.on_customContextMenuRequested)
def on_customContextMenuRequested(self, pos):
items = self.items(pos)
if items:
menu = QtGui.QMenu()
change_action = menu.addAction("Change image")
delete_action = menu.addAction("Delete")
action = menu.exec(self.mapToGlobal(pos))
if action is change_action:
filename = QtGui.QFileDialog.getOpenFileName(self,
"Open Image", QtCore.QDir.currentPath() , "Image Files (*.png *.jpg *.bmp)")
if filename:
pixmap = QtGui.QPixmap(filename)
if not pixmap.isNull():
pixmap = pixmap.scaled(Setting.WIDTH, Setting.HEIGHT,
QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation)
for item in items:
if isinstance(item, QtGui.QGraphicsPixmapItem):
item.setPixmap(pixmap)
elif action is delete_action:
for it in reversed(items):
self.scene().removeItem(it)
del it
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QS(self)
view = QV(scene)
self.setCentralWidget(view)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
How can i slide QtWidgets.QStackedWidget pages on QPushButton.clicked as shown in below image (in right) ?
action: on left button press QStackedWidget page index will set to 0 & on right button press QStackedWidget page index will set to 1
Using this example written in C++ I translated it to PySide2 in addition to giving them certain improvements:
import random
from PySide2 import QtCore, QtGui, QtWidgets
class SlidingStackedWidget(QtWidgets.QStackedWidget):
def __init__(self, parent=None):
super(SlidingStackedWidget, self).__init__(parent)
self.m_direction = QtCore.Qt.Horizontal
self.m_speed = 500
self.m_animationtype = QtCore.QEasingCurve.OutCubic
self.m_now = 0
self.m_next = 0
self.m_wrap = False
self.m_pnow = QtCore.QPoint(0, 0)
self.m_active = False
def setDirection(self, direction):
self.m_direction = direction
def setSpeed(self, speed):
self.m_speed = speed
def setAnimation(self, animationtype):
self.m_animationtype = animationtype
def setWrap(self, wrap):
self.m_wrap = wrap
#QtCore.Slot()
def slideInPrev(self):
now = self.currentIndex()
if self.m_wrap or now > 0:
self.slideInIdx(now - 1)
#QtCore.Slot()
def slideInNext(self):
now = self.currentIndex()
if self.m_wrap or now < (self.count() - 1):
self.slideInIdx(now + 1)
def slideInIdx(self, idx):
if idx > (self.count() - 1):
idx = idx % self.count()
elif idx < 0:
idx = (idx + self.count()) % self.count()
self.slideInWgt(self.widget(idx))
def slideInWgt(self, newwidget):
if self.m_active:
return
self.m_active = True
_now = self.currentIndex()
_next = self.indexOf(newwidget)
if _now == _next:
self.m_active = False
return
offsetx, offsety = self.frameRect().width(), self.frameRect().height()
self.widget(_next).setGeometry(self.frameRect())
if not self.m_direction == QtCore.Qt.Horizontal:
if _now < _next:
offsetx, offsety = 0, -offsety
else:
offsetx = 0
else:
if _now < _next:
offsetx, offsety = -offsetx, 0
else:
offsety = 0
pnext = self.widget(_next).pos()
pnow = self.widget(_now).pos()
self.m_pnow = pnow
offset = QtCore.QPoint(offsetx, offsety)
self.widget(_next).move(pnext - offset)
self.widget(_next).show()
self.widget(_next).raise_()
anim_group = QtCore.QParallelAnimationGroup(
self, finished=self.animationDoneSlot
)
for index, start, end in zip(
(_now, _next), (pnow, pnext - offset), (pnow + offset, pnext)
):
animation = QtCore.QPropertyAnimation(
self.widget(index),
b"pos",
duration=self.m_speed,
easingCurve=self.m_animationtype,
startValue=start,
endValue=end,
)
anim_group.addAnimation(animation)
self.m_next = _next
self.m_now = _now
self.m_active = True
anim_group.start(QtCore.QAbstractAnimation.DeleteWhenStopped)
#QtCore.Slot()
def animationDoneSlot(self):
self.setCurrentIndex(self.m_next)
self.widget(self.m_now).hide()
self.widget(self.m_now).move(self.m_pnow)
self.m_active = False
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
slidingStacked = SlidingStackedWidget()
for i in range(10):
label = QtWidgets.QLabel(
"Qt is cool " + i * "!", alignment=QtCore.Qt.AlignCenter
)
color = QtGui.QColor(*random.sample(range(255), 3))
label.setStyleSheet(
"QLabel{ background-color: %s; color : white; font: 40pt}"
% (color.name(),)
)
slidingStacked.addWidget(label)
button_prev = QtWidgets.QPushButton(
"Previous", pressed=slidingStacked.slideInPrev
)
button_next = QtWidgets.QPushButton(
"Next", pressed=slidingStacked.slideInNext
)
hlay = QtWidgets.QHBoxLayout()
hlay.addWidget(button_prev)
hlay.addWidget(button_next)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addLayout(hlay)
lay.addWidget(slidingStacked)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Nota: For PyQt5 only change Slot to pyqtSlot
For PyQt5 I have converted #eyllanesc's answer:
import random
from PyQt5 import QtCore, QtGui, QtWidgets
class SlidingStackedWidget(QtWidgets.QStackedWidget):
def __init__(self, parent=None):
super(SlidingStackedWidget, self).__init__(parent)
self.m_direction = QtCore.Qt.Horizontal
self.m_speed = 500
self.m_animationType = QtCore.QEasingCurve.InCurve
self.m_now = 0
self.m_next = 0
self.m_wrap = False
self.m_pnow = QtCore.QPoint(0, 0)
self.m_active = False
def setDirection(self, direction):
self.m_direction = direction
def setSpeed(self, speed):
self.m_speed = speed
def setAnimation(self, animation_type):
self.m_animationType = animation_type
def setWrap(self, wrap):
self.m_wrap = wrap
#QtCore.pyqtSlot()
def slideInPrev(self):
now = self.currentIndex()
if self.m_wrap or now > 0:
self.slideInIdx(now - 1)
#QtCore.pyqtSlot()
def slideInNext(self):
now = self.currentIndex()
if self.m_wrap or now < (self.count() - 1):
self.slideInIdx(now + 1)
def slideInIdx(self, idx):
if idx > (self.count() - 1):
idx = idx % self.count()
elif idx < 0:
idx = (idx + self.count()) % self.count()
self.slideInWgt(self.widget(idx))
def slideInWgt(self, new_widget):
if self.m_active:
return
self.m_active = True
_now = self.currentIndex()
_next = self.indexOf(new_widget)
if _now == _next:
self.m_active = False
return
offset_X, offset_Y = self.frameRect().width(), self.frameRect().height()
self.widget(_next).setGeometry(self.frameRect())
if not self.m_direction == QtCore.Qt.Horizontal:
if _now < _next:
offset_X, offset_Y = 0, -offset_Y
else:
offset_X = 0
else:
if _now < _next:
offset_X, offset_Y = -offset_X, 0
else:
offset_Y = 0
page_next = self.widget(_next).pos()
pnow = self.widget(_now).pos()
self.m_pnow = pnow
offset = QtCore.QPoint(offset_X, offset_Y)
self.widget(_next).move(page_next - offset)
self.widget(_next).show()
self.widget(_next).raise_()
anim_group = QtCore.QParallelAnimationGroup(self)
anim_group.finished.connect(self.animationDoneSlot)
for index, start, end in zip(
(_now, _next), (pnow, page_next - offset), (pnow + offset, page_next)
):
animation = QtCore.QPropertyAnimation(self.widget(index), b'pos')
animation.setEasingCurve(self.m_animationType)
animation.setDuration(self.m_speed)
animation.setStartValue(start)
animation.setEndValue(end)
anim_group.addAnimation(animation)
self.m_next = _next
self.m_now = _now
self.m_active = True
anim_group.start(QtCore.QAbstractAnimation.DeleteWhenStopped)
#QtCore.pyqtSlot()
def animationDoneSlot(self):
self.setCurrentIndex(self.m_next)
self.widget(self.m_now).hide()
self.widget(self.m_now).move(self.m_pnow)
self.m_active = False
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
slidingStacked = SlidingStackedWidget()
for i in range(10):
label = QtWidgets.QLabel(
"Qt is cool " + i * "!", alignment=QtCore.Qt.AlignCenter
)
color = QtGui.QColor(*random.sample(range(255), 3))
label.setStyleSheet(
"QLabel{ background-color: %s; color : white; font: 40pt}"
% (color.name(),)
)
slidingStacked.addWidget(label)
button_prev = QtWidgets.QPushButton(
"Previous", pressed=slidingStacked.slideInPrev
)
button_next = QtWidgets.QPushButton(
"Next", pressed=slidingStacked.slideInNext
)
hlay = QtWidgets.QHBoxLayout()
hlay.addWidget(button_prev)
hlay.addWidget(button_next)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addLayout(hlay)
lay.addWidget(slidingStacked)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())