How to implement the clone formatting functionality of a word processor?
-
Hi,
How do you implement this feature?
I have tried to do it this way:- The easy part: Creating a clone action to copy the format of a piece of text and storing the format in global variable to be used later.
- This is the part that I haven't been able to do: selecting another piece of text and after releasing the left mouse button, the format is applied.
I'd appreciate it if you could help me to do part 2.
-
C Christian Ehrlicher referenced this topic on
-
@JonB .
Thank you. I overrode mouseReleaseEvent like this:
void cloneFormatting(); //cloneFormatting copies of the selected text format into clonedFormat to be applied in applyCLonedFormatting.
void applyClonedFormatting();
void SdiWindow::mouseReleaseEvent(QMouseEvent *event)
{
QDebug() << "mouse released";
if((event->buttons() & Qt::LeftButton) && isCloneFormatting)
docWidget->textCursor().setCharFormat(clonedFormat);
}
Somehow, mouseReleaseEvent is never called.
@Christian-Ehrlicher . No. It's not me. -
@JonB .
Thank you. I overrode mouseReleaseEvent like this:
void cloneFormatting(); //cloneFormatting copies of the selected text format into clonedFormat to be applied in applyCLonedFormatting.
void applyClonedFormatting();
void SdiWindow::mouseReleaseEvent(QMouseEvent *event)
{
QDebug() << "mouse released";
if((event->buttons() & Qt::LeftButton) && isCloneFormatting)
docWidget->textCursor().setCharFormat(clonedFormat);
}
Somehow, mouseReleaseEvent is never called.
@Christian-Ehrlicher . No. It's not me.@ntos
Please appendoverride
to your method declaration (in the.h
file) to make sure you have defined it correctly.
Your code won't print anything as it stands --- in fact it should give you a compilation error --- so if you want help you need to paste actual code, not just type something in here.
The overriding does work so I don't know what you are doing. -
@JonB . I am on chapter 4 of foundations of Qt development about mdi. I don't know if there is a chapter on mouse event. I have tried, with the help of gpt, to implement the clone formatting as seen in libreoffice, but the sdi window doesn't detect mouse event because the QTextEdit is set as the main widget. Here is the code if you are interested:
Please see the edited post tomorrow perhaps. I need 3600 seconds to be allowed to edit my post. -
@JonB . I can post the complete code. The forum says it is spam.
void SdiWindow::cloneFormatting()
{QTextCursor cursor = docWidget->textCursor(); clonedFormat = cursor.charFormat(); isCloneFormatting = true; qDebug() << "Cloned format - Bold:" << (clonedFormat.fontWeight() == QFont::Bold); statusBar()->showMessage(tr("Select text or click to apply formatting"));
}
void SdiWindow::applyClonedFormatting()
{
qDebug() << "Applying cloned formatting";
QTextCursor cursor = docWidget->textCursor();
if (cursor.hasSelection()) {
qDebug() << "Applying to selection";
cursor.mergeCharFormat(clonedFormat);
} else {
qDebug() << "Applying to cursor position";
docWidget->setCurrentCharFormat(clonedFormat);
}
isCloneFormatting = false;
docWidget->setFocus(); // Ensure the text edit has focus
statusBar()->clearMessage();
qDebug() << "Format applied - Bold:" << (cursor.charFormat().fontWeight() == QFont::Bold);
}// void SdiWindow::mouseReleaseEvent(QMouseEvent *event)
// {
// if ((event->button() == Qt::LeftButton) && isCloneFormatting )
// {
// qDebug() << "Left mouse button released.";
// applyClonedFormatting();
// }
// // QWidget::mouseReleaseEvent(event);
// } -
@JonB . I can post the complete code. The forum says it is spam.
void SdiWindow::cloneFormatting()
{QTextCursor cursor = docWidget->textCursor(); clonedFormat = cursor.charFormat(); isCloneFormatting = true; qDebug() << "Cloned format - Bold:" << (clonedFormat.fontWeight() == QFont::Bold); statusBar()->showMessage(tr("Select text or click to apply formatting"));
}
void SdiWindow::applyClonedFormatting()
{
qDebug() << "Applying cloned formatting";
QTextCursor cursor = docWidget->textCursor();
if (cursor.hasSelection()) {
qDebug() << "Applying to selection";
cursor.mergeCharFormat(clonedFormat);
} else {
qDebug() << "Applying to cursor position";
docWidget->setCurrentCharFormat(clonedFormat);
}
isCloneFormatting = false;
docWidget->setFocus(); // Ensure the text edit has focus
statusBar()->clearMessage();
qDebug() << "Format applied - Bold:" << (cursor.charFormat().fontWeight() == QFont::Bold);
}// void SdiWindow::mouseReleaseEvent(QMouseEvent *event)
// {
// if ((event->button() == Qt::LeftButton) && isCloneFormatting )
// {
// qDebug() << "Left mouse button released.";
// applyClonedFormatting();
// }
// // QWidget::mouseReleaseEvent(event);
// } -
@JonB . I cannot edit my original post. I had to post the code in quick reply, which doesn't have that code tag. But as you can see the mouseReleaseEvent function that is commented out. It never got called because of this code:
docWidget = new QTextEdit( this );
setCentralWidget( docWidget );So applyClonedFormatting never got called .
I even installed an eventfilter in docWidget to intercept mouse events but it never worked.
If only this forum allowed uploading files or editting posts at any time, it would be much easier. -
@JonB . I cannot edit my original post. I had to post the code in quick reply, which doesn't have that code tag. But as you can see the mouseReleaseEvent function that is commented out. It never got called because of this code:
docWidget = new QTextEdit( this );
setCentralWidget( docWidget );So applyClonedFormatting never got called .
I even installed an eventfilter in docWidget to intercept mouse events but it never worked.
If only this forum allowed uploading files or editting posts at any time, it would be much easier.@ntos
Your code is quite incomplete. You comment out the code you would like to work? You define aSdiWindow::mouseReleaseEvent()
but no definition ofSdiWindow
not where it is used given to us? You have someQMainWindow
, in order to gosetCentralWidget()
, but we don't see it? You say setting a central widget of aQTextEdit
stops what working? (Presumably you realize any widget will gobble mouse events on it?) If you are going to want a format to be applied in aQTextEdit
when you drag-select some text and release the mouse, maybe you need themouseReleaseEvent()
of the text edit? Or, if this is a "selection", what about QTextEdit::selectionChanged() signal?Maybe someone else can tell from the code you provide.
-
Ok. Let me try again. I am still new around here and haven't known the ropes yet. Why is this forum so prohibitive. It doesn't allow you to post long code, saying content is a spam by Askismet.com.
Thank you for your perseverance. I am not allowed to post code. -
Ok. Let me try again. I am still new around here and haven't known the ropes yet. Why is this forum so prohibitive. It doesn't allow you to post long code, saying content is a spam by Askismet.com.
Thank you for your perseverance. I am not allowed to post code.