The alpha version looks really nice and promising, but seems it ain't backward compatible.
I think that with that new release it's possible to code what I want though.
Code:
#![windows_subsystem = "windows"]
/*
use qt_widgets::{
cpp_core::{QBox, Ptr},
cpp_core,
qt_core::QString,
qt_core::slot, qt_core::SlotNoArgs,
QApplication, QLineEdit, QMessageBox, QPushButton, QVBoxLayout, QWidget,
QLabel,
qt_gui::QFont,
qt_core::QObject,
QLayoutItem,
qt_core,
};
use std::rc::Rc;
*/
use cpp_core::{Ptr, StaticUpcast};
use qt_core::{
q_init_resource, qs, slot, CheckState, ItemDataRole, QBox, QObject, QPtr,
QSortFilterProxyModel, SlotNoArgs, QString,
};
use qt_gui::{QStandardItem, QStandardItemModel, QFont};
use qt_ui_tools::ui_form;
use qt_widgets::{QApplication, QListView, QPushButton, QRadioButton, QWidget, QLabel, QLayoutItem, QVBoxLayout, };
use std::collections::BTreeSet;
use std::rc::Rc;
//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<QBox<QVBoxLayout>>,
// slot: Option<qt_core::slot>,
// switch_btn: Option<>
}
impl UserInterface {
//Constructor
pub fn new() -> Rc<Self> {
Rc::new(UserInterface {
// layout: None,
// slot: None,
})
}
//Build the labels, buttons,... for the startscreen
pub fn build_start_screen(self: Rc<Self>, window: cpp_core::Ptr<QWidget>) {
unsafe {
//Big fat font
let mut big_font = 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 : QBox<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 : QBox<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 : QBox<QPushButton> = QPushButton::new();
option_a_btn.set_text(&QString::from_std_str("Go option A"));
//self.slot = option_a_btn_clicked;
option_a_btn.clicked().connect(&self.option_a_btn_clicked(window));
//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::Ptr<QWidget>) {
unsafe {
self.reset(window);
//Label 'Cat Manager'
let mut label_a : QBox<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::Ptr<QWidget>) {
unsafe {
while window.layout().count() > 0 {
//Following gives a funny Error:
//let child : cpp_core::ptr::Ptr<QLayoutItem> = window.layout().take_at(0);
let child = window.layout().take_at(0);
window.layout().delete_later();
}
}
}
//OnClick Function
#[slot(SlotNoArgs)]
unsafe fn option_a_btn_clicked(self: &Rc<Self>, window: cpp_core::Ptr<QWidget>) {
self.build_option_a_screen(window);
}
}
fn main() {
QApplication::init(|_app| unsafe {
let mut window : QBox<QWidget> = QWidget::new_0a();
window.resize_2a(400, 250);
let mut layout : QBox<QVBoxLayout> = QVBoxLayout::new_1a(&mut window);
let mut ui : Rc<UserInterface> = UserInterface::new();
let window_ptr = window.as_raw_ptr();
ui.build_start_screen(window_ptr);
window.show();
QApplication::exec()
})
}
Tons of errors:
Compiling Qt-rs2 v0.1.0 (/Users/niel/School/Rust/rust_gui/Qt-rs2)
warning: unused imports: `Ptr`, `StaticUpcast`
--> src/main.rs:21:16
|
21 | use cpp_core::{Ptr, StaticUpcast};
| ^^^ ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unused imports: `CheckState`, `ItemDataRole`, `QObject`, `QPtr`, `QSortFilterProxyModel`, `q_init_resource`, `qs`
--> src/main.rs:23:5
|
23 | q_init_resource, qs, slot, CheckState, ItemDataRole, QBox, QObject, QPtr,
| ^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^ ^^^^
24 | QSortFilterProxyModel, SlotNoArgs, QString,
| ^^^^^^^^^^^^^^^^^^^^^
warning: unused imports: `QStandardItemModel`, `QStandardItem`
--> src/main.rs:26:14
|
26 | use qt_gui::{QStandardItem, QStandardItemModel, QFont};
| ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
warning: unused import: `qt_ui_tools::ui_form`
--> src/main.rs:27:5
|
27 | use qt_ui_tools::ui_form;
| ^^^^^^^^^^^^^^^^^^^^
warning: unused imports: `QLayoutItem`, `QListView`, `QRadioButton`
--> src/main.rs:28:32
|
28 | use qt_widgets::{QApplication, QListView, QPushButton, QRadioButton, QWidget, QLabel, QLayoutItem, QVBoxLayout, };
| ^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^
warning: unused import: `std::collections::BTreeSet`
--> src/main.rs:29:5
|
29 | use std::collections::BTreeSet;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0277]: the trait bound `&(): qt_core::connect::AsReceiver` is not satisfied
--> src/main.rs:74:44
|
74 | option_a_btn.clicked().connect(&self.option_a_btn_clicked(window));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `qt_core::connect::AsReceiver` is not implemented for `&()`
error[E0277]: the trait bound `cpp_core::ptr::Ptr<qt_core::QObject>: cpp_core::convert::CastFrom<cpp_core::ptr::Ptr<UserInterface>>` is not satisfied
--> src/main.rs:114:5
|
114 | #[slot(SlotNoArgs)]
| ^^^^^^^^^^^^^^^^^^^ the trait `cpp_core::convert::CastFrom<cpp_core::ptr::Ptr<UserInterface>>` is not implemented for `cpp_core::ptr::Ptr<qt_core::QObject>`
|
::: /Users/niel/.cargo/registry/src/github.com-1ecc6299db9ec823/qt_core-0.5.0-alpha.2/src/lib.rs:249066:22
|
249066 | parent: impl ::cpp_core::CastInto<::cpp_core::Ptr<crate::QObject>>,
| ----------------------------------------------------- required by this bound in `qt_core::SlotNoArgs::new`
|
= help: the following implementations were found:
<cpp_core::ptr::Ptr<T> as cpp_core::convert::CastFrom<&'a cpp_core::cpp_box::CppBox<U>>>
<cpp_core::ptr::Ptr<T> as cpp_core::convert::CastFrom<&'a qt_core::q_box::QBox<U>>>
<cpp_core::ptr::Ptr<T> as cpp_core::convert::CastFrom<&'a qt_core::q_ptr::QPtr<U>>>
<cpp_core::ptr::Ptr<T> as cpp_core::convert::CastFrom<*const U>>
and 5 others
= note: required because of the requirements on the impl of `cpp_core::convert::CastInto<cpp_core::ptr::Ptr<qt_core::QObject>>` for `cpp_core::ptr::Ptr<UserInterface>`
error[E0593]: closure is expected to take 0 arguments, but it takes 1 argument
--> src/main.rs:114:5
|
114 | #[slot(SlotNoArgs)]
| ^^^^^^^^^^^^^^^^^^^
| |
| expected closure that takes 0 arguments
| takes 1 argument
error[E0277]: the trait bound `cpp_core::ptr::Ptr<qt_widgets::QWidget>: cpp_core::convert::CastFrom<&mut qt_core::q_box::QBox<qt_widgets::QWidget>>` is not satisfied
--> src/main.rs:125:46
|
125 | let mut layout : QBox<QVBoxLayout> = QVBoxLayout::new_1a(&mut window);
| ^^^^^^^^^^^^^^^^^^^ the trait `cpp_core::convert::CastFrom<&mut qt_core::q_box::QBox<qt_widgets::QWidget>>` is not implemented for `cpp_core::ptr::Ptr<qt_widgets::QWidget>`
|
::: /Users/niel/.cargo/registry/src/github.com-1ecc6299db9ec823/qt_widgets-0.5.0-alpha.2/src/lib.rs:38163:22
|
38163 | parent: impl ::cpp_core::CastInto<::cpp_core::Ptr<crate::QWidget>>,
| ----------------------------------------------------- required by this bound in `qt_widgets::QVBoxLayout::new_1a`
|
= help: the following implementations were found:
<cpp_core::ptr::Ptr<T> as cpp_core::convert::CastFrom<&'a cpp_core::cpp_box::CppBox<U>>>
<cpp_core::ptr::Ptr<T> as cpp_core::convert::CastFrom<&'a qt_core::q_box::QBox<U>>>
<cpp_core::ptr::Ptr<T> as cpp_core::convert::CastFrom<&'a qt_core::q_ptr::QPtr<U>>>
<cpp_core::ptr::Ptr<T> as cpp_core::convert::CastFrom<*const U>>
and 5 others
= note: required because of the requirements on the impl of `cpp_core::convert::CastInto<cpp_core::ptr::Ptr<qt_widgets::QWidget>>` for `&mut qt_core::q_box::QBox<qt_widgets::QWidget>`
error[E0308]: mismatched types
--> src/main.rs:129:31
|
129 | ui.build_start_screen(window_ptr);
| ^^^^^^^^^^ expected struct `cpp_core::ptr::Ptr`, found *-ptr
|
= note: expected struct `cpp_core::ptr::Ptr<qt_widgets::QWidget>`
found raw pointer `*const qt_widgets::QWidget`
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0277, E0308, E0593.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `Qt-rs2`.
To learn more, run the command again with --verbose.
Cargo.toml:
[package]
name = "Qt-rs2"
version = "0.1.0"
authors = ["Niel"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
qt_widgets = "0.5.0-alpha.2"
qt_core = "0.5.0-alpha.2"
cpp_core = "0.5.0-alpha.2"
qt_ui_tools = "0.5.0-alpha.2"
qt_gui = "0.5.0-alpha.2"
Tried fixing those errors but that seems a lost effort/impossible mission.
Next week I'll try to do the same starting from scratch and by basing me more on the example you posted.
But I don't know man... It's really a thousand times more complicated than C++.