Gtk3 rich text widget machinery (based upon GtkTextBuffer & GtkTextView) has both "begin-user-action" and "end-user-actions" signals, which permits quickly to process user input conveniently (and distinguish it from application generated changes to the buffer or the view).
But it looks like there is no similar stuff in Qt5. For example, my incomplete understanding is that QTextEdit::insertHtml or QTextDocument::contentsChange or QTextDocument::contentsChanged does not separate changes related to user input (either keyboard, or pasting, etc...) from those done by the application.
What I have in mind is some syntax oriented editor.
I'm probably misunderstanding Qt5 rich text editor support.
(For curious people: I am redesigning & reimplementing my MELT monitor with C & GTK into something with C++11 & Qt5 tentatively called Basixmo; all is GPL free software, but I have not coded yet the Qt5 thing)
example
I have a window with a button say and a QTextEdit as its central widget. When I click the button, a "hello" string is inserted in the document, and I call that an application change (you could imagine that the button is replaced by something unrelated to the user, e.g. some network input). When I type some keypresses into the text-editor, some string is also inserted from that user-action change. I want to distinguish both.
#include <QApplication>
#include <QMainWindow>
#include <QTextEdit>
#include <QToolBar>
#include <fstream>
#include <iostream>
int main(int argc, char**argv)
{
QApplication app(argc, argv);
auto win = new QMainWindow;
auto tb = win->addToolBar("Basile example");
auto ted = new QTextEdit;
win->setCentralWidget(ted);
tb->addAction("say",[=]{ted->insertPlainText("hello");});
auto doc = ted->document();
QObject::connect(doc,&QTextDocument::contentsChange,
[=](int pos, int removedchars, int addedchars)
{ std::clog << "contentChange: pos=" << pos
<< " removedchars=" << removedchars
<< " addedchars=" << addedchars
<< std::endl; });
win->show();
app.exec();
delete win;
}
//// Local Variables:
//// compile-command: "g++ -std=c++11 -Wall -g $(pkg-config --cflags Qt5Core Qt5Widgets Qt5Gui) -fPIC $(pkg-config --libs Qt5Core Qt5Widgets Qt5Gui) -o eqb eqb.cc"
//// End:
But when I run the above code, the contentsChange signal (connected to my lambda function outputting to std::clog) is triggered for both user-action changes and application changes.
I don't care to work at the QTextEdit, or QTextDocument or QTextCursor level, but I want to separate user-action changes (typing into the QTextEdit widget, or pasting with a mouse click & menu, etc...) from application changes. I would like to avoid working at widget events level (e.g. redefine a virtual member function for QWidget::keyPressEvent in my own class, and so on) in particular because I am not sure to know all the possible events affecting a QTextEdit instance.
BTW, a too broad generalization of my question might be : how to design & code an editor as scriptable as emacs is using genuine Qt5 coding style and widgets in C++11 (of course by embedding some scriptable interpreter à la Guile).
PS. If that matters, my desktop system runs Debian/testing/x86-64. Qt is version 5.6.1, my code is compiled by GCC 6.2; the compilation command is in the last wide comment.