Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
knoxfighter opened this issue Apr 17, 2024 · 3 comments · Fixed by #932

Comments

@knoxfighter
Copy link
Contributor

knoxfighter commented Apr 17, 2024

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
Copy link
Contributor Author

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
Copy link
Collaborator

Also have a look at #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 ahayzen-kdab changed the title unable to make qsignal to call its closure Connections don't have must_use when returning a QMetaObjectConnectionGuard (was: unable to make qsignal to call its closure) Apr 18, 2024
@ahayzen-kdab
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants