Below is a small working code to diplay what I am trying to achieve.
import sys
import os
import sqlite3
from PyQt5.QtSql import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
# createDB()
# con_1 = sqlite3.connect("temp.db")
# # create a table
# con_1.execute('''create table t1(
# id INTEGER PRIMARY KEY, 'First Name' TEXT, 'Last Name' TEXT, 'Full Name' TEXT);''')
# # populate the table
# con_1.execute("insert into t1 ('First Name', 'Last Name', 'Full Name') values('Wade', 'Wilson', 'Wade Wilson');")
# con_1.execute("insert into t1 ('First Name', 'Last Name', 'Full Name') values('Norman', 'Osborn', 'Norman Osborn');")
# con_1.execute("insert into t1 ('First Name', 'Last Name', 'Full Name') values('Stephen', 'Strange', 'Stephen Strange');")
# con_1.commit()
# con_1.close()
class MultilineTextEditor(QStyledItemDelegate):
def createEditor(self, parent, options, index):
return QTextEdit(parent)
def setEditorData(self, editor, index):
editor.setText(index.data())
def setModelData(self, editor, model, index):
model.setData(index, editor.toPlainText())
def paint(self, painter, option, index):
value = index.data(Qt.DisplayRole)
if option.state & QStyle.State_Selected:
text1 = index.data()
doc = QTextDocument(text1)
cursor = QTextCursor(doc)
cursor.selectedTableCells()
# cursor.selectedText() <-- How to impliment this?
# Trying out to simply select the first line!
cursor.movePosition(QTextCursor.Start)
cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
format = QTextCharFormat()
format.setFontWeight(QFont.Bold)
format.setFontPointSize(20)
weight = QFont.DemiBold
format.setFontWeight(weight)
cursor.mergeCharFormat(format)
print(cursor.selectedText())
# painter commands??
QStyledItemDelegate.paint(self, painter, option, index)
else:
QStyledItemDelegate.paint(self, painter, option, index)
class TableViewer(QMainWindow):
def __init__(self):
super().__init__()
working_dir = os.getcwd()
db_path = f"{working_dir}\temp.db"
print(db_path)
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName(db_path)
self.db.open()
self.model = QSqlTableModel(self, self.db)
self.model.setTable('t1')
self.model.select()
self.tableView = QTableView(self)
self.tableView.setModel(self.model)
self.tableView.resizeColumnsToContents()
layout = QVBoxLayout()
layout.addWidget(self.tableView)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.tableView.setItemDelegateForColumn(2, MultilineTextEditor())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
tv = TableViewer()
sys.exit(app.exec_())
- A SQL Lite Database (temp.db) is created in the commented out sections
- This db is read and displayed as in Qtableview
- The column containing full names is editable
How would you set the format (bold, Italics, underline), size or colour of selcted string? I would eventually bind the selection with say keyboard shorcuts (eg cntr + B for bold) or display a toolbar for user selection. Found an example here: https://forum.qt.io/topic/138318/how-do-you-bold-and-un-bold-a-selected-text-pyqt-or-pyside. But Im unable to implement it, even rudementarily in Qtableview cell.
The rich text data should be saved back to be retrieved from the DB. Unfortunately I am unable to find any good explainitions on Delgate –> paint.