Code: Select all
// Render checkbox
if (blockText == "- [ ]") {
cursor.beginEditBlock();
cursor.removeSelectedText();
QTextCharFormat fmt;
fmt.setObjectType(QTextFormat::UserObject + 1);
fmt.setProperty(1000, "checkbox");
fmt.setProperty(1001, true);
fmt.setFontPointSize(FONT_SIZE);
cursor.insertText(QString(QChar::ObjectReplacementCharacter), fmt);
cursor.endEditBlock();
return;
}
void MarkdownEditor::mousePressEvent(QMouseEvent *event)
{
QTextCursor cursor = cursorForPosition(event->pos());
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1); // Select one character
QTextCharFormat fmt = cursor.charFormat();
if (fmt.objectType() == QTextFormat::UserObject + 1 &&
fmt.property(1000).toString() == "checkbox") {
//Toggle the checkbox state
bool checked = fmt.property(1001).toBool();
fmt.setProperty(1001, !checked);
cursor.setCharFormat(fmt); //
Dies ist auch der Kontrollkästchen -Code (CustomObjectrenderer) < /p>
class CustomObjectRenderer : public QObject, public QTextObjectInterface {
Q_OBJECT
Q_INTERFACES(QTextObjectInterface)
public:
explicit CustomObjectRenderer(QObject *parent = nullptr) : QObject(parent) {}
QSizeF intrinsicSize(QTextDocument *, int, const QTextFormat &) override {
return QSizeF(20, 20); // Size of the checkbox
}
void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *, int, const QTextFormat &format) override
{
if (format.property(1000).toString() != "checkbox") return;
bool checked = format.property(1001).toBool();
painter->save();
const qreal radius = 2.0;
const QColor backgroundColor = QColor("#111111");
const QColor checkColor = QColor("#ffffff");
painter->setRenderHint(QPainter::Antialiasing);
// Define size of checkbox (same as in intrinsicSize)
const QSizeF boxSize = QSizeF(20, 20);
// Calculate centered rect within `rect`
QRectF boxRect(
rect.left(),
rect.top() + (rect.height() - boxSize.height()) / 2.0 + 2, // vertically center it
boxSize.width(),
boxSize.height()
);
painter->setBrush(backgroundColor);
painter->setPen(Qt::NoPen); // or a border if desired
painter->drawRoundedRect(boxRect, radius, radius);
if (checked) {
QPen pen(checkColor, 2);
painter->setPen(pen);
QPointF p1(boxRect.left() + boxRect.width() * 0.25, boxRect.top() + boxRect.height() * 0.55);
QPointF p2(boxRect.center().x(), boxRect.bottom() - boxRect.height() * 0.3);
QPointF p3(boxRect.right() - boxRect.width() * 0.2, boxRect.top() + boxRect.height() * 0.3);
painter->drawLine(p1, p2);
painter->drawLine(p2, p3);
}
painter->restore();
}
};
< /code>
Irgendwelche Hinweise darauf, wie ich die zugrunde liegende Grafik -Rendering -Architektur von QT -Apps verstehen kann? Ich verlasse mich dafür stark auf LLMs, aber alle Zeiger, um zu verstehen, wie dies auf einer tieferen Ebene funktioniert, damit ich beheben kann>