Add helper function for appending text to a textedit

This commit is contained in:
baldurk
2026-08-31 12:13:26 +01:00
parent 0690269cb7
commit 1aac7afdf1
4 changed files with 38 additions and 0 deletions
+11
View File
@@ -687,6 +687,17 @@ add text next to it.
)");
virtual void SetWidgetText(QWidget *widget, const rdcstr &text) = 0;
DOCUMENT(R"(Appends to the 'text' of a widget. For most widgets this will not be different from
getting the text with :meth:`GetWidgetText`, appending to the string, and setting with :meth:`SetWidgetText`
but for multi-line widgets like text edits this can give a better experience with better scrolling.
This will also scroll to and move the cursor to the end of the text.
:param QWidget widget: The widget to append text for.
:param str text: The text to append to the widget's text.
)");
virtual void AppendText(QWidget *widget, const rdcstr &text) = 0;
DOCUMENT(R"(Return the current text of a widget. See :meth:`SetWidgetText`.
:param QWidget widget: The widget to query.
+22
View File
@@ -365,6 +365,28 @@ void MiniQtHelper::SetWidgetText(QWidget *widget, const rdcstr &text)
}
}
void MiniQtHelper::AppendText(QWidget *widget, const rdcstr &text)
{
#define APPEND_TEXT(TextWidget) \
{ \
TextWidget *w = qobject_cast<TextWidget *>(widget); \
if(w) \
{ \
ScrollToBottom(widget); \
w->moveCursor(QTextCursor::End); \
w->insertPlainText(text); \
return; \
} \
}
APPEND_TEXT(RDTextEdit);
APPEND_TEXT(QTextEdit);
rdcstr t = GetWidgetText(widget);
t += text;
SetWidgetText(widget, t);
}
rdcstr MiniQtHelper::GetWidgetText(QWidget *widget)
{
if(!widget)
+1
View File
@@ -72,6 +72,7 @@ public:
// widget manipulation
void SetWidgetText(QWidget *widget, const rdcstr &text) override;
void AppendText(QWidget *widget, const rdcstr &text) override;
rdcstr GetWidgetText(QWidget *widget) override;
void ScrollToTop(QWidget *widget) override;
@@ -177,6 +177,10 @@ struct MiniQtInvoker : UIThreadInvoker<IMiniQtHelper>
{
InvokeVoidFunction(&IMiniQtHelper::SetWidgetText, widget, text);
}
void AppendText(QWidget *widget, const rdcstr &text)
{
InvokeVoidFunction(&IMiniQtHelper::AppendText, widget, text);
}
rdcstr GetWidgetText(QWidget *widget)
{
return InvokeRetFunction<rdcstr>(&IMiniQtHelper::GetWidgetText, widget);