KDAB / cxx-qt

Safe interop between Rust and Qt
https://kdab.github.io/cxx-qt/book/
973 stars 67 forks source link

Connections don't have must_use when returning a QMetaObjectConnectionGuard (was: unable to make qsignal to call its closure) #930

Closed knoxfighter closed 2 months ago

knoxfighter commented 2 months ago

I mapped quite a few Widgets for my project. Now i want to use callbacks on a QPushButton. Unfortunately i am unable to make the clicked (on_clicked) signal to work. It seems like the function is never called.

Example code:

fn main() {
    // Create the application and engine
    let mut app = QApplication::new();

    let mut main_window = QMainWindow_new_ptr();

    let mut button = unsafe { QPushButton_new_ptr_with_text_parent(&QString::from("test"), main_window as *mut _) };
    let mut button = unsafe {Pin::new_unchecked(&mut *button)};
    println!("test");
    button.as_mut().on_clicked(|btn, status| println!("button pressed! {status}"));
    // button.show();

    unsafe {Pin::new_unchecked(&mut *main_window)}.show();

    // Start the app
    if let Some(app) = app.as_mut() {
        app.exec();
    }
}

Definition of the signal:

#[cxx_qt::bridge]
pub mod qobject {
    unsafe extern "C++Qt" {
        include!(<QPushButton>);
        #[qobject]
        type QPushButton;

        #[qsignal]
        fn clicked(self: Pin<&mut QPushButton>, checked: bool);
    }
}

I pushed the full example to my fork of this project: https://github.com/binja-tools/cxx-qt/tree/test-button/examples/cargo_without_cmake_2

Am i doing something wrong or is this not implemented for C++Qt classes?

knoxfighter commented 2 months ago

RTFM: Don't drop the return value of the on_clicked function.

#[must_use] should be added to the QMetaObjectConnectionGuard to prevent this in the future.

ahayzen-kdab commented 2 months ago

Also have a look at https://github.com/KDAB/cxx-qt/pull/622/ we've been building as an example for widgets.

Ah hmm, we used to have #[must_use] but it must have been lost in the refactor to having QMetaObjectConnectionGuard as it's own type.

Let change this issue to track adding back #[must_use] to the connection calls.

ahayzen-kdab commented 2 months ago

So #932 adds must_use back in the right place for the changes in main