Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Qt Creator plugin - how to modify current editor margins
Forum Updated to NodeBB v4.3 + New Features

Qt Creator plugin - how to modify current editor margins

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
pluginc++qt creator
4 Posts 2 Posters 269 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SebastianMS Offline
    SebastianMS Offline
    SebastianM
    wrote last edited by SebastianM
    #1

    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 setViewportMargins is protected method and requries some additional hacks to be called.

    1 Reply Last reply
    0
    • aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote last edited by aha_1980
      #2

      @SebastianM Does your plugin depend on/ link to the TextEditor plugin?

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • SebastianMS Offline
        SebastianMS Offline
        SebastianM
        wrote last edited by
        #3

        Yes, you right.
        I added QtCreator::TextEditor in CMakeLists.txt to PLUGIN_DEPENDS section in add_qtc_plugin and linking problem is gone.

        1 Reply Last reply
        0
        • SebastianMS Offline
          SebastianMS Offline
          SebastianM
          wrote last edited by
          #4

          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 edditorWidget to class CustomTextEditorWidget : public TextEditor::TextEditorWidget with custom virtual 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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved