From 1704d392e866a532320a2ae536c02a94408545db Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Sun, 4 Feb 2024 15:03:50 -0600 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=9Ccustom-widgets=E2=80=9D=20exampl?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This small example demonstrates defining and binding to your own subclass of a QT widget. --- examples/custom-widgets/README.md | 13 +++++ examples/custom-widgets/goodbye-widgets.h | 8 +++ examples/custom-widgets/goodbye-widgets.scm | 43 +++++++++++++++ examples/custom-widgets/goodbye.egg | 13 +++++ examples/custom-widgets/goodbye.scm | 12 ++++ examples/custom-widgets/goodbye.ui | 61 +++++++++++++++++++++ examples/custom-widgets/main.cpp | 29 ++++++++++ examples/custom-widgets/prototypes.h | 4 ++ 8 files changed, 183 insertions(+) create mode 100644 examples/custom-widgets/README.md create mode 100644 examples/custom-widgets/goodbye-widgets.h create mode 100644 examples/custom-widgets/goodbye-widgets.scm create mode 100644 examples/custom-widgets/goodbye.egg create mode 100644 examples/custom-widgets/goodbye.scm create mode 100644 examples/custom-widgets/goodbye.ui create mode 100644 examples/custom-widgets/main.cpp create mode 100644 examples/custom-widgets/prototypes.h diff --git a/examples/custom-widgets/README.md b/examples/custom-widgets/README.md new file mode 100644 index 0000000..8f4958d --- /dev/null +++ b/examples/custom-widgets/README.md @@ -0,0 +1,13 @@ +# “Goodbye” example + +This is a bare-minimum example egg that shows how one can define their own +custom QT widgets and bind to their methods in a way compatible with qt-light. + +It defines an `OurButton` C++ class that is derived from +[`QPushButton`](https://doc.qt.io/qt-6/qpushbutton.html) with one change: +It adds a `SayGoodbye()` method that changes the button’s label to “Goodbye”. + +We write a wrapper C-function `gb_saygoodbye()` and bind this function into +our `goodbye-widgets` module under the name `gb:saygoodbye`. It is run on the +button (originally labelled “Quit” in the `goodbye.ui` file), so changing its +text to “Goodbye”. diff --git a/examples/custom-widgets/goodbye-widgets.h b/examples/custom-widgets/goodbye-widgets.h new file mode 100644 index 0000000..4405d03 --- /dev/null +++ b/examples/custom-widgets/goodbye-widgets.h @@ -0,0 +1,8 @@ +#include + +class OurButton : public QPushButton { +public: + OurButton(QWidget* parent); + + void SayGoodbye(); +}; diff --git a/examples/custom-widgets/goodbye-widgets.scm b/examples/custom-widgets/goodbye-widgets.scm new file mode 100644 index 0000000..bc7618c --- /dev/null +++ b/examples/custom-widgets/goodbye-widgets.scm @@ -0,0 +1,43 @@ +;;;; qt-light.scm + + +(module goodbye-widgets (gb:saygoodbye) + +(import + scheme + (chicken base) + (chicken bitwise) + (chicken foreign) + (chicken format) + (chicken fixnum) + (chicken string) + bind + miscmacros + qt-light + srfi-4 srfi-1 protobj matchable + ) + + +(define (% (class 'gb-button))) + +(define (qt:->pointer i) (and i (? i pointer))) +(define (qt:pointer->button p) (and p (% (pointer p)))) + +(bind-rename/pattern "gb_" "gb:") +(bind-type gbbutton c-pointer gb:->pointer gb:pointer->button) + +#> +#define ___safe +#define ___bool int +#define ___out +<# + +#> +extern "C" { + #include "prototypes.h" + } +<# + +(bind-file* "prototypes.h") + +) diff --git a/examples/custom-widgets/goodbye.egg b/examples/custom-widgets/goodbye.egg new file mode 100644 index 0000000..45c1db7 --- /dev/null +++ b/examples/custom-widgets/goodbye.egg @@ -0,0 +1,13 @@ +((synopsis "An example program that uses a subclassed QT widget.") + (category ui) + (license "BSD") + (dependencies bind protobj qt-light matchable miscmacros shell) + (author "Jaidyn Ann") + (components + (extension + goodbye-widgets + (custom-build "../../chicken-compile-qt-extension.scm") + (csc-options ; + "-O3" "-d1" "-X" "bind" "-s" "-k" "-emit-link-file" "goodbye-widgets.link" "-o" "goodbye-widgets.o") + ) + (program goodbye))) diff --git a/examples/custom-widgets/goodbye.scm b/examples/custom-widgets/goodbye.scm new file mode 100644 index 0000000..491fd7e --- /dev/null +++ b/examples/custom-widgets/goodbye.scm @@ -0,0 +1,12 @@ +(import qt-light goodbye-widgets protobj + (chicken io)) + +(define a (qt:init)) +(define w (qt:widget (call-with-input-file "goodbye.ui" (lambda (p) (read-string #f p))))) +(print (? w pointer)) +(define b (qt:find w "goodbyeButton")) +(print b) +(qt:connect b "clicked()" a "quit()") +(qt:show w) +(gb:saygoodbye b) +(qt:run) diff --git a/examples/custom-widgets/goodbye.ui b/examples/custom-widgets/goodbye.ui new file mode 100644 index 0000000..bed8e8d --- /dev/null +++ b/examples/custom-widgets/goodbye.ui @@ -0,0 +1,61 @@ + + + Form + + + + 0 + 0 + 295 + 144 + + + + + + + + + 40 + 30 + 201 + 31 + + + + + 15 + true + + + + Adieu, monsieur! + + + Qt::AlignCenter + + + + + + 180 + 90 + 75 + 31 + + + + Quit + + + + + + OurButton + QPushButton +
custom_widget.h
+
+
+ + +
diff --git a/examples/custom-widgets/main.cpp b/examples/custom-widgets/main.cpp new file mode 100644 index 0000000..99fa911 --- /dev/null +++ b/examples/custom-widgets/main.cpp @@ -0,0 +1,29 @@ +#include "goodbye-widgets.h" + +#define gbbutton OurButton * + +OurButton::OurButton(QWidget* parent) + : QPushButton(parent) +{ +} + + +void +OurButton::SayGoodbye() +{ + setText("Goodbye"); +} + + +#define cwbutton OurButton * + +extern "C" { +#include "prototypes.h" +} + + +void +gb_saygoodbye(cwbutton button) +{ + button->SayGoodbye(); +} diff --git a/examples/custom-widgets/prototypes.h b/examples/custom-widgets/prototypes.h new file mode 100644 index 0000000..851d7f7 --- /dev/null +++ b/examples/custom-widgets/prototypes.h @@ -0,0 +1,4 @@ +/* prototypes.h */ + + +void gb_saygoodbye(gbbutton button);