I am very new to Qt/PySide6 and am trying to create an app that edits text in many little boxes. To give some idea of how many boxes, I think it should easily be able to handle 10,000 boxes.
Here’s what I tried initially:
import sys
from PySide6.QtCore import QSize, Qt
from PySide6.QtWidgets import (
QApplication,
QWidget,
QMainWindow,
QPlainTextEdit,
QVBoxLayout,
QScrollArea,
)
DATA = ["hellonn"]*10000
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.scroll = QScrollArea()
layout = QVBoxLayout()
self.textArray = []
for v in DATA:
self.textArray.append(QPlainTextEdit(v))
for text in self.textArray:
layout.addWidget(text)
widget = QWidget()
widget.setLayout(layout)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(widget)
self.setCentralWidget(self.scroll)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Unfortunately, this is way too slow (it takes maybe 30 seconds for the app window to show up when I run it and my laptop fan starts spinning…).
But now consider the following:
import sys
from PySide6.QtCore import QSize, Qt
from PySide6.QtWidgets import (
QApplication,
QWidget,
QMainWindow,
QPlainTextEdit,
QVBoxLayout,
QScrollArea,
)
DATA = ["hellonn"]*10000
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.scroll = QScrollArea()
layout = QVBoxLayout()
self.textfield = QPlainTextEdit("".join(v for v in DATA))
layout.addWidget(self.textfield)
widget = QWidget()
widget.setLayout(layout)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(widget)
self.setCentralWidget(self.scroll)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
This is the same amount of data, but it’s just all stored in a single QPlainTextEdit
widget, and it runs very fast. So this makes me wonder whether it is possible to somehow create (for the user) an illusion that there are 10,000 separate boxes, even though on the Qt/internal level there’s just a single QPlainTextEdit
(or some other) widget tracking the state. However, I don’t know enough about Qt to know whether such a thing is possible and how to go about doing it if it is. Alternatively, maybe there is some other approach that would work better.
I am fine with each box not having its own edit history/undo sequence, but there should be some way to programmatically “know” the contents of each box separately, to know which box is currently being edited, and operations like Ctrl-a to select all text should only select the text inside the current box.