Important: Please read the Qt Code of Conduct -

Qt Creator Plugin: How to edit opened files

  • Greetings!

    I started a simple plugin for Qt Creator which adds/modifies specific "lines" within files opened by Creator. I was able to list files using the ProjectExplorerPlugin but I resorted to using QFile to open and edit these files. Though the code is working, a dialog box pop-ups which says that "The file has changed outside Qt Creator. Do you want to reload it?"

    Is there a better and cleaner way to edit opened files in Qt Creator? Can you please show example codes?

    Thanks and more powers!

  • Moderators

    QFile opens files on the hard drive. If you modify that, Creator detects that the file has changed on disk and pops up a window asking what to do: Reload the file from disk, ignore the changes on disk, etc.

    QFile obviously does not get you access to the piece of memory that holds the contents of that opened file in Creator which is displayed in the editors. Use the editormanager to get to the editors.

    Code is readily available in Qt Creator, we do that all over the place. Just search for usages of the specific methods of the editor manager you need examples for. To open the editor manager class without knowing the file name: Type Ctrl-K and then "c EditorManager" to jump straight to that class.

  • @Tobias
    I am able to open the "file" using Core::EditorManager and Core::IEditor

    The problem is how do i read and write into the file, specifically, "line by line". Unfortunately, Ctrl-K doesn't direct me to EditorManager class so i used this "link": as a reference.

    My target is reading and writing line by line and append some "code" if the keyword I am looking for doesn't exist. If the keyword exists, delete or modify it. Sorry but there is very less entry/guide in the documentation. So I would be grateful for some help.


Log in to reply