Skip to content

Commit

Permalink
Add QML and QML Example (#187)
Browse files Browse the repository at this point in the history
Make qmarkdowntextedit static...

Update build.yml

Update build.yml

Link to  Qml

`qmlplugin`: Link to Qml

Try to fix build

MAKE IT WORK!!!!!

Remove old file

Revert workflow changes

Remove old file, sync qmarkdowntextedit.cpp/h with upstream

Dont't force static

Add missing `highlightingEnabled` - function

Move qml stuff into seperate folder, make Quick dependency optional

Add missing file

Don't force Qt6, might work with Qt5

Made it work with Qt 5.12

Fix build if Qt Quick is not installed

Remove unneeded include

Remove old code

Add missing `highlightingEnabled` - function

Co-authored-by: Tim Gromeyer <tim.gromeyer@gmail.com>
  • Loading branch information
tim-gromeyer and tim-gromeyer committed Jul 7, 2023
1 parent 39da927 commit bb3bcd3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16) # Qt requires CMake 3.16
cmake_minimum_required(VERSION 3.16) # Qt requires CMake 3.16
project(qmarkdowntextedit LANGUAGES CXX VERSION 1.0.0)

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Expand All @@ -16,6 +16,7 @@ option(QMARKDOWNTEXTEDIT_EXE "Build test executable" ON)
# find qt
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} OPTIONAL_COMPONENTS Quick)

# needed for windows
if(WIN32)
Expand Down Expand Up @@ -60,6 +61,13 @@ target_link_libraries(qmarkdowntextedit PUBLIC
${INTL_LDFLAGS}
)

if (Qt${QT_VERSION_MAJOR}Quick_FOUND)
target_link_libraries(qmarkdowntextedit PUBLIC Qt${QT_VERSION_MAJOR}::Quick)

add_executable(QtQuickExample examples/qml/example.cpp examples/qml/ressources.qrc)
target_link_libraries(QtQuickExample PRIVATE Qt${QT_VERSION_MAJOR}::Quick qmarkdowntextedit)
endif()

# QMarkdownTextEdit executable
if(QMARKDOWNTEXTEDIT_EXE)
set(SOURCE_FILES
Expand Down
24 changes: 24 additions & 0 deletions examples/qml/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <QApplication>
#include <QQmlApplicationEngine>

#include "markdownhighlighter.h"

int main(int argc, char *argv[]) {
QApplication app(argc, argv);

qmlRegisterType<MarkdownHighlighter>("MarkdownHighlighter", 1, 0,
"MarkdownHighlighter");

QQmlApplicationEngine engine;

const QUrl url(QStringLiteral("qrc:/example.qml"));
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &app,
[url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl) QCoreApplication::exit(-1);
},
Qt::QueuedConnection);
engine.load(url);

return app.exec();
}
22 changes: 22 additions & 0 deletions examples/qml/example.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import QtQuick 2.0
import QtQuick.Window 2.0
import MarkdownHighlighter 1.0

Window {
id: mainwindow
width: 640
height: 400
visible: true
title: qsTr("QtQuick Project")

TextEdit {
id: editor
text: "# Hello world!"
focus: true
}

MarkdownHighlighter {
id: syntaxHighlighter
textDocument: editor.textDocument
}
}
5 changes: 5 additions & 0 deletions examples/qml/ressources.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>example.qml</file>
</qresource>
</RCC>
23 changes: 23 additions & 0 deletions markdownhighlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include <QSyntaxHighlighter>
#include <QTextCharFormat>

#ifdef QT_QUICK_LIB
#include <QQuickTextDocument>
#endif

QT_BEGIN_NAMESPACE
class QTextDocument;

Expand All @@ -27,6 +31,25 @@ QT_END_NAMESPACE
class MarkdownHighlighter : public QSyntaxHighlighter {
Q_OBJECT

#ifdef QT_QUICK_LIB
Q_PROPERTY(QQuickTextDocument *textDocument READ textDocument WRITE
setTextDocument NOTIFY textDocumentChanged)

QQuickTextDocument *m_quickDocument = nullptr;

signals:
void textDocumentChanged();

public:
inline QQuickTextDocument *textDocument() const { return m_quickDocument; };
void setTextDocument(QQuickTextDocument *textDocument) {
if (!textDocument) return;
m_quickDocument = textDocument;
setDocument(m_quickDocument->textDocument());
Q_EMIT textDocumentChanged();
};
#endif

public:
enum HighlightingOption {
None = 0,
Expand Down

0 comments on commit bb3bcd3

Please sign in to comment.