Recently, I have been struggling with understanding of pyqtProperty usage. In following snippet I try to create a custom button which would change it's text and appearance after clicking on it. I wanted to separate logic and appearance, so I've created custom property 'state', which is used in the stylesheet.
The result looks kind of strange to me. Program throws 'CustomButton' object has no attribute '_state' exception, but runs anyway. Button's text is changing after clicking on it, but color stays the same.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
STYLE = '''
CustomButton {
margin: 50px;
padding: 10px;
}
CustomButton[state='true'] {
background: yellowgreen;
}
CustomButton[state='false'] {
background: orangered;
}
'''
class CustomButton(QtGui.QPushButton):
def __init__(self):
super(CustomButton, self).__init__()
self.setText('ON')
self.setStyleSheet(STYLE)
self._state = True
def g_state(self):
return self._state
def s_state(self, value):
self._state = value
state = QtCore.pyqtProperty(bool, fget=g_state, fset=s_state)
def mousePressEvent(self, event):
print 'clicked'
if self.state == True:
self.state = False
self.setText('OFF')
else:
self.state = True
self.setText('ON')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
b = CustomButton()
b.show()
sys.exit(app.exec_())
There are a couple of issues here.
Firstly, the re-implemented mousePressEvent should call the base-class implementation in order to retain the normal behaviour.
Secondly, changes to properties do not automatically trigger style changes, so you need to explicitly do that in your code.
Also, its worth noting that pyqtProperty can be used as a decorator, which might improve the readability of the code a little.
So, with these changes, the code might look something like this:
...
#QtCore.pyqtProperty(bool)
def state(self):
return self._state
#state.setter
def state(self, value):
self._state = value
def mousePressEvent(self, event):
print 'clicked'
if self.state:
self.state = False
self.setText('OFF')
else:
self.state = True
self.setText('ON')
self.style().unpolish(self)
self.style().polish(self)
self.update()
super(CustomButton, self).mousePressEvent(event)
Related
I wanted to have some hover effect over p tag inside QTextDocument.
I have tried to set QSS for QTextDocument using QTextDocument().setDefaultStyleSheet().
Here's what the result I have obtained;
Script;
from PySide2 import QtWidgets
class Test(QtWidgets.QTextBrowser):
def __init__(self):
super().__init__()
self.document().setDefaultStyleSheet(
"""
p.out{
background-color: "orange";
color: "black";
}
p.out:hover{
background-color: "yellow";
}
"""
)
self.setStyleSheet(
"""
QTextBrowser{
background-color: "black";
color: "white";
}
""")
self.setHtml(
"""
<p class='out'> Checking this </p>
"""
)
test = QtWidgets.QApplication([])
sample = Test()
sample.show()
test.exec_()
Color attribute inside qss worked but the hover doesn't work.
Is there any way of achieving hover effect over fragments of text inside document?
After Understanding, QTextDocument's structure and Syntax Highlighter. We can modify QTextBlock's Format whenever we want. I tried to change it's format using enterEvent, and restore format in leaveEvent.
we can modify box model using QTextFrame using QTextFrameFormat.
from PySide2 import QtWidgets, QtGui
class High(QtGui.QSyntaxHighlighter):
def __init__(self, doc):
super().__init__(doc)
self._formats = tuple(
QtGui.QTextCharFormat() for _ in range(2)
)
self._formats[1].setForeground(
QtGui.QBrush(QtGui.QColor("orange"))
)
self._formats[1].setFontWeight(3)
self._formats[1].setFontUnderline(True)
self._formats[1].setFontPointSize(12)
def highlightBlock(self, text: str):
self.setFormat(
0, len(text), self._formats[self.currentBlockState() if self.currentBlockState() > -1 else 0]
)
class Sample(QtWidgets.QTextBrowser):
def __init__(self):
super().__init__()
self.lighter = High(self.document())
self._prev = None
self.insertBlock("This is a sample line\n")
self.insertBlock("Can also be a\nparagraph thing\n")
self.insertBlock("it's a block, Bye!")
def insertBlock(self, text):
cur = self.textCursor()
cur.block().setUserState(0) # default: -1
cur.insertText(text)
def mouseMoveEvent(self, eve):
super().mouseMoveEvent(eve)
cur = self.cursorForPosition(eve.pos())
if cur.block() == self._prev:
return
self.restore()
cur.block().setUserState(1)
self.lighter.rehighlightBlock(cur.block())
self._prev = cur.block()
def restore(self):
if not self._prev:
return
self._prev.setUserState(0)
self.lighter.rehighlightBlock(self._prev)
self._prev = None
def leaveEvent(self, eve):
self.restore()
super().leaveEvent(eve)
testing = QtWidgets.QApplication([])
sample = Sample()
sample.show()
testing.exec_()
I have a QCompleter on a QTableWidget column. As soon as the user starts editing I would like the completer to pop up, not waiting for them to enter text first. I subclassed the setEditorData function of the QStyledItemDelegate to do this which seems to make the most sense to me, however when I call completer.complete() nothing happens until I finish editing (at which point the popup fires).
Here is my code for the delegate:
class CompleterItemDelegate(QtGui.QStyledItemDelegate):
def createEditor(self, parent, option, index):
completer = QtGui.QCompleter(['test', 'test2'])
completer.setCompletionMode(completer.UnfilteredPopupCompletion)
edit = QtGui.QLineEdit(parent)
edit.setCompleter(completer)
return edit
def setEditorData(self, editor, index):
completer = editor.completer()
completer.complete() # does not fire until after editing is done
completer.popup().show() # no luck here either
print("setting editor data") # this however does work as expected...
super().setEditorData(editor, index)
You have to call complete() when the widget is displayed and for this you can use the showEvent() method:
from PyQt4 import QtCore, QtGui
class LineEdit(QtGui.QLineEdit):
def showEvent(self, event):
if self.completer() is not None:
QtCore.QTimer.singleShot(0, self.completer().complete)
super().showEvent(event)
class CompleterItemDelegate(QtGui.QStyledItemDelegate):
def createEditor(self, parent, option, index):
completer = QtGui.QCompleter(["test", "test2"])
completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
edit = LineEdit(parent)
edit.setCompleter(completer)
return edit
def main(args):
app = QtGui.QApplication(args)
w = QtGui.QTableWidget(4, 4)
delegate = CompleterItemDelegate(w)
w.setItemDelegate(delegate)
w.show()
ret = app.exec_()
return ret
if __name__ == "__main__":
import sys
sys.exit(main(sys.argv))
I've found a way to do css based cell styling in a TableView based on contents in the cell. The following code shows an example:
#!/usr/bin/python3
from PyQt5 import QtWidgets, QtGui, QtCore
class_values = ["zero", "one", "two"]
class Cell(QtWidgets.QWidget):
def initFromItem(self, item):
self.setProperty('dataClass', class_values[int(item.text())])
class TDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, *a):
super(TDelegate, self).__init__(*a)
self.cell = Cell(self.parent())
def paint(self, painter, option, index):
item = index.model().itemFromIndex(index)
self.cell.initFromItem(item)
self.initStyleOption(option, index)
style = option.widget.style() if option.widget else QtWidgets.QApplication.style()
style.unpolish(self.cell)
style.polish(self.cell)
style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, option, painter, self.cell)
class TTableModel(QtGui.QStandardItemModel):
def __init__(self, parent=None):
super(TTableModel, self).__init__(parent)
for i in range(5):
self.appendRow([QtGui.QStandardItem(str((x+i) % 3)) for x in range(5)])
class TTableView(QtWidgets.QTableView):
def __init__(self, parent=None):
super(TTableView, self).__init__(parent)
self.setItemDelegate(TDelegate(self))
class Main(QtWidgets.QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.table = TTableView(self)
self.model = TTableModel(self)
self.table.setModel(self.model)
self.setCentralWidget(self.table)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet("""
Cell[dataClass=zero]::item { background-color: gray; }
Cell[dataClass=one]::item { background-color: green; font-style: italic }
Cell[dataClass=two]::item { font-weight: bold }
""")
mainWin = Main()
mainWin.show()
sys.exit(app.exec_())
This generates a table like this:
TableView with per cell styling
The problem is that while the colours work, the font styling has no effect. What am I doing wrong? How could I improve my code? And how does it work? For example, why does the CSS selector have to include the ::item. All answers gratefully received. But please bear in mind that the need for CSS based styling is essential to the project.
This is due to a bug in qt (v5.9.5) that ignores all font styling information when creating a CE_ItemViewItem (see QStyleSheetStyle::drawControl). Cheating by creating something else like a CE_ToolBoxTabLabel (which does correct handling of fonts in drawControl) does get you font formatting, but gets you on the colour because the rendering uses the button face palette, not the one specified in the option (or associated CSS). So you can have one or the other but not both. I know of no workaround.
As to how this works. In QStyleSheetStyle::drawControl for a CE_ItemViewItem the CSS for the subrole of ::item is looked up and if present, applied to a copy of the option (but not the font styling), and then the Item is drawn based on the updated option and its updated palette. Unfortunately there is no way to break into this code since there is no way to apply stylesheets from PyQt (since QStyleSheet is not part of the public API of Qt).
I am currently making a program where a user selects an image qpushbutton. I already have superseded mouseMoveEvent, mousePressEvent, and mouseReleaseEvent in the button class to get a movable button. The buttons are currently moving independently, but I would like the buttons to move so that the horizontal distance between them stays the same.
So currently in pseudo code I have:
import stuff
import mvbutton as mv
class MasterClass(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
#more setup stuff, layout, etc
self.addbutton(image,name,size)
def addbutton(#args):
self.button=mv.dragbutton(#args)
#some more setup
#now rename so that each button has its own name
if name== "name1":
self.name1=self.button
else:
self.name2=self.button
self.button=""
#more code to set up
I supersede the mouse motion/press/release functions in the dragbutton class. I cannot, therefore reference the new self.name# there. So the self.move(pos) in my dragbutton class cannot get the self.name# because it is a different self. Any ideas on how I could get this to work? Thanks.
Done something very rough after trying to understand your requirement.
Hope this helps.
EDIT
tried to add more accuracy in moving. Won't do real time moving cause it has problems with lag and update. I guess the moving won't be jittery any more.
from PyQt4 import QtGui
import sys
class MultiButton(QtGui.QWidget):
def __init__(self, *args, **kwargs):
QtGui.QWidget.__init__(self, *args, **kwargs)
self._b1 = QtGui.QPushButton("B1")
self._b2 = QtGui.QPushButton("B2")
self._arrangeWidgets()
self.setStyleSheet("background-color: rgb(0, 0, 0);\n"+\
"color: rgb(255, 255, 255);\n"+\
"border:1px solid #7F462C ;\n")
self._moveStart = False
self._startX = 0
self._startY = 0
def _arrangeWidgets(self):
layout = QtGui.QHBoxLayout()
layout.addWidget(self._b1)
#horizontal spacing remains constant now
layout.addSpacing(90)
layout.addWidget(self._b2)
self.setLayout(layout)
def mousePressEvent(self,event):
self._moveStart = True
self._startX = event.pos().x() - self.pos().x()
self._startY = event.pos().y() - self.pos().y()
return QtGui.QWidget.mousePressEvent(self, event)
def mouseReleaseEvent(self, event):
if self._moveStart:
self.setGeometry(event.pos().x() - self._startX,event.pos().y() - self._startY,self.width(),self.height())
self._moveStart = False
self._startX = 0
self._startY = 0
return QtGui.QWidget.mouseReleaseEvent(self, event)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
wd = QtGui.QMainWindow()
wd.resize(500,500)
mb = MultiButton()
mb.setFixedSize(200,50)
wd.setCentralWidget(mb)
wd.show()
sys.exit(app.exec_())
here the MultiButton widget moves the two buttons keeping the horizontal space between the two always constant.
This is my keyPressEvent
def keyPressEvent(self , e):
key = e.key()
if key == QtCore.Qt.Key_Escape:
self.close()
elif key == QtCore.Qt.Key_A:
print 'Im here'
However if I click on A , it doesn't print. However the window is closing if I click on Escape.Where am I going wrong?
EDIT:
Basically I have a window with a lineedit and a pushbutton. I want to link the button to a function by clicking on Enter, lets say fun. This is my code
import sys
from PyQt4 import QtGui , QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example , self).__init__()
self.window()
def window(self):
self.setWindowTitle('Trial')
self.layout = QtGui.QGridLayout()
self.text = QtGui.QLineEdit()
self.first = QtGui.QPushButton('Button')
self.layout.addWidget(self.text , 0 , 0)
self.layout.addWidget(self.first , 1 , 0)
self.setLayout(self.layout)
self.first.clicked.connect(self.fun)
self.show()
def fun(self):
//do something
def keyPressEvent(self , e):
key = e.key()
if key == QtCore.Qt.Key_Escape:
self.close()
elif key == QtCore.Qt.Key_Enter:
self.fun()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I would add more keys later on. But none of them except Escape are working/
The method you're looking for is called keyPressEvent, not KeyPressEvent.
It seems that your QLineEdit is stealing your KeyPress events. If handling the enter key from the line edit is all you want to do, you could connect the returnPressed signal to self.fun:
self.text.returnPressed.connect(self.fun) # in PySide
Otherwise, you will have to mess around with event filters. I'll try posting some code later on.
Your final edit made it clearer. You can safely drop keyPressEvent and just use:
self.text.returnPressed.connect(self.fun)
self.button.clicked.connect(self.fun)
What a messy answer this turned out to be :)
You are making the GUI application, right? if yes then printing like that will print in the console. try this...
QtGui.QMessageBox.information(self,"hello","I m here")