Trouble creating a clipboard object with gtk-rs

Hi all,

I’m very new to gtk4 and trying to get a bit more experience by making a simple application that copies text to the clipboard upon clicking a button. However, I’m already running into trouble creating a clipboard object:

use gtk4::gdk::Clipboard;
use gtk4::prelude::*;
use gtk4::{Application, ApplicationWindow, Button};

const APP_ID: &str = "org.testing.Glower";

fn main() {
	// Create a new application
	let app = Application::builder().application_id(APP_ID).build();

	// Connect to the "activate" signal of 'app'
	app.connect_activate(build_ui);
	
	// Run the application
	app.run();
}

fn build_ui(app: &Application) {

	let cb: Clipboard = DisplayExt::clipboard(&self);

	// Create a button with label and margins
    let button = Button::builder()
        .label("😀")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    // Connect to "clicked" signal of `button`
    button.connect_clicked(move |button| {
        // Set the label to "Hello World!" after the button has been clicked on
        button.set_label("Hello World");
    });

	// Create a window and set the title
	let window = ApplicationWindow::builder()
		.application(app)
		.title("Glower")
		.child(&button)
		.build();

	// Present window
	window.present();
}

Here’s what the compiler is telling me. I feel like I’m missing something obvious but I haven’t been able to figure out where I’m going wrong.

error[E0424]: expected value, found module `self`
  --> src/main.rs:19:34
   |
17 | fn build_ui(app: &Application) {
   |    -------- this function can't have a `self` parameter
18 |
19 |     let cb = DisplayExt::clipboard(&self);
   |                                     ^^^^ `self` value is a keyword only available in methods with a `self` parameter

Hello, you may want to read through the Rust book and make sure you understand the usage of the self keyword. That should only be used for methods: Method Syntax - The Rust Programming Language

The DisplayExt trait includes methods for calling only on Display objects. You need to obtain one first: what you probably want to do here is gdk::Display::default().unwrap().clipboard().

1 Like

There is a clipboard example in the gtk4-rs repository by the way.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.