I have a simple GUI with a label and a button. But when the button is clicked I want the label and button removed to show a new label with other text.
This to emulate different 'pages' in my GUI application. I have read about StackedLayout etc but I don't understand how to implement that in Rust. In this C++ documentation they use one QStackedLayout and multiple widgets, each widget representing a different page. But I can't do that in Rust because I can only have one widget because it needs to be upcasted etc (lifetimes issues and stuff).
I tried overriding the layout the widget uses on line 76, but that doesn't work. Compiles though.
I have no clue in how to do it. I am using the Qt-rust binding 0.5.0.
This is my code:
#![windows_subsystem = "windows"]
//Importing the required stuff
use cpp_core::{Ptr, Ref, StaticUpcast};
use qt_core::{qs, slot, ContextMenuPolicy, QBox, QObject, QPoint, SlotNoArgs, QString};
use qt_widgets::{
QAction, QApplication, QLineEdit, QMenu, QMessageBox, QPushButton, QLabel, QTableWidget,
QTableWidgetItem, QVBoxLayout, QWidget, SlotOfQPoint, SlotOfQTableWidgetItemQTableWidgetItem,
};
use std::rc::Rc;
//Struct for Main window
struct MainWindow {
widget: QBox<QWidget>,
button: QBox<QPushButton>,
}
//From form-struct to base-struct ???
impl StaticUpcast<QObject> for MainWindow {
unsafe fn static_upcast(ptr: Ptr<Self>) -> Ptr<QObject> {
ptr.widget.as_ptr().static_upcast()
}
}
//Implementing functions for MainWindow
impl MainWindow {
//Defining all the graphical elements in MainWindow
//Returns the MainWindow in a Reference Counted Smart Pointer so it can have multiple owners
//and doesn't get destroyed until it has no owners anymore
fn new() -> Rc<MainWindow> {
unsafe {
//Widget
let widget = QWidget::new_0a();
let layout = QVBoxLayout::new_1a(&widget);
//Label
let label = QLabel::new();
label.set_text(&qs("Label A!"));
layout.add_widget(&label);
//Button
let button = QPushButton::from_q_string(&qs("Click me."));
layout.add_widget(&button);
widget.show();
let this = Rc::new(Self {
widget,
button,
});
this.init();
this
}
}
//Binding functions/slots to elements etc
unsafe fn init(self: &Rc<Self>) {
self.button
.clicked()
.connect(&self.slot_build_option_a());
}
//Slots/Functions for buttons
#[slot(SlotNoArgs)]
unsafe fn build_option_a(self: &Rc<Self>) {
/*
while (self.widget.layout().count() > 0) {
self.widget.layout().remove_item(
self.widget.layout().take_at(0)
);
println!("{}", "Test 123...");
}*/
let layout = QVBoxLayout::new_0a();
self.widget.set_layout(&layout);
//Label
let label = QLabel::new();
label.set_text(&qs("Label B!"));
layout.add_widget(&label);
}
}
fn main() {
QApplication::init(|_| unsafe {
let _mw = MainWindow::new();
QApplication::exec()
})
}
I