pbek / qmarkdowntextedit

A C++ Qt QPlainTextEdit widget with markdown highlighting support and a lot of other extras
http://www.qownnotes.org
MIT License
414 stars 88 forks source link

is it possible to use this plugin in a qml script? #158

Closed tubbadu closed 1 year ago

pbek commented 2 years ago

If we are talking about the QMarkdownTextEdit widget, I don't think you can use Qt widgets in QML, but I can be wrong...

tim-gromeyer commented 2 years ago

Acording to this Website it would be possible to create a fork and port it to QML. Qt Version 6.2 or abowe required.

pbek commented 2 years ago

Hm, but those are no QWidgets, right?

tim-gromeyer commented 2 years ago

We can only try. I'll see what can be done in a few hours.

pbek commented 2 years ago

Good luck, have fun. 😁

tim-gromeyer commented 2 years ago

I have created a new repo (I already have a fork). The code works, but I can't figure out how to display in QML a TextEdit object or in this case QMarkdownTextEdit object. Please help. The files here: example.cpp example.qml I have almost no QML experience, because there are far too few examples and tutorials available.

pbek commented 2 years ago

Nice 😁

nuttyartist commented 1 year ago

@tim-gromeyer, did you manage to make this work?? I'm eagerly looking for something like this.

tim-gromeyer commented 1 year ago

No, not really. It was just a black window, but I can give it another try

nuttyartist commented 1 year ago

No, not really. It was just a black window, but I can give it another try

That would be great. I'm looking to convert our app https://github.com/nuttyartist/notes over to QML and porting this repo would be one of the main things to achieve before continuing.

pbek commented 1 year ago

Porting qmarkdowntextedit (which is a widget) to QML? Please keep me posted. 😀

tim-gromeyer commented 1 year ago

@nuttyartist Which Qt version(s) should it be compatible with? I'm planning to use the qt_add_qml_module - CMake command which was introduced with Qt 6.2

nuttyartist commented 1 year ago

We want to support Qt5 as well. But if it's just the imports that shouldn't be a problem? We can easily add the .qml files into a resource file and load them that way, no?

tim-gromeyer commented 1 year ago

I don't know if the imports are going to make problems yet, I have no experience with QML. The problem I'm facing is, that qt-add-qml-module is needed for building the QML module from the c++ source files.

nuttyartist commented 1 year ago

Well, any success in this would be appreciated! We can always improve thereafter.

tim-gromeyer commented 1 year ago

Okay, here some updates:

  1. We can support Qt 6.0 - Qt 6.2 by using an alternative to qt-add-qml-module
  2. For Qt5 support, we need to use qmake

I got it working, at least partially: I managed to compile it and display some text (highlighted).

HOWEVER:

If you wanna test it, use this tarball (I hope this is allowed (License)). I'M NOT DONE YET and will create a GitHub repo if I'm.

nuttyartist commented 1 year ago

I'm getting an error during cmake:

CMake Error at /usr/local/lib/cmake/Qt6Qml/Qt6QmlMacros.cmake:126 (message):
  Called without a module version.  Please specify one using the VERSION
  argument.
Call Stack (most recent call first):
  CMakeLists.txt:64 (qt6_add_qml_module)

-- Configuring incomplete, errors occurred!

Although I see that module version is set:

qt_add_qml_module(ExampleProject
    URI ExampleProjectApp
    VERSION 1.0
    QML_FILES example.qml
)
nuttyartist commented 1 year ago

Nevermind, I added VERSION 1.0 to line 64, but the app seems to crash:

QQmlApplicationEngine failed to load component
qrc:/ExampleProjectApp/example.qml:15:5: MarkdownTextEdit is not a type
tim-gromeyer commented 1 year ago

Hmm... it works for me. Which Qt version are you using? I'm using my system's Qt which is version 6.5.1

tim-gromeyer commented 1 year ago

For the simplicity I created a new branch on my fork. I made some changes which might/should fix this issue. Here the link: https://github.com/tim-gromeyer/qmarkdowntextedit/tree/qml (later, I'll squash the commits and rename the branch)

nuttyartist commented 1 year ago

I'm using Qt 6.5.0. Using the mentioned repo I get this error:

Undefined symbols for architecture x86_64:
  "qt_static_plugin_QtQmlPlugin()", referenced from:
      StaticQtQmlPluginPluginInstance::StaticQtQmlPluginPluginInstance() in qmlplugin_init.cpp.o
ld: symbol(s) not found for architecture x86_64

Am I missing something?

tim-gromeyer commented 1 year ago

Nope. I created a github actions workflow and there it fails too

tim-gromeyer commented 1 year ago

Oh, great, the build finally works! @nuttyartist would you test it again and See if the "MarkdownTextEdit is not installed" error is fixed?

nuttyartist commented 1 year ago

Yay! it works! Do you think you can make editing work as well?

nuttyartist commented 1 year ago

BTW, I'm getting the following runtime errors if helpful:

qt.svg: Cannot open file ':/media/go-top.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/go-top.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/window-close.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/window-close.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/edit-find-replace.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/edit-find-replace.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/go-bottom.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/go-bottom.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/format-text-superscript.svg', because: No such file or directory
qt.svg: Cannot open file ':/media/format-text-superscript.svg', because: No such file or directory
setText
paint
QObject::setParent: Cannot set parent, new parent is in a different thread
tim-gromeyer commented 1 year ago

Yay! it works! Do you think you can make editing work as well?

Hmm... I don't know. It looks like it doesn't receive mouseMove - events which is essential. If this works I can take a look at editing...

nuttyartist commented 1 year ago

Oh okay, I hope you succeed! We won't be able to incorporate this into our app without editing.

tim-gromeyer commented 1 year ago

Oh okay, I hope you succeed! We won't be able to incorporate this into our app without editing.

Yeah, an editor which doesn't support editing would indeed be a problem 😅️

I figured out, that the mouseMoveEvent only gets called when I press a button on the mouse, e.g. right click.

I'm gonna try to map the hoverMoveEvent to QMarkdoenTextEdit's mouseMoveEvent

nuttyartist commented 1 year ago

Haha yes. Okay, crossing fingers.

tim-gromeyer commented 1 year ago

@nuttyartist, I have an idea. We could just port the syntax highlighter from QMarkdownTextEdit to QML and apply it to a standard TextEdit, would this be a solution? Or do you need some special functions from QMarkdowntextEdit

nuttyartist commented 1 year ago

I think that should work as well. How will it be different than QMarkdowntextEdit?

Not quite related question: Qt Quick already supports Markdown rendering but not in a live editor (as a user types) and no syntex highlighting, will it be hard to add live markdown rendering to it?

tim-gromeyer commented 1 year ago

I think that should work as well. How will it be different than QMarkdowntextEdit?

I'm not sure but ai think you will lose features like the search widget, line number area, brackets auto closing, etc.

Not quite related question: Qt Quick already supports Markdown rendering but not in a live editor (as a user types) and no syntex highlighting, will it be hard to add live markdown rendering to it?

AFAIK it support Markdown text, which it renders using md4c. For live markdown rendering you might take a Look here. As I already said, I have ni experience with QML and I can only guess how hard it'll be

nuttyartist commented 1 year ago

I'm not sure but ai think you will lose features like the search widget, line number area, brackets auto closing, etc.

Sounds like things our app isn't using already so seems alright.

For live markdown rendering you might take a Look here.

But it isn't live inside the same editor like QMarkdownTextEdit is.

As I already said, I have ni experience with QML and I can only guess how hard it'll be

Well I still think porting the syntax highlighter from QMarkdownTextEdit to QML will be valuable.

tim-gromeyer commented 1 year ago

@nuttyartist I made a nee commit, it should work now. Here a demo video:

Bildschirmaufzeichnung vom 2023-07-04, 16-21-00.webm

nuttyartist commented 1 year ago

Not sure why I'm getting:

CMake Error at CMakeLists.txt:67 (qt_add_executable):
  Unknown CMake command "qt_add_executable".

EDIT: I was running on Qt 5... testing now again.

nuttyartist commented 1 year ago

Woohoo! Amazing work, @tim-gromeyer! You made my day.

Is that using the second approach, using just the syntax highlighter from QMarkdownTextEdit?

tim-gromeyer commented 1 year ago

Woohoo! Amazing work, @tim-gromeyer! You made my day.

Thank you! 😁️

Is that using the second approach, using just the syntax highlighter from QMarkdownTextEdit?

Yeah, it is! Works like a charm.

tim-gromeyer commented 1 year ago

@nuttyartist I got it to work with Qt 5.12! And I might upstream these changes here.

@pbek What do you think about the idea (upstreaming)? I'll squash the commits before creating the pull request.

nuttyartist commented 1 year ago

Awesome!

pbek commented 1 year ago

@pbek What do you think about the idea (upstreaming)? I'll squash the commits before creating the pull request.

Sure, that would be great! As long as QML / QtQuick as dependency is purely optional...

We are talking about https://github.com/pbek/qmarkdowntextedit/compare/develop...tim-gromeyer:qmarkdowntextedit:qml, right?

tim-gromeyer commented 1 year ago

Sure, that would be great! As long as QML / QtQuick as dependency is purely optional...

I made it so that if QtQuick was not found it just doesn't build it.

We are talking about develop...tim-gromeyer:qmarkdowntextedit:qml, right?

Yes, exactly. As you can see there QtQuick is completely optional and I tested it with Qt 5.12 - 6.6