Issue
I just started using Python PyQt5 and I am trying to make a GUI. It all works, but I have a problem with the textbox. Every time the textbox gets filled and I try to add more, it stays the same size and just adds a small scrollbar IN the textbox. What I want is for the textbox to readjust depending on the size of the text so that you can always see the text. How can I do this.
Here is my current code:
#Import Module
from PyQt5.QtGui import QFont
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
#Create Main Class
class AddToDeckWindow(QtWidgets.QMainWindow):
def __init__(self):
super(AddToDeckWindow, self).__init__()
#Set The UI
self.initUI()
#Set The GUI Position And Size
self.setGeometry(200, 200, 900, 710)
#Set The GUI Title
self.setWindowTitle("Add")
#Set The GUI Icon
self.setWindowIcon(QtGui.QIcon('give_way.png'))
#Set The Translator
_translate = QtCore.QCoreApplication.translate
def initUI(self):
widAddToDeckWindow = QtWidgets.QWidget(self)
#Create The Text Box
self.setCentralWidget(widAddToDeckWindow)
textBox = QtWidgets.QTextEdit(widAddToDeckWindow)
textBox.setGeometry(200, 200, 500, 200)
#Set The Font
font = QFont()
font.setPointSize(14)
textBox.setFont(font)
#Create A Windows
def window():
app = QtWidgets.QApplication(sys.argv)
win = AddToDeckWindow()
#Centers The Window On The Screen
qtRectangle = win.frameGeometry()
centerPoint = QtWidgets.QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
win.move(qtRectangle.topLeft())
win.show()
sys.exit(app.exec_())
window()
Solution
First of all, consider that you're not using a layout manager, and using fixed geometries like you did, is considered bad practice and also a very bad idea.
For instance, in your case, if I try to resize the window to a size smaller than the bottom right corner of the text edit, it will become partially (or completely) invisible.
I strongly advise you against this pattern (actually, almost everybody would), as if you want to show such big margins you only need to correctly use the layout properties instead.
That said, if you want to resize a textedit according to its contents, you must implement a function that constantly checks its document()
.
Then, you should choose if set the minimumHeight
, or, better the sizeHint
, which is used by the layout to limit its size if no more space is available. I strongly suggest you to use the latter.
class ResizingTextEdit(QtWidgets.QTextEdit):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.textChanged.connect(self.computeHint)
self._sizeHint = super().sizeHint()
def computeHint(self):
hint = super().sizeHint()
height = self.document().size().height()
height += self.frameWidth() * 2
self._sizeHint.setHeight(max(height, hint.height()))
self.updateGeometry()
self.adjustSize()
def sizeHint(self):
return self._sizeHint
class AddToDeckWindow(QtWidgets.QMainWindow):
# ...
def initUI(self):
widAddToDeckWindow = QtWidgets.QWidget(self)
#Create The Text Box
self.setCentralWidget(widAddToDeckWindow)
textBox = ResizingTextEdit(widAddToDeckWindow)
textBox.setMinimumSize(500, 200)
# this is no more necessary, as the layout will completely override it
# textBox.setGeometry(200, 200, 500, 200)
layout = QtWidgets.QGridLayout(widAddToDeckWindow)
layout.addWidget(textBox, 1, 1)
layout.setColumnStretch(0, 1)
layout.setColumnStretch(2, 1)
layout.setRowStretch(0, 1)
layout.setRowStretch(2, 1)
#Set The Font
font = QFont()
font.setPointSize(14)
textBox.setFont(font)
In case of the minimumHeight
, use the following:
class ResizingTextEdit(QtWidgets.QTextEdit):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.textChanged.connect(self.computeHint)
def computeHint(self):
height = self.document().size().height()
height += self.frameWidth() * 2
if height > super().sizeHint().height():
self.setMinimumHeight(height)
else:
self.setMinimumHeight(0)
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.