Unwrap cpp_core::cpp_box::CppBox<qt_widgets::QWidget>


I have this basic code:

#![windows_subsystem = "windows"]

use qt_widgets::{
    cpp_core::{CppBox, MutPtr},
    QApplication, QLineEdit, QMessageBox, QPushButton, QVBoxLayout, QWidget,

fn main() {
    QApplication::init(|_app| unsafe {
        let mut window = QWidget::new_0a();
        window.resize(400, 250);


Here I'm trying to create a basic QWidget element:

let window = QWidget::new_0a();

However, now window has the type cpp_core::cpp_box::CppBox<qt_widgets::QWidget>. How can I 'unwrap' CppBox so that I actually have a QWidget element?

Compiling this program gives the following error:

error[E0599]: no method named `resize` found for type `cpp_core::cpp_box::CppBox<qt_widgets::QWidget>` in the current scope
  --> src/main.rs:18:16
18 |         window.resize(400, 250);
   |                ^^^^^^ help: there is a method with a similar name: `size`

QWidget has a method resize though.

can you use window.as_ref().resize()?

1 Like

QWidget::resize is an overloaded method, so it corresponds to multiple Rust methods with different names. You have to use resize_2a or resize_1a instead. See documentation.

CppBox implements Deref and DerefMut, so you can access QWidget's methods without any kind of unwrapping. Note that QWidget in Rust is an opaque type, so you cannot obtain it by value. It can only be used behind some kind of pointer (e.g. a CppBox).

1 Like

Nope. Thanks for your answer though!

Window.resize_2a() is the correct answer! Thanks.

1 Like

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