Everything works in another program, where only textedit, and in the main program - does not work. Apparently the reason is in some kind of Layout. Can you tell me what could be? I am attaching an excerpt from the code.))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
from PyQt5 import QtCore, QtGui, QtWidgets
import os
from PyQt5.QtWidgets import QTextEdit, QApplication
from PyQt5.QtGui import QTextDocument, QTextCursor, QTextBlockFormat
class TextEditor(QTextEdit):
def __init__(self,parent):
super().__init__(parent)
self.initUI()
def initUI(self):
cursor = QTextCursor(self.document())
cursor.select(QTextCursor.Document)
fmt = QTextBlockFormat()
fmt.setTextIndent(60)
cursor.mergeBlockFormat(fmt)
font= QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
self.setFont(font)
self.show()
def insertFromMimeData(self, md):
cursor = self.textCursor()
fmt = QTextBlockFormat()
fmt.setTextIndent(cursor.blockFormat().textIndent())
cursor.mergeBlockFormat(fmt)
cursor.insertText(md.text())
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.textEdit = TextEditor(self.centralwidget)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout_2.addWidget(self.textEdit)
self.gridLayout.addLayout(self.horizontalLayout_2, 2, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(11)
self.label.setFont(font)
self.label.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 1, 0, 1, 1)
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.pig()
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
self.butclick()
self.butclick2()
Use QTextBlockFormat.setTextIndent and QTextCursor.mergeBlockFormat instead of QTextEdit.setStyleSheet:
cursor = QTextCursor(self.textEdit.document())
cursor.select(QTextCursor.Document)
fmt = QTextBlockFormat()
fmt.setTextIndent(60)
cursor.mergeBlockFormat(fmt)
Result:
UPD As #ВалерияГригорьева rightfully pointed out, the indent is discarded when pasting plain text (although I would expect it should be taken from the current block by the default implementation). Therefore we need to override QTextEdit.insertFromMimeData and apply indentation on insert:
cursor = self.textCursor()
fmt = QTextBlockFormat()
fmt.setTextIndent(cursor.blockFormat().textIndent())
cursor.mergeBlockFormat(fmt)
cursor.insertText(md.text())
For pasting the rich text, on the other hand, we don't want to suppress indents because they are a part of the rich text formatting and thus we can rely on the default implementation of QTextEdit.insertFromMimeData.
Complete code sample (for PyQt5):
import sys
from PyQt5.QtWidgets import QTextEdit, QApplication
from PyQt5.QtGui import QTextDocument, QTextCursor, QTextBlockFormat
class TextEditor(QTextEdit):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# format text edit
self.setPlainText(
"Impedit voluptatem sequi quae quo quos. \n" +
"Asperiores non repellat culpa nihil. \n" +
"Voluptatum ut numquam dolorem molestiae voluptatem " +
"est modi necessitatibus. \n" +
"Hic rerum voluptas voluptatem. \n" +
"Ut expedita unde eum molestias voluptatem aut" +
"dignissimos dolor. \n" +
"Non illo dolore ut doloremque ut.")
cursor = QTextCursor(self.document())
cursor.select(QTextCursor.Document)
fmt = QTextBlockFormat()
fmt.setTextIndent(60)
cursor.mergeBlockFormat(fmt)
# setup UI
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Text indent')
self.show()
def insertFromMimeData(self, md):
# leave processing of the rich text as it is
if md.hasFormat("application/x-qrichtext") or md.hasHtml():
super().insertFromMimeData(md)
else:
# force indentation from the current block
# (shouldn't Qt do this by default, huh?)
cursor = self.textCursor()
fmt = QTextBlockFormat()
fmt.setTextIndent(cursor.blockFormat().textIndent())
cursor.mergeBlockFormat(fmt)
cursor.insertText(md.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = TextEditor()
sys.exit(app.exec())
Related
The program does not work changing the paragraph indentation in the text. I want the text to always be indented in TextEdit. If do not use other widgets than TextEdit, then everything works.I think that there are problems with layouts, but I can’t understand, because I don’t really understand Python, but this problem needs to be solved. What could be the problem?
PyQt5 import QtCore, QtGui, QtWidgets
import os
from PyQt5.QtWidgets import QTextEdit, QApplication
from PyQt5.QtGui import QTextDocument, QTextCursor, QTextBlockFormat
class TextEditor(QTextEdit):
def __init__(self,parent):
super().__init__(parent)
self.initUI()
def initUI(self):
cursor = QTextCursor(self.document())
cursor.select(QTextCursor.Document)
fmt = QTextBlockFormat()
fmt.setTextIndent(60)
cursor.mergeBlockFormat(fmt)
font= QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
self.setFont(font)
self.show()
def insertFromMimeData(self, md):
cursor = self.textCursor()
fmt = QTextBlockFormat()
fmt.setTextIndent(cursor.blockFormat().textIndent())
cursor.mergeBlockFormat(fmt)
cursor.insertText(md.text())
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.textEdit = TextEditor(self.centralwidget)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout_2.addWidget(self.textEdit)
self.gridLayout.addLayout(self.horizontalLayout_2, 2, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(11)
self.label.setFont(font)
self.label.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 1, 0, 1, 1)
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.pig()
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
self.butclick()
self.butclick2()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Please how can I add permanently different widgets in the tabs and save them permanently
<html>
<pre>
import wx
tabs = []
with open('test.txt','r') as file:
for element in file.readlines():
tabs.append(element)
class TabPanel(wx.Panel):
def __init__(self, parent, pageNum):
self.parent = parent
self.pageNum = pageNum
wx.Panel.__init__(self, parent=parent)
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Notebook", size=(600,400))
panel = wx.Panel(self)
self.tab_num = len(tabs)
self.notebook = wx.Notebook(panel)
for tab in tabs:
name = "Page " + str(tab)
tab = TabPanel(self.notebook, 1)
self.notebook.AddPage(tab, name)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
btn = wx.Button(panel, label="Add Page")
btn.Bind(wx.EVT_BUTTON, self.addPage)
sizer.Add(btn)
panel.SetSizer(sizer)
self.Layout()
self.Show()
def addPage(self, event):
self.tab_num += 1
new_tab = TabPanel(self.notebook, self.tab_num)
self.notebook.AddPage(new_tab, "Page %s" % self.tab_num)
tabs.append(self.tab_num)
print()
with open('test.txt','a+') as file:
file.write(str(self.tab_num))
file.write('\n')
if __name__ == "__main__":
app = wx.App(False)
frame = DemoFrame()
app.MainLoop()
</pre>
</html>
Something like this should get you started (sorry not readdly familiar with python):
class DemoFrame(wx.Frame):
def __init__(self):
self.tab_num = 1
wx.Frame.__init__(self, None, wx.ID_ANY, "Notebook", size=(600,400))
panel = wx.Panel(self)
with open( "test.txt", "r" as file:
self.tab_num = file.read()
self.notebook = wx.Notebook(panel)
for tab in [1..self.tab_num]:
name = "Page " + str(tab)
tab = TabPanel(self.notebook, 1)
self.notebook.AddPage(tab, name)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
btn = wx.Button(panel, label="Add Page")
btn.Bind(wx.EVT_BUTTON, self.addPage)
sizer.Add(btn)
panel.SetSizer(sizer)
self.Layout()
self.Show()
def addPage(self, event):
self.tab_num += 1
new_tab = TabPanel(self.notebook, self.tab_num)
self.notebook.AddPage(new_tab, "Page %s" % self.tab_num)
tabs.append(self.tab_num)
print()
with open('test.txt','a+') as file:
file.write(str(self.tab_num))
file.write('\n')
if __name__ == "__main__":
app = wx.App(False)
frame = DemoFrame()
app.MainLoop()
from PyQt5.QtWidgets import QGraphicsItem
from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer
from PyQt5.QtCore import Qt, QRectF
class MyItem(QGraphicsItem):
def __init__(self, parent ):
super().__init__( parent = parent )
self.textRect = QRectF( 0,0,100,100 )
self.SvgGItem = QGraphicsSvgItem(self)
self.Svg_renderer = QSvgRenderer("./item.svg")
self.SvgGItem.setSharedRenderer ( self.Svg_renderer )
self.SvgGItem.setElementId( "1" )
def paint(self, painter, option, widget):
alignFlags = Qt.AlignLeft | Qt.AlignTop
painter.drawText( self.textRect, alignFlags , "text" )
I want the text being above the svg item. This code is for example, the real code is pretty difficult, but the sence is the same. setZValue() does not work in this case and it is clear. There is some way or other decision?
Try it:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer
class MyItem(QGraphicsItem):
def __init__(self):
super().__init__()
self.x = 0
self.y = 0
self.w = 100
self.h = 100
self.width = 8
self.text = "None"
self.setFlags(MyItem.ItemIsSelectable | MyItem.ItemIsMovable)
def boundingRect(self):
return QRectF(self.x, self.y, self.w, self.h)
def paint(self, painter, option, widget):
painter.setPen(QPen(Qt.blue, self.width))
rec = self.boundingRect().adjusted(self.width / 2,
self.width / 2,
-self.width / 2,
-self.width / 2)
painter.setBrush(Qt.yellow)
painter.drawRect(rec)
painter.setFont(QFont("Arial", 16))
self.textRect = QRectF( 0, 0, 100, 100 )
alignFlags = Qt.AlignCenter
painter.drawText( self.textRect, alignFlags , "! text !" )
class MySvg(QGraphicsSvgItem):
def __init__(self):
super(MySvg, self).__init__('blank.svg')
self.setFlags(QGraphicsItem.ItemIsSelectable|
QGraphicsItem.ItemIsMovable)
class GraphicsScene(QGraphicsScene):
def __init__(self):
super().__init__()
self.setSceneRect(-200, -200, 400, 400)
self.fillScene()
def fillScene(self):
test = MySvg()
self.addItem(test)
item = MyItem()
self.addItem(item)
def drawBackground(self, painter, rect):
painter.setPen(QPen(Qt.green, 3))
painter.drawRect(self.sceneRect())
painter.setPen(QPen(Qt.red, 3))
painter.drawText(0, 0, '0')
painter.drawText(-200, -200, '-200')
painter.drawText(200, 200, '200')
class Main(QWidget):
def __init__(self):
super(Main, self).__init__()
self.scene = GraphicsScene()
self.view = QGraphicsView(self)
self.view.setScene(self.scene)
hbox = QHBoxLayout()
hbox.addWidget(self.view)
self.setLayout(hbox)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
ex.show()
sys.exit(app.exec_())
I am trying to create a custom widget and adding it to a listwidget following the example here but used a widget as here(Answer) instead of the progress bar. The custom widget works fine independently. But when it is added to the list widget, the horizontal scrolling works perfect but the vertical scroll covers only a portion of the image. Can somebody suggest some improvement?
The code is
import sys
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.scene = QtGui.QGraphicsScene()
self.view = QtGui.QGraphicsView(self.scene)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.view)
self.setLayout(layout)
self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('image.png'), None, self.scene)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.listWidget = QtGui.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(170, 20, 400, 400))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.listWidget.sizePolicy().hasHeightForWidth())
self.listWidget.setSizePolicy(sizePolicy)
self.listWidget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.listWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.listWidget.setObjectName(_fromUtf8("listWidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 200, 113, 32))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.progress)
def progress(self):
item = QtGui.QListWidgetItem(self.ui.listWidget)
item_widget = MainWidget()
item.setSizeHint(item_widget.sizeHint())
self.ui.listWidget.addItem(item)
self.ui.listWidget.setItemWidget(item,item_widget)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
and here is the test image:
I want to rotate a QLabel so that it is oriented vertically. A similar question was asked here: PyQT: Rotate a QLabel so that it's positioned diagonally instead of horizontally but the code in answer causes a recursive loop issue.
Here is the code I am using, why does the code run in a recursive loop and why are the labels not drawn?:
from PyQt4.QtGui import QLabel, QPainter
from PyQt4.QtCore import QSize
from PyQt4 import QtCore, QtGui
import sys
class VerticalLabel(QLabel):
def __init__(self, text = ""):
QtGui.QLabel.__init__(self, text)
self.text = text
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QtCore.Qt.black)
painter.translate(0, self.height()-1)
painter.rotate(-90)
self.setGeometry(self.x(), self.y(), self.height(), self.width())
painter.drawText(0, 0, self.text)
print("text: {0}".format(self.text))
painter.end()
def minimumSizeHint(self):
size = QLabel.minimumSizeHint(self)
return QSize(size.height(), size.width())
def sizeHint(self):
size = QLabel.sizeHint(self)
return QSize(size.height(), size.width())
def setText(self, newText):
self.text = newText
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lbl1 = VerticalLabel('label 1')
lbl2 = VerticalLabel('label 2')
lbl3 = VerticalLabel('label 3')
hBoxLayout = QtGui.QHBoxLayout()
hBoxLayout.addWidget(lbl1)
hBoxLayout.addWidget(lbl2)
hBoxLayout.addWidget(lbl3)
self.setLayout(hBoxLayout)
self.setGeometry(300, 300, 250, 150)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This is slightly modified version of the same code from another answer and remember if you put any print under paintEvent it will print every time if you have any activity on the widget like mouse move or re size or what ever even a touch in window will redraw, because that's what paintEvent means.
class MyLabel(QtGui.QWidget):
def __init__(self, text=None):
super(self.__class__, self).__init__()
self.text = text
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(QtCore.Qt.black)
painter.translate(20, 100)
painter.rotate(-90)
if self.text:
painter.drawText(0, 0, self.text)
painter.end()
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lbl1 = MyLabel('lbl 1')
lbl2 = MyLabel('lbl 2')
lbl3 = MyLabel('lbl 3')
hBoxLayout = QtGui.QHBoxLayout()
hBoxLayout.addWidget(lbl1)
hBoxLayout.addWidget(lbl2)
hBoxLayout.addWidget(lbl3)
self.setLayout(hBoxLayout)
self.setGeometry(300, 300, 250, 150)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()