Qt Creator plugin - how to modify current editor margins
-
Hi,
I've created ZenMode plugin. It hides sidebars, menubar and Modes View and make Creator fullscreen.
By default current editor align text/code to left side.
What I need is ... to make code horizontally centered. Let's make left margin different for active ZenMode.
I tried with// Get the current editor Core::IEditor *currentEditor = Core::EditorManager::currentEditor(); if (currentEditor) { // Cast to text editor TextEditor::BaseTextEditor *textEditor = qobject_cast<TextEditor::BaseTextEditor*>(currentEditor); if (textEditor) { // Access the widget QPlainTextEdit *editorWidget = qobject_cast<QPlainTextEdit*>(textEditor->editorWidget()); if (editorWidget) { // Apply centered margins int margin = 200; // Or calculate dynamically editorWidget->setViewportMargins(margin, 0, margin, 0); } } }but linking fails on
qobject_cast<TextEditor::BaseTextEditor*>(currentEditor);.
What other solution are available?PS: Second problem is that
setViewportMarginsis protected method and requries some additional hacks to be called. -
@SebastianM Does your plugin depend on/ link to the
TextEditorplugin? -
Yes, you right.
I addedQtCreator::TextEditorin CMakeLists.txt toPLUGIN_DEPENDSsection inadd_qtc_pluginand linking problem is gone. -
Still, I can't find way to modify TextEditor margins.
Core::IEditor *currentEditor = Core::EditorManager::currentEditor(); TextEditor::BaseTextEditor *textEditor = qobject_cast<TextEditor::BaseTextEditor*>(currentEditor); TextEditor::TextEditorWidget * editorWidget = textEditor->editorWidget(); //how to do it?Tried casting
edditorWidgettoclass CustomTextEditorWidget : public TextEditor::TextEditorWidgetwith customvirtual int extraAreaWidth(int *markWidthPtr = nullptr) const override. qobject_cast fails, after static_cast there's exception.Accessor approach
class TextEditorWidgetAccessor : public TextEditor::TextEditorWidget { public: static void setMargins(TextEditor::TextEditorWidget *widget, int left, int top, int right, int bottom) { // Cast to accessor to get access to protected method TextEditorWidgetAccessor *accessor = static_cast<TextEditorWidgetAccessor*>(widget); accessor->setViewportMargins(left, top, right, bottom); } }; .... TextEditorWidgetAccessor::setMargins(editorWidget, 300, 0, 0, 0);doesn't change viewportMargins - before and after setting they return the same original value.