Introduction to V Language and Desktop App Development
V is a modern, high-performance, and statically typed programming language designed for simplicity and speed. It boasts fast compilation times, safe memory management, and seamless interoperability with C. These features make it an excellent choice for a wide range of applications, from systems programming to web and desktop app development.
In this tutorial, we will create a simple desktop app—a to-do list—with an embedded database using V language. This project will help you understand the basics of V for desktop applications and how to use its built-in database capabilities.
Creating a To-Do List Desktop App with V
Step 1: Set Up Your Development Environment
Install V:
Follow the installation instructions for your operating system from the official V documentation.
Create a New Project:
Open your terminal and create a new directory for your project.
mkdir todo_app
cd todo_app
v init
Step 2: Define the To-Do Item Model
Create a new file named models.v
to define the structure of a to-do item.
module main
struct Todo {
id int [primary; sql: serial]
title string
done bool
}
Step 3: Set Up the Database
Create a file named database.v
to handle database operations.
module main
import sqlite
struct App {
db sqlite.DB
}
fn (app &App) init_db() {
sql app.db {
create table Todo
}
}
fn (app &App) add_todo_item(title string) {
new_todo := Todo{
title: title
done: false
}
sql app.db {
insert new_todo into Todo
}
}
fn (app &App) get_todo_items() []Todo {
return sql app.db {
select from Todo
}
}
fn (app &App) update_todo_item(id int, done bool) {
sql app.db {
update Todo set done = done where id == id
}
}
fn (app &App) delete_todo_item(id int) {
sql app.db {
delete from Todo where id == id
}
}
Step 4: Create the User Interface
Create a file named main.v
to build the user interface.
module main
import iui as ui
fn main() {
mut window := ui.window(
width: 800
height: 600
title: 'To-Do List'
)
mut app := App{
db: sqlite.connect(':memory:') or { panic(err) }
}
app.init_db()
mut title_input := ui.textbox(placeholder: 'Enter a new task')
mut add_button := ui.button(
text: 'Add'
onclick: fn [mut app, mut title_input, mut window] () {
app.add_todo_item(title_input.text())
title_input.set_text('')
update_todo_list(mut window, app)
}
)
window.add_child(title_input)
window.add_child(add_button)
update_todo_list(mut window, app)
ui.run(window)
}
fn update_todo_list(mut window ui.Window, app &App) {
todo_items := app.get_todo_items()
mut list_container := ui.vbox()
for todo in todo_items {
mut checkbox := ui.checkbox(
text: todo.title
checked: todo.done
onclick: fn [mut app, todo] (checked bool) {
app.update_todo_item(todo.id, checked)
}
)
mut delete_button := ui.button(
text: 'Delete'
onclick: fn [mut app, todo, mut window] () {
app.delete_todo_item(todo.id)
update_todo_list(mut window, app)
}
)
mut item_container := ui.hbox()
item_container.add_child(checkbox)
item_container.add_child(delete_button)
list_container.add_child(item_container)
}
window.clear()
window.add_child(list_container)
}
Step 5: Run the Application
Run your application using the V compiler.
v run .
Conclusion
In this tutorial, we explored the V language by building a simple to-do list desktop application with an embedded database. We covered the basics of setting up a project, defining data models, handling database operations, and creating a user interface.
V's simplicity and performance make it a great choice for developing desktop applications efficiently. Continue experimenting with V to discover more of its capabilities and potential for your projects.