Add “custom-widgets” example

This small example demonstrates defining and
binding to your own subclass of a QT widget.
This commit is contained in:
Jaidyn Ann 2024-02-04 15:03:50 -06:00
parent 890c20ca77
commit 1704d392e8
8 changed files with 183 additions and 0 deletions

View File

@ -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 buttons 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”.

View File

@ -0,0 +1,8 @@
#include <QPushButton>
class OurButton : public QPushButton {
public:
OurButton(QWidget* parent);
void SayGoodbye();
};

View File

@ -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 <gb-button> (% <qt-widget> (class 'gb-button)))
(define (qt:->pointer i) (and i (? i pointer)))
(define (qt:pointer->button p) (and p (% <gb-button> (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")
)

View File

@ -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)))

View File

@ -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)

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>295</width>
<height>144</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>40</x>
<y>30</y>
<width>201</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>15</pointsize>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Adieu, monsieur!</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="OurButton" name="goodbyeButton">
<property name="geometry">
<rect>
<x>180</x>
<y>90</y>
<width>75</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Quit</string>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>OurButton</class>
<extends>QPushButton</extends>
<header>custom_widget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -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();
}

View File

@ -0,0 +1,4 @@
/* prototypes.h */
void gb_saygoodbye(gbbutton button);