I have a simple paint kivy I wish to add shape recognizer to it similar as in xournal.
Quoting docs:
"The shape recognizer is also a special operating mode
of the pen and highlighter tools. When it is enabled, Xournal attempts
to recognize geometric shapes as they are drawn, and if successful
will replace the drawn strokes accordingly. The shapes that can be
recognized are: line segments, circles, rectangles, arrows, triangles
and quadrilaterals. Polygonal shapes can be drawn in a single stroke
or in a sequence of consecutive strokes."
Code:
from kivy.base import EventLoop
from kivy.config import Config
from kivy.graphics import Color, Line
from kivy.uix.behaviors import ToggleButtonBehavior
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.widget import Widget
from kivy.utils import get_color_from_hex
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.clock import Clock
class Update_Location(Widget):
pass
class CanvasWidget(Widget):
line_width = 2
def on_touch_down(self, touch):
if Widget.on_touch_down(self, touch):
return
with self.canvas:
touch.ud['current_line'] = Line(
points=(touch.x, touch.y),
width=self.line_width)
def on_touch_move(self, touch):
if 'current_line' in touch.ud:
touch.ud['current_line'].points += (touch.x, touch.y)
def set_color(self, new_color):
self.last_color = new_color
self.canvas.add(Color(*new_color))
def set_line_width(self, line_width='Normal'):
self.line_width = {
'Thin': 1, 'Normal': 2, 'Thick': 4
}[line_width]
def clear_canvas(self):
saved = self.children[:]
self.clear_widgets()
self.canvas.clear()
for widget in saved:
self.add_widget(widget)
self.set_color(self.last_color)
def start_server(self):
host = '127.0.0.1'
port = 5000
notification_text="Server started on host: "+host+" and port: "+str(port)
server_start=Popup(title='Notification',content=Label(text=notification_text),size_hint=(.75,.75),auto_dismiss=True)
server_start.open()
Clock.schedule_interval(server_start.dismiss, 3)
class PaintApp(App):
def build(self):
EventLoop.ensure_window()
if EventLoop.window.__class__.__name__.endswith('Pygame'):
try:
from pygame import mouse
a, b = pygame_compile_cursor()
mouse.set_cursor((24, 24), (9, 9), a, b)
except:
pass
#boxlayout
self.layout = BoxLayout(orientation='vertical')
self.canvas_widget = CanvasWidget()
self.canvas_widget.set_color(
get_color_from_hex('#2980b9'))
self.layout.add_widget(self.canvas_widget)
#self.layout.add_widget(Label(text="Started Server : False , Connected to Server : False",color=(1,1,1),size_hint=(1, .1)))
return self.layout
#return self.canvas_widget
class RadioButton(ToggleButton):
def _do_press(self):
if self.state == 'normal':
ToggleButtonBehavior._do_press(self)
def pygame_compile_cursor(black='#', white='-'):
aa, bb = [], []
a = b = 0
i = 8
for s in CURSOR:
for c in s:
a <<= 1
b <<= 1
i -= 1
if c == black:
a |= 1
b |= 1
elif c == white:
b |= 1
if not i:
aa.append(a)
bb.append(b)
a = b = 0
i = 8
return tuple(aa), tuple(bb)
if __name__ == '__main__':
Config.set('graphics', 'width', '960')
Config.set('graphics', 'height', '540') # 16:9
# Config.set('graphics', 'resizable', '0')
# Config.set('input', 'mouse', 'mouse,disable_multitouch')
from kivy.core.window import Window
Window.clearcolor = get_color_from_hex('#ffffff')
PaintApp().run()
Now, I have a set of points stored in touch.ud['current_line']. On on_touch_up I should pass these points/set of coordinates which replaces them with the shape of the corresponding size. Are there any such code in available, I wasn't to find in xounral source clearly.
Related
I want to draw image (*.png file) at FlayoutBox, on pc is result OK, after using KivyLauncher is on phone screen only white square. I tested two variants for drawing (draw_img_cnv and draw_img_wdg), but both results are bad.
Cann you explain me:
why is result white square on phone screen,
why draw_img_wdg isn't centered on pc,
how is diferent between both solutions?
Thank You!
from kivy.app import App
from kivy.graphics import Color, Rectangle, Ellipse
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.image import Image
import math
class Screen(FloatLayout):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self.li_item = []
return
def draw_img_cnv(self):
size_img = (498, 498)
pos_img = (self.center[0] -size_img[0]/2.0, self.center[1] -size_img[1]/2.0)
with self.canvas:
Color(0, 1, 0, 1)
self.rect1 = Rectangle(size=self.size, pos=self.pos)
self. rose = Image(source="rose.png",pos=pos_img,size=size_img )
def draw_img_wdg(self):
size_img = (498, 498)
pos_img = (self.center[0] -size_img[0]/2.0, self.center[1] -size_img[1]/2.0)
with self.canvas:
Color(0, 1, 0, 1)
self.rect1 = Rectangle(size=self.size, pos=self.pos)
self. rose = Image(source="rose.png",pos=pos_img,size=size_img )
self.add_widget(self.rose)
return
class MainApp(App):
def build(self):
self.pos_sun = None
root = BoxLayout(orientation = 'vertical')
self.screen = Screen()
button = Button(text = 'Press',size_hint=(1, None), height=50)
root.add_widget(self.screen)
root.add_widget(button)
self.screen.draw_img_wdg()
self.screen.bind(size=self._update_rect, pos=self._update_rect)
return root
def _update_rect(self, instance, value):
self.screen.rect1.pos = instance.pos
self.screen.rect1.size = instance.size
self.screen.rose. pos = (instance.center[0] -498/2.0, \
instance.center[1] -498/2.0)
return
if __name__ == '__main__':
MainApp().run()
I am trying to display the speedometer in my MainWindow screen. Right now when i run the code, the speedometer works but it is not displayed on the MainWindow screen which i want rather it is just appearing on a normal screen. It is possible to combine class Gauge(Widget): and class MainWindow(Screen): together so that the speedometer will actually be displayed on the MainWindow?
.py file
import kivy
kivy.require('1.6.0')
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import NumericProperty
from kivy.properties import StringProperty
from kivy.properties import BoundedNumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.scatter import Scatter
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.progressbar import ProgressBar
from os.path import join, dirname, abspath
from kivy.uix.screenmanager import Screen, ScreenManager
class WindowManager(ScreenManager):
pass
class MainWindow(Screen):
pass
class Gauge(Widget):
unit = NumericProperty(1.8)
value = BoundedNumericProperty(0, min=0, max=100, errorvalue=0)
path = dirname(abspath(__file__))
file_gauge = StringProperty(join(path, "cadran.png"))
file_needle = StringProperty(join(path, "needle.png"))
size_gauge = BoundedNumericProperty(128, min=128, max=256, errorvalue=128)
size_text = NumericProperty(10)
def __init__(self, **kwargs):
super(Gauge, self).__init__(**kwargs)
self._gauge = Scatter(
size=(1350,600),
do_rotation=False,
do_scale=False,
do_translation=False
)
_img_gauge = Image(
source=self.file_gauge,
size=(1350,600)
)
self._needle = Scatter(
size=(self.size_gauge, self.size_gauge),
do_rotation=False,
do_scale=False,
do_translation=False
)
_img_needle = Image(
source=self.file_needle,
size=(self.size_gauge, self.size_gauge)
)
self._glab = Label(font_size=self.size_text, markup=True)
self._progress = ProgressBar(max=100, height=20, value=self.value , size=(500,400))
self._gauge.add_widget(_img_gauge)
self._needle.add_widget(_img_needle)
self.add_widget(self._gauge)
self.add_widget(self._needle)
self.add_widget(self._glab)
self.add_widget(self._progress)
self.bind(pos=self._update)
self.bind(size=self._update)
self.bind(value=self._turn)
def _update(self, *args):
'''
Update gauge and needle positions after sizing or positioning.
'''
self._gauge.pos = self.pos
self._needle.pos = (self.x, self.y)
self._needle.center = self._gauge.center
self._glab.center_x = self._gauge.center_x
self._glab.center_y = self._gauge.center_y + (self.size_gauge / 4)
self._progress.x = self._gauge.x + (self.size_gauge/0.468 )
self._progress.y = self._gauge.y + (self.size_gauge/4 )
self._progress.width = self.size_gauge
def _turn(self, *args):
'''
Turn needle, 1 degree = 1 unit, 0 degree point start on 50 value.
'''
self._needle.center_x = self._gauge.center_x
self._needle.center_y = self._gauge.center_y
self._needle.rotation = (50 * self.unit) - (self.value * self.unit)
self._glab.text = "[b]{0:.0f}[/b]".format(self.value)
self._progress.value = self.value
class GaugeApp(App):
increasing = NumericProperty(1)
begin = NumericProperty(50)
step = NumericProperty(1)
def build(self):
box = BoxLayout(orientation='horizontal', padding=5)
self.gauge = Gauge(value=50, size_gauge=256, size_text=25)
box.add_widget(self.gauge)
Clock.schedule_interval(lambda *t: self.gauge_increment(), 0.05)
return box
def gauge_increment(self):
begin = self.begin
begin += self.step * self.increasing
if begin > 0 and begin < 100:
self.gauge.value = begin
else:
self.increasing *= -1
self.begin = begin
if __name__ == '__main__':
GaugeApp().run()
You need to create an instance of ScreenManager and an instance of Screen. Next you add_widget the Screen to ScreenManager and to the Screen you add box
sm = ScreenManager()
s1 = Screen()
s1.add_widget(box)
sm.add_widget(s1)
This is the complete code which uses your new classes
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import NumericProperty
from kivy.properties import StringProperty
from kivy.properties import BoundedNumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.scatter import Scatter
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.progressbar import ProgressBar
from os.path import join, dirname, abspath
from kivy.uix.screenmanager import Screen, ScreenManager
class WindowManager(ScreenManager):
pass
class MainWindow(Screen):
pass
class Gauge(Widget):
unit = NumericProperty(1.8)
value = BoundedNumericProperty(0, min=0, max=100, errorvalue=0)
path = dirname(abspath(__file__))
file_gauge = StringProperty(join(path, "cadran.png"))
file_needle = StringProperty(join(path, "needle.png"))
size_gauge = BoundedNumericProperty(128, min=128, max=256, errorvalue=128)
size_text = NumericProperty(10)
def __init__(self, **kwargs):
super(Gauge, self).__init__(**kwargs)
self._gauge = Scatter(
size=(1350,600),
do_rotation=False,
do_scale=False,
do_translation=False
)
_img_gauge = Image(
source=self.file_gauge,
size=(1350,600)
)
self._needle = Scatter(
size=(self.size_gauge, self.size_gauge),
do_rotation=False,
do_scale=False,
do_translation=False
)
_img_needle = Image(
source=self.file_needle,
size=(self.size_gauge, self.size_gauge)
)
self._glab = Label(font_size=self.size_text, markup=True)
self._progress = ProgressBar(max=100, height=20, value=self.value , size=(500,400))
self._gauge.add_widget(_img_gauge)
self._needle.add_widget(_img_needle)
self.add_widget(self._gauge)
self.add_widget(self._needle)
self.add_widget(self._glab)
self.add_widget(self._progress)
self.bind(pos=self._update)
self.bind(size=self._update)
self.bind(value=self._turn)
def _update(self, *args):
'''
Update gauge and needle positions after sizing or positioning.
'''
self._gauge.pos = self.pos
self._needle.pos = (self.x, self.y)
self._needle.center = self._gauge.center
self._glab.center_x = self._gauge.center_x
self._glab.center_y = self._gauge.center_y + (self.size_gauge / 4)
self._progress.x = self._gauge.x + (self.size_gauge/0.468 )
self._progress.y = self._gauge.y + (self.size_gauge/4 )
self._progress.width = self.size_gauge
def _turn(self, *args):
'''
Turn needle, 1 degree = 1 unit, 0 degree point start on 50 value.
'''
self._needle.center_x = self._gauge.center_x
self._needle.center_y = self._gauge.center_y
self._needle.rotation = (50 * self.unit) - (self.value * self.unit)
self._glab.text = "[b]{0:.0f}[/b]".format(self.value)
self._progress.value = self.value
class GaugeApp(App):
increasing = NumericProperty(1)
begin = NumericProperty(50)
step = NumericProperty(1)
def build(self):
box = BoxLayout(orientation='horizontal', padding=5)
self.gauge = Gauge(value=50, size_gauge=256, size_text=25)
box.add_widget(self.gauge)
Clock.schedule_interval(lambda *t: self.gauge_increment(), 0.05)
sm = WindowManager()
s1 = MainWindow()
s1.add_widget(box)
sm.add_widget(s1)
return sm
def gauge_increment(self):
begin = self.begin
begin += self.step * self.increasing
if begin > 0 and begin < 100:
self.gauge.value = begin
else:
self.increasing *= -1
self.begin = begin
if __name__ == '__main__':
GaugeApp().run()
What I tried to do:
I wanted my program to print a message when it collides with the sprite in the middle.
Problem:
It doesn't stop printing that it collided. I looked into it with pdb and it says that the Player Sprite is the size of the whole screen, which is weird because I set the image size to 32px by 32px.
What it looks like
Code and Sprites mentioned
Link to mentioned block
Link to mentioned sprite
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
import random
import time
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
#############################################################################
class Collider(Image):
pass
#############################################################################
root = FloatLayout()
class GameScreen(Screen):
def __init__(self, **kwargs):
super(GameScreen, self).__init__(**kwargs)
self.wimg = Basic(size = (32,32), source='sprite.png', pos=(270,-120))
self.block = Collider(size = (32,32), source = 'block.png')
def playy(self):
root.add_widget(self.wimg)
Clock.schedule_interval(self.wimg.update, 1.0 / 60.0)
def worldgen1(self):
self.add_widget(root)
root.add_widget(self.block)
def on_enter(self):
self.worldgen1()
self.playy()
#############################################################################
class Basic(Image):
pFrame = 0
def __init__(self, **kwargs):
super(Basic, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_keyboard_down)
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'left':
self.x -= 10
elif keycode[1] == 'right':
self.x += 10
elif keycode[1] == 'up':
self.y += 10
elif keycode[1] == 'down':
self.y -= 10
else:
return False
return True
def update(self, dt):
self.pFrame += 1
if self.parent.parent.block.collide_widget(self.parent.parent.wimg):
print ("collide with", self.parent.parent.block)
#############################################################################
class MeinApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(GameScreen(name='gameplay'))
return sm
if __name__ == '__main__':
MeinApp().run()
In this case the problem is that FloatLayout uses the size_hint to set the size of the widget when it is added, since it does not set it, they take the value of (1, 1), that is, the size of the window.
The solution is to set size_hint to (None, None), in addition to using allow_stretch in True and keep_ratio in False. In the code I have set the block pos_hint to (0.5, 0.5) and it will anchor it to that position so it will not move, if you want it to be able to move you must set the pos, not the pos_hint.
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
import random
import time
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
#############################################################################
class Collider(Image):
pass
#############################################################################
root = FloatLayout()
class GameScreen(Screen):
def __init__(self, **kwargs):
super(GameScreen, self).__init__(**kwargs)
self.wimg = Basic(size = (32,32), pos=(200, 100), source='sprite.png', size_hint=(None, None), allow_stretch=True, keep_ratio=False)
self.block = Collider(size = (32,32), pos_hint={'x': 0.5, 'y':0.5}, source = 'block.png', size_hint=(None, None), allow_stretch=True, keep_ratio=False)
def playy(self):
root.add_widget(self.wimg)
Clock.schedule_interval(self.wimg.update, 1.0 / 60.0)
def worldgen1(self):
self.add_widget(root)
root.add_widget(self.block)
def on_enter(self):
self.worldgen1()
self.playy()
#############################################################################
class Basic(Image):
pFrame = 0
def __init__(self, **kwargs):
super(Basic, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_keyboard_down)
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'left':
self.x -= 10
elif keycode[1] == 'right':
self.x += 10
elif keycode[1] == 'up':
self.y += 10
elif keycode[1] == 'down':
self.y -= 10
else:
return False
return True
def update(self, dt):
self.pFrame += 1
if self.parent.parent.block.collide_widget(self.parent.parent.wimg):
print ("collide with", self.parent.parent.block)
#############################################################################
class MeinApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(GameScreen(name='gameplay'))
return sm
if __name__ == '__main__':
MeinApp().run()
In the following code i don't explain two problems:
If I use method __init__ --> Scene.canvas = None
I don't underestand to relative position objects on image.
I wanted to obtain knowledges about coordinates system from Kivy Manual, but without success.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.graphics import Ellipse
class Scene(Widget):
"""
def __init__(self):
self.x = 0
self.y = 100
self.dir = 1
"""
def set_par(self):
self.x = 0
self.y = 100
self.dir = 1
def create_ball(self):
with self.canvas:
Ellipse(pos = (0,0), size = (40, 40))
img = Image(pos = (0,0), source = 'image1.png') # square 40x40pixels
self.add_widget(img)
class SceneApp(App):
def build(self):
scene = Scene()
scene.create_ball()
return scene
if __name__ == '__main__':
SceneApp().run()
Printscreen form my code:
You did not call super() in your __init__ method. So Widget's __init__ method just get completely overrited.
Then use FloatLayout instead of Widget
You can to do something like this:
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Ellipse
Window.size = (300, 300)
class Scene(FloatLayout):
def __init__(self, **kwargs):
super(Scene,self).__init__(**kwargs)
self.size = 300,300
def create_ball(self):
img = Image(pos = (-130, -130), source = 'image1.png') # square 40x40pixels
self.add_widget(img)
with self.canvas:
Ellipse(pos = (0,0), size = (40, 40))
class SceneApp(App):
def build(self):
scene = Scene()
scene.create_ball()
return scene
if __name__ == '__main__':
SceneApp().run()
i want to b able to draw only in the white area (the recangle area) for example if your trying to draw behind the buttons or over the label it wont draw. is it possible? i need it for my project in school, i need to be able to draw only in the white area so the paint wont cover the label for example or not to be able drawing under the buttons
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Rectangle, Line
from kivy.uix.stencilview import StencilView
from kivy.core.window import Window
import socket
import sys
import os
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from functools import partial
class MyPaintWidget(Widget):
lineSize = 5
def on_touch_down(self, touch):
with self.canvas:
touch.ud['Line'] = Line(points=(touch.x, touch.y), width=self.lineSize)
def on_touch_move(self, touch):
touch.ud['Line'].points += [touch.x, touch.y]
class MyPaintApp(App):
def build(self):
self.parent = Widget()
self.painter=MyPaintWidget(size=(695,510),pos=(100,50))
with self.painter.canvas:
Rectangle(pos=(100,50), size=(695,510))
with open("Send_word.txt",'r') as fr:
word_lable = Label(text = "Your word is " + fr.read(),font_size='30sp',pos = (400,530))
self.parent.add_widget(word_lable)
finishbtn = Button(text='finish',pos=(0, 0),size = (100,150))#finish button position and text
finishbtn.bind(on_release=self.finish)#finish button onclick
erasebtn = Button(text = "Erasor",pos=(0,150),size =(100,150))#erasor button position and text
erasebtn.bind(on_release=self.black)
redbtn = Button(text = "Red",background_color=(255,0,0,1.0),pos=(100, 0), size=(100,50))
redbtn.bind(on_release=self.red)
bluebtn = Button(text = "Blue",background_color=(0,0,255,1.0),pos=(200, 0), size=(100,50))
bluebtn.bind(on_release=self.blue)
greenbtn = Button(text = "Green",background_color=(0,255,0,1.0),pos=(300, 0), size=(100,50))
greenbtn.bind(on_release=self.green)
whitebtn = Button(text = "Black",pos=(400, 0), size=(100,50))
whitebtn.bind(on_release=self.white)
yellowbtn = Button(text = "Yellow",background_color=(255,255,0,1.0),pos=(500, 0), size=(100,50))
yellowbtn.bind(on_release=self.yellow)
lightbluebtn = Button(text = "L.Blue",background_color=(0,255,255,1.0),pos=(600, 0), size=(100,50))
lightbluebtn.bind(on_release=self.lightblue)
purplebtn = Button(text = "Purple", size=(100,50),background_color=(148,0,211,1.0),pos=(700,0))
purplebtn.bind(on_release=self.purple)
sizeupbtn = Button(text = "SizeUp",pos=(0, 300),size = (100,150))
sizeupbtn.bind(on_release=self.SizeUp)
sizedowmbtn = Button(text = "SizeDown",pos=(0, 450),size = (100,150))
sizedowmbtn.bind(on_release=self.SizeDown)
self.parent.add_widget(self.painter)
#self.parent.add_widget(word_lable())
self.parent.add_widget(finishbtn)
self.parent.add_widget(redbtn)
self.parent.add_widget(bluebtn)
self.parent.add_widget(greenbtn)
self.parent.add_widget(whitebtn)
self.parent.add_widget(yellowbtn)
self.parent.add_widget(erasebtn)
self.parent.add_widget(lightbluebtn)
self.parent.add_widget(purplebtn)
self.parent.add_widget(sizeupbtn)
self.parent.add_widget(sizedowmbtn)
return self.parent
def finish(self,obj):
self.painter.export_to_png("screenshot.png")
sexi = open("Send_word.txt",'r').read()
print repr(sexi)
send_file(sexi)
def red(self,obj):
with self.painter.canvas:
Color(1,0,0)
def blue(self,obj):
with self.painter.canvas:
Color(0,0,1)
def green(self,obj):
with self.painter.canvas:
Color(0,1,0)
def white(self,obj):
with self.painter.canvas:
Color(0,0,0)
def yellow(self,obj):
with self.painter.canvas:
Color(1,1,0)
def black(self,obj):
with self.painter.canvas:
Color(1,1,1)
def lightblue(self,obj):
with self.painter.canvas:
Color(0,1,1)
def purple(self,obj):
with self.painter.canvas:
Color(1,0,1)
def SizeUp(self,obj):
if(self.painter.lineSize <100):
self.painter.lineSize += 5
def SizeDown(self,obj):
if(self.painter.lineSize >1):
self.painter.lineSize -= 5
if __name__ == '__main__':
MyPaintApp().run()
You can do that the easy way and the hard way. The easy way is using StencilView as a widget, which has a nice demo.
The harder way is using the Stencil directly and target only specific areas of the widget's canvas.