Hello
I'm trying to delete all the buttons and labels from a layout and afterwards I delete the layout as well. This to add a new layout with other buttons and labels.
This is the relevant code (executed after button click):
//DELETING all buttons, labels,...
while self.widget.layout().count() > 0 {
self.widget.layout().remove_item(
self.widget.layout().item_at(0)
);
println!("{}", "Deleted something...");
}
//ATTEMPTS to delete the layout:
//self.widget.as_mut_raw_ref().unwrap().layout().delete_later();
//self.widget.layout().delete_later();
//std::mem::drop(self.widget.as_mut_raw_ref().unwrap().layout());
//std::mem::drop(self.widget.layout());
//self.widget.layout().delete();
//self.widget.as_mut_raw_ref().unwrap().layout().delete();
//self.widget.layout().as_ref().unwrap().delete();
//self.widget.[...].layout().as_ref().unwrap().delete()
//self.widget.layout().as_ref().unwrap().delete()
self.widget.as_mut_raw_ref().unwrap().layout().as_ref().unwrap().delete();
//Building new layout etc
let layout = QVBoxLayout::new_1a(&self.widget);
//Label
let label = QLabel::new();
label.set_text(&qs("Label B!"));
layout.add_widget(&label);
//self.widget.as_mut_raw_ref().unwrap().set_layout(&layout);
It is compiling, and when I click the button Label B is added. But the problem is that the other labels and buttons are also still visible...
Full code & Cargo.toml:
#![windows_subsystem = "windows"]
//Importing the required stuff
use cpp_core::{Ptr, Ref, StaticUpcast, CppDeletable};
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;
use std::cell::RefCell;
//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: &Self) {
//DELETING all buttons, labels,...
while self.widget.layout().count() > 0 {
self.widget.layout().remove_item(
self.widget.layout().item_at(0)
);
println!("{}", "Deleted something...");
}
//ATTEMPTS to delete the layout:
//self.widget.as_mut_raw_ref().unwrap().layout().delete_later();
//self.widget.layout().delete_later();
//std::mem::drop(self.widget.as_mut_raw_ref().unwrap().layout());
//std::mem::drop(self.widget.layout());
//self.widget.layout().delete();
//self.widget.as_mut_raw_ref().unwrap().layout().delete();
//self.widget.layout().as_ref().unwrap().delete();
//self.widget.[...].layout().as_ref().unwrap().delete()
//self.widget.layout().as_ref().unwrap().delete()
self.widget.as_mut_raw_ref().unwrap().layout().as_ref().unwrap().delete();
//Building new layout etc
let layout = QVBoxLayout::new_1a(&self.widget);
//Label
let label = QLabel::new();
label.set_text(&qs("Label B!"));
layout.add_widget(&label);
//self.widget.as_mut_raw_ref().unwrap().set_layout(&layout);
}
}
fn main() {
QApplication::init(|_| unsafe {
let _mw = MainWindow::new();
QApplication::exec()
})
}
[package]
name = "qt_rs_3"
version = "0.1.0"
authors = ["ONiel"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cpp_core = "0.6.0"
qt_core = "0.5.0"
qt_gui = "0.5.0"
qt_qml = "0.5.0"
qt_widgets = "0.5.0"
Video to show what's wrong:
When the button is clicked Label A and the Click-me button should disappear, so only Label B is visible. Now the initial label and button are still visible...
EDIT:
Solved. Working code for people with the same question reading this thread via Google:
Related questions: