Code broken after updating Rust

Hello

I've updated my Rust to the latest version, and when I suddenly tried to run my code it outputs a lot of errors. I didn't changed my code though.

Code:

#![windows_subsystem = "windows"]

use qt_widgets::{
    cpp_core::{CppBox, MutPtr},
    qt_core::QString,
    qt_core::Slot,
    QApplication, QLineEdit, QMessageBox, QPushButton, QVBoxLayout, QWidget,
    QLabel,
    qt_gui::QFont,
    qt_core::QObject,
    QLayoutItem,
};

//Userinterface handling all the possible UserInterface-screens
//Each function is to build a specific userinterface (depending on e.g
//selected option)
struct UserInterface {
  //  layout: Option<CppBox<QVBoxLayout>>,
}
impl UserInterface {
    //Constructor
    pub fn new() -> UserInterface {
        UserInterface {
//            layout: None,
        }
    }

    //Build the labels, buttons,... for the startscreen
    pub fn build_start_screen(&mut self, window: cpp_core::MutPtr<QWidget>) {
        unsafe {
            //Big fat font
            let mut big_font : CppBox<QFont> = QFont::new();
            big_font.set_family(&QString::from_std_str("Sans Serif"));
            big_font.set_point_size(24);
            big_font.set_weight(400);

            //Label 'Cat Manager'
            let mut label_a : CppBox<QLabel> = QLabel::new();
            label_a.set_text(&QString::from_std_str("Cat Manager"));
            label_a.set_font(&big_font);

            //Label 'Your favourite...'
            let mut label_b : CppBox<QLabel> = QLabel::new();
            label_b.set_text(&QString::from_std_str("Your favourite application to manage your best friends!"));

            //Button to go to Option A-screen
            let mut option_a_btn : CppBox<QPushButton> = QPushButton::new();
            option_a_btn.set_text(&QString::from_std_str("Go option A"));
           
            //When button option_a_btn is clicked we need to toggle the screen
            //to the Option A-userinterface
            //let window_ptr = window.as_mut_ptr();
            let option_a_btn_clicked = Slot::new(move || {
                println!("Rofl");
                self.build_option_a_screen(window);
            });
            option_a_btn.clicked().connect(&option_a_btn_clicked);

            //Add all the widgets to the layout
            window.layout().add_widget(label_a.into_ptr());
            window.layout().add_widget(label_b.into_ptr());
            window.layout().add_widget(option_a_btn.into_ptr());
            
            //self.layout = Some(layout);

        }
    }

    //Build labels, options,... for the screen which shows when Option A gets selected
    pub fn build_option_a_screen(&mut self, window: cpp_core::MutPtr<QWidget>) {
        unsafe {
            self.reset(window);

            //Label 'Cat Manager'
            let mut label_a : CppBox<QLabel> = QLabel::new();
            label_a.set_text(&QString::from_std_str("Option A selected!"));
          
            window.layout().add_widget(label_a.into_ptr());
        }
    }

    pub fn reset(&mut self, window: cpp_core::MutPtr<QWidget>) {
        unsafe {
            while window.layout().count() > 0 {
                let child : cpp_core::MutPtr<QLayoutItem> = window.layout().take_at(0);
                window.layout().delete_later();
            }

        
        }
    }
}

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

        let mut layout : CppBox<QVBoxLayout> = QVBoxLayout::new_1a(&mut window);

        let mut ui : UserInterface = UserInterface::new();
        let window_ptr = window.as_mut_ptr();
        ui.build_start_screen(window_ptr);

        window.show();

        QApplication::exec()
    })
}

Errors:

   Compiling Qt-rs v0.1.0 (/Users/niel/School/Rust/rust_gui/Qt-rs)
error[E0433]: failed to resolve: maybe a missing crate `qt_widgets`?
 --> src/main.rs:3:5
  |
3 | use qt_widgets::{
  |     ^^^^^^^^^^ maybe a missing crate `qt_widgets`?

error[E0432]: unresolved import `qt_widgets`
 --> src/main.rs:3:5
  |
3 | use qt_widgets::{
  |     ^^^^^^^^^^ maybe a missing crate `qt_widgets`?

error[E0433]: failed to resolve: use of undeclared type or module `QFont`
  --> src/main.rs:32:48
   |
32 |             let mut big_font : CppBox<QFont> = QFont::new();
   |                                                ^^^^^ use of undeclared type or module `QFont`

error[E0433]: failed to resolve: use of undeclared type or module `QString`
  --> src/main.rs:33:34
   |
33 |             big_font.set_family(&QString::from_std_str("Sans Serif"));
   |                                  ^^^^^^^ use of undeclared type or module `QString`

error[E0433]: failed to resolve: use of undeclared type or module `QString`
  --> src/main.rs:39:31
   |
39 |             label_a.set_text(&QString::from_std_str("Cat Manager"));
   |                               ^^^^^^^ use of undeclared type or module `QString`

error[E0433]: failed to resolve: use of undeclared type or module `QString`
  --> src/main.rs:44:31
   |
44 |             label_b.set_text(&QString::from_std_str("Your favourite application to manage your best friends!"));
   |                               ^^^^^^^ use of undeclared type or module `QString`

error[E0433]: failed to resolve: use of undeclared type or module `QString`
  --> src/main.rs:48:36
   |
48 |             option_a_btn.set_text(&QString::from_std_str("Go option A"));
   |                                    ^^^^^^^ use of undeclared type or module `QString`

error[E0433]: failed to resolve: use of undeclared type or module `Slot`
  --> src/main.rs:53:40
   |
53 |             let option_a_btn_clicked = Slot::new(move || {
   |                                        ^^^^ use of undeclared type or module `Slot`

error[E0433]: failed to resolve: use of undeclared type or module `QString`
  --> src/main.rs:76:31
   |
76 |             label_a.set_text(&QString::from_std_str("Option A selected!"));
   |                               ^^^^^^^ use of undeclared type or module `QString`

error[E0412]: cannot find type `CppBox` in this scope
  --> src/main.rs:32:32
   |
32 |             let mut big_font : CppBox<QFont> = QFont::new();
   |                                ^^^^^^ not found in this scope

error[E0412]: cannot find type `QFont` in this scope
  --> src/main.rs:32:39
   |
20 | impl UserInterface {
   |     - help: you might be missing a type parameter: `<QFont>`
...
32 |             let mut big_font : CppBox<QFont> = QFont::new();
   |                                       ^^^^^ not found in this scope

error[E0412]: cannot find type `CppBox` in this scope
  --> src/main.rs:38:31
   |
38 |             let mut label_a : CppBox<QLabel> = QLabel::new();
   |                               ^^^^^^ not found in this scope

error[E0412]: cannot find type `CppBox` in this scope
  --> src/main.rs:43:31
   |
43 |             let mut label_b : CppBox<QLabel> = QLabel::new();
   |                               ^^^^^^ not found in this scope

error[E0412]: cannot find type `CppBox` in this scope
  --> src/main.rs:47:36
   |
47 |             let mut option_a_btn : CppBox<QPushButton> = QPushButton::new();
   |                                    ^^^^^^ not found in this scope

error[E0412]: cannot find type `CppBox` in this scope
  --> src/main.rs:75:31
   |
75 |             let mut label_a : CppBox<QLabel> = QLabel::new();
   |                               ^^^^^^ not found in this scope

error[E0412]: cannot find type `CppBox` in this scope
  --> src/main.rs:96:26
   |
96 |         let mut window : CppBox<QWidget> = QWidget::new_0a();
   |                          ^^^^^^ not found in this scope

error[E0412]: cannot find type `CppBox` in this scope
  --> src/main.rs:99:26
   |
99 |         let mut layout : CppBox<QVBoxLayout> = QVBoxLayout::new_1a(&mut window);
   |                          ^^^^^^ not found in this scope

error: aborting due to 17 previous errors

Cargo.toml

[package]
name = "Qt-rs"
version = "0.1.0"
authors = ["ONiel"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
qt_core = "0.4.1"
qt_gui = "0.4.1"
qt_widgets = "0.4.1"
qt_ui_tools = "0.4.1"
qt_3d_core = "0.4.1"
qt_3d_render = "0.4.1"
qt_3d_input = "0.4.1"
qt_3d_logic = "0.4.1"
qt_3d_extras = "0.4.1"
qt_charts = "0.4.1"

cpp_core = "0.5.1"

It compiled and ran before the update.
Thanks!

Can you post your Cargo.toml here?

1 Like

Updated OP. Knew I had forgotten something. :wink:

It's not immediately obvious why it can't find the crate. I would suggest creating a new crate, that just has qt_widgets as a dependency and then just add a use statement importing something from it.

See if it still happens. If it doesn't you'll have to find what makes the difference. If it does you'll have a minimal reproduceable example you can put online for people to look at.

2 Likes

I indeed created a new project using cargo new and copy/pasted the code in there. It suddenly worked! Thanks.

Seems like it isn't a good idea to update Rust while working on a project though.

oh sorry, I didn't think about it but often cargo clean is a good idea with mysterious things like this.

1 Like

If you think something is messed up, cargo clean is your friend. If it isn't enough, remove the directory, re-clone the repository, and optionally reboot your computer.

3 Likes

It looks like your code is written using the Rust 2018 edition (because there are no extern crate imports), but your old Cargo.toml was missing edition = "2018" for some reason.

5 Likes

We strive to make it so that you never have to fear upgrading your Rust compiler. If you do upgrade your Rust compiler, haven't changed anything else, and your code stops compiling-- that is a bug we definitely want to know about!!

I agree with @mbrubeck's assessment though - are you absolutely positive you didn't change your project's Cargo.toml around when you were updating Rust?

Also, what Rust versions were you upgrading from and to?

2 Likes

@mbrubeck Was right. When I created this project few days ago I deleted the line edition = "2018" because I thought it meant the edition of my own project. I was like "It's 2020 already... wt*?". That's why I deleted it.

Didn't knew it was important. It works when the edition is specified.
It worked fine without specifying the edition when my Rust compiler matched the version of my project though.

This code requires edition="2018" on any version of Rust. (You can test this yourself by installing and running older versions using rustup.) Possibly it had already been compiled using the working manifest, and wasn’t actually rebuilt with the new manifest until after the toolchain upgrade.

2 Likes

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