How to make every paragraph in plaintextedit have indent in Pyside6? - python

I want every paragraph in plainTextEdit.text has text-indent.
I tried to use setTextIndent(). But it didn't work.
This is my code
from ui2 import Ui_Form
from PySide6.QtWidgets import QApplication,QWidget
from PySide6 import QtCore,QtGui
from PySide6.QtGui import QTextCursor
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
doc=self.ui.plainTextEdit.document()
a=doc.firstBlock().blockFormat()
a.setTextIndent(100)
cursor = QTextCursor(doc)
cursor.setBlockFormat(a)
cursor.insertText("This is the first paragraph\nThis is the second paragraph")
print(self.ui.plainTextEdit.document().toPlainText())
app = QApplication([])
mainw = MainWindow()
mainw.show()
app.exec()
This is my print:
This is the first paragraph
This is the second paragraph
which don't have textindent.

You have to use QTextEdit:
from PySide6.QtWidgets import QApplication, QTextEdit
from PySide6.QtGui import QTextCursor
app = QApplication([])
te = QTextEdit()
te.resize(640, 480)
te.show()
cursor = QTextCursor(te.document())
block_format = cursor.blockFormat()
block_format.setTextIndent(100)
cursor.setBlockFormat(block_format)
cursor.insertText("This is the first paragraph\nThis is the second paragraph")
app.exec()

Related

PYQT5 GIF freezes on GUI initialization

Keep gif running while GUI starts. Is that possible? I have read many reporitys but none with the true and understandable answer.
I have prepared a code example that shows the problem.
import sys
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5 import QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QSize, QThread
class Main_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(500, 500))
self.setWindowTitle("Main Window")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
gif = QLabel(self)
gif.setGeometry(0,0,500,500)
self.movie = QMovie(r"C:\Users\...\Pictures\Icon_LOAD.gif")
gif.setMovie(self.movie)
self.movie.start()
# #Call event handler to process the queue, but it is shocking, dirty and unsuitable
#app.processEvents()
self.show()
for i in range(0,1000000):
print(i)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWin = Main_Window()
sys.exit(app.exec_())
Now it works smooth
import sys
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5 import QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QSize
from threading import Thread
class Main_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(500, 500))
self.setWindowTitle("Main Window")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
gif = QLabel(self)
gif.setGeometry(0,0,500,500)
self.movie = QMovie(r"gif.gif")
gif.setMovie(self.movie)
self.movie.start()
self.show()
Thread(target=self.function_on_thread).start()
def function_on_thread(self):
for i in range(0,1000000):
print(i)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWin = Main_Window()
sys.exit(app.exec_())

How to get a button to move a widget in relation to its current position every time the button is pressed?

Here's my attempt at making this work:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.on_click()
def initUI(self):
button = QPushButton('PyQt5 button', self)
button.move(100,200)
button.clicked.connect(self.on_click)
def on_click(self):
text = QTextEdit(self)
insideText = "Text"
text.document().setPlainText(insideText)
text.resize(100,25)
print('PyQt5 button click')
position = text.pos()
text.move(position.x()+50,position.y()+0)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
Maybe I need to make the QTextEdit go somewhere in the initUI() method, instead of being defined in on_click(). I already tried this but I get "text is not defined", and I'm not sure how to reference it inside another function/method. Any help is appreciated.
Edit: Here's the working code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton,QTextEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.on_click()
def initUI(self):
self.button = QPushButton('PyQt5 button', self)
self.button.move(100,200)
self.button.clicked.connect(self.on_click)
self.text = QTextEdit(self)
insideText = "Text"
self.text.document().setPlainText(insideText)
self.text.resize(100,25)
def on_click(self):
print('PyQt5 button click')
position = self.text.pos()
self.text.move(position.x()+50,position.y()+0)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())

PyQt5 Layouts not moving button [duplicate]

I'm making an application in PyQt4 and this is my code so far:
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initUi()
def initUi(self):
self.setWindowTitle('Main Menu')
self.setFixedSize(1200, 625)
self.firstWidgets()
self.show()
def firstWidgets(self):
self.vbox1 = QtGui.QVBoxLayout()
self.task1 = QtGui.QLabel('Check 1', self)
self.task1CB = QtGui.QCheckBox(self)
self.hbox1 = QtGui.QHBoxLayout()
self.hbox1.addWidget(self.task1)
self.hbox1.addWidget(self.task1CB)
self.vbox1.addLayout(self.hbox1)
self.setLayout(self.vbox1)
def main():
application = QtGui.QApplication(sys.argv)
gui = MainWindow()
sys.exit(application.exec_())
if __name__=='__main__':
main()
My problem is in MainWindow.firstWidgets(). I try to set a layout but I get an error even though that's my first time using .setLayout for that form, which confuses me.
QWidget::setLayout: Attempting to set QLayout "" on MainWindow "",
which already has a layout
You can't set a QLayout directly on the QMainWindow. You need to create a QWidget and set it as the central widget on the QMainWindow and assign the QLayout to that.
wid = QtGui.QWidget(self)
self.setCentralWidget(wid)
layout = QtGui.QVBoxLayout()
wid.setLayout(layout)
NOTE: This is for Qt4 -- see the other answer on this question for the Qt5 updated code.
Just an update to Brenden Abel's answer:
QWidget and QVBoxLayout (for Python3, PyQt5) are now contained in the PyQt5.QtWidgets module and not the PyQt5.QtGui module.
So updated code:
wid = QtWidgets.QWidget(self)
self.setCentralWidget(wid)
layout = QtWidgets.QVBoxLayout()
wid.setLayout(layout)
This is an example using PyQt5
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('My App')
# Cannot set QxxLayout directly on the QMainWindow
# Need to create a QWidget and set it as the central widget
widget = QWidget()
layout = QVBoxLayout()
b1 = QPushButton('Red' ); b1.setStyleSheet("background-color: red;")
b2 = QPushButton('Blue' ); b2.setStyleSheet("background-color: blue;")
b3 = QPushButton('Yellow'); b3.setStyleSheet("background-color: yellow;")
layout.addWidget(b1)
layout.addWidget(b2)
layout.addWidget(b3)
widget.setLayout(layout)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Set mouse pointer cursor on QTextEdit

Aiming for Qt StyleSheet cursor: pointer; option but in PyQt5; as you can see by executing the code below, the cursor changes but only at the border of QTextEdit, not in the text area. I need it to also change in the text area.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QTextEdit
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.myLayout = QHBoxLayout()
self.status = QTextEdit()
self.status.setStyleSheet("QTextEdit {min-width:500px;min-height:200px;border:15px solid green;}")
self.status.setCursor(Qt.WaitCursor)
self.status.setPlainText("test")
self.myLayout.addWidget(self.status)
self.setLayout(self.myLayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
Also that sets the Qt::WaitCursor cursor in the QTextEdit viewport:
self.status.setCursor(Qt.WaitCursor)
self.status.viewport().setCursor(Qt.WaitCursor)

Qt Multiple GraphicsEffects

Is there a way in Qt5 to apply a QGraphicsEffect to a widget, even when one of its parent widgets already has a QGraphicsEffect applied to it?
When i try the following:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class ParentWidget(QWidget):
def __init__(self,parent):
super().__init__(parent)
effect = QGraphicsBlurEffect(self)
self.setGraphicsEffect(effect)
effect.setBlurRadius(0)
class ChildWidget(ParentWidget):
def __init__(self,parent):
super().__init__(parent)
self.layout = QGridLayout(self)
widget = QWidget()
widget.setObjectName('reviewControlArea')
effect = QGraphicsOpacityEffect(widget)
widget.setGraphicsEffect(effect)
self.layout.addWidget(widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = QMainWindow()
cw = ChildWidget(MainWindow)
MainWindow.setCentralWidget(cw)
MainWindow.show()
sys.exit(app.exec_())
The stdout says:
QPainter::begin: A paint device can only be painted by one painter at a time.

Categories