Comments (1)
I solved the issue...
The "form" QWidget must be declared in the "MyWindow" QMainWindow as follow :
#[derive(Debug)]
struct MyWindow {
window: QBox<QMainWindow>,
centralwidget: QBox<QWidget>,
gridlayout: QBox<QGridLayout>,
table: QBox<QTableWidget>,
verticallayout: QBox<QVBoxLayout>,
button_vec: Vec<Ptr<QPushButton>>,
model_vec: Vec<Ptr<QStandardItemModel>>,
form_vec: Vec<Rc<Form>>,
col: i32,
row: i32,
}
The goal of this project id to create a kind of "Excel" like filter.
Here is the complete code :
#![windows_subsystem = "windows"]
#![allow(unused)]
use cpp_core::{Ptr, Ref, StaticUpcast,};
use cpp_core::{CppBox};
use qt_core::{
q_init_resource, qs, slot, ScrollBarPolicy, CheckState, QVectorOf, QBox, QObject, QPtr, QPointF, SlotOfInt,
SlotNoArgs, QFlags, WindowType, WindowModality, AlignmentFlag, QCoreApplication, Orientation, QStringList,
q_state_machine::SignalEvent, QListOfQVariant, QItemSelectionModel, QModelIndex, QListOfQModelIndex, QVariant, QString,
};
use qt_gui::{QStandardItem, QStandardItemModel, QPen, QBrush, QColor, q_painter::RenderHint, SlotOfQStandardItem,
};
use qt_widgets::{q_abstract_scroll_area, QApplication, QMainWindow, QListOfQWidget, QTableWidget, QTableWidgetItem,
QHeaderView, QPushButton, QWidget, QFrame, QVBoxLayout, QGridLayout, QTreeView, QHBoxLayout,
};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use itertools::Itertools;
use std::ptr;
///////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)]
struct Form {
widget: QBox<QWidget>,
gridlayout: QBox<QGridLayout>,
treeview: QBox<QTreeView>,
verticallayout: QBox<QVBoxLayout>,
horizontallayout: QBox<QHBoxLayout>,
enterbutton: QBox<QPushButton>,
cancelbutton: QBox<QPushButton>,
}
impl StaticUpcast<QObject> for Form {
unsafe fn static_upcast(ptr: Ptr<Self>) -> Ptr<QObject> {
ptr.widget.as_ptr().static_upcast()
}
}
impl Form {
fn new() -> Rc<Form> {
unsafe {
let widget = QWidget::new_0a();
widget.set_window_flags(QFlags::from(WindowType::FramelessWindowHint));
widget.set_window_modality(WindowModality::ApplicationModal);
let gridlayout = QGridLayout::new_1a(&widget);
let verticallayout = QVBoxLayout::new_0a();
let treeview = QTreeView::new_1a(&widget);
verticallayout.add_widget_1a(&treeview);
let horizontallayout = QHBoxLayout::new_0a();
let enterbutton = QPushButton::new();
enterbutton.set_text(&qs("Enter"));
horizontallayout.add_widget(&enterbutton);
let cancelbutton = QPushButton::new();
cancelbutton.set_text(&qs("Cancel"));
horizontallayout.add_widget(&cancelbutton);
verticallayout.add_layout_1a(&horizontallayout);
gridlayout.add_layout_5a(&verticallayout, 0, 0, 1, 1);
// widget.show();
let this = Rc::new(Self {
widget,
gridlayout,
verticallayout,
treeview,
horizontallayout,
enterbutton,
cancelbutton,
});
this.init();
this
}
}
unsafe fn init(self: &Rc<Self>) {
// self.treeview.slot_data_changed().connect(&self.slot_on_item_changed());
self.enterbutton.clicked().connect(&self.slot_on_enter_clicked());
self.cancelbutton.clicked().connect(&self.slot_on_cancel_clicked());
}
#[slot(SlotNoArgs)]
unsafe fn on_enter_clicked(self: &Rc<Self>) {
println!("enter clicked");
// Filtering function must be located here
self.widget.close();
}
#[slot(SlotNoArgs)]
unsafe fn on_cancel_clicked(self: &Rc<Self>) {
println!("cancel clicked");
self.widget.close();
}
// #[slot(SlotNoArgs)]
// unsafe fn on_item_changed(self: &Rc<Self>) {
// println!("item changed");
// }
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)]
struct MyWindow {
window: QBox<QMainWindow>,
centralwidget: QBox<QWidget>,
gridlayout: QBox<QGridLayout>,
table: QBox<QTableWidget>,
verticallayout: QBox<QVBoxLayout>,
button_vec: Vec<Ptr<QPushButton>>,
model_vec: Vec<Ptr<QStandardItemModel>>,
form_vec: Vec<Rc<Form>>,
col: i32,
row: i32,
form_index: Arc<Mutex<i32>>,
// old_vec: Arc<Mutex<Vec<Ptr<QVariant>>>>,
// new_vec: Arc<Mutex<Vec<Ptr<QVariant>>>>,
}
impl StaticUpcast<QObject> for MyWindow {
unsafe fn static_upcast(ptr: Ptr<Self>) -> Ptr<QObject> {
ptr.window.as_ptr().static_upcast()
}
}
impl MyWindow {
fn new() -> Rc<MyWindow> {
unsafe {
let row: i32 = 11;
let col: i32 = 3;
let form_index = Arc::new(Mutex::new(0));
let window = QMainWindow::new_0a();
window.resize_2a(400, 400);
let centralwidget = QWidget::new_1a(&window);
let gridlayout = QGridLayout::new_1a(¢ralwidget);
let verticallayout = QVBoxLayout::new_0a();
gridlayout.add_layout_5a(&verticallayout, 0, 0, 2, 2);
window.set_central_widget(¢ralwidget);
let table = QTableWidget::new_0a();
verticallayout.add_widget(&table);
table.set_row_count(row);
table.set_column_count(col);
table.horizontal_header().set_stretch_last_section(true);
table.vertical_header().set_stretch_last_section(true);
table.set_alternating_row_colors(true);
// let new_vec = Arc::new(Mutex::new(Vec::new()));
// let old_vec = Arc::new(Mutex::new(Vec::new()));
let my_string_array = [
["Column", "Unit", "Test Group"],
["A", "1", "a"],
["A", "1", "b"],
["A", "1", "c"],
["A", "2", "a"],
["B", "1", "a"],
["B", "2", "c"],
["B", "3", "a"],
["C", "1", "a"],
["C", "2", "c"],
["D", "1", "c"]
];
// Fill in table
for i in 1..row {
for j in 0..col {
let item = QTableWidgetItem::new().into_ptr();
item.set_text(&qs(my_string_array[i as usize][j as usize].to_string()));
table.set_item(i, j, item);
}
}
// Create choices array
let mut choices_2d: Vec<Vec<String>> = Vec::new();
for i in 0..col {
let mut my_col: Vec<String> = Vec::new();
for j in 1..row {
my_col.push(my_string_array[j as usize][i as usize].to_string());
}
let choices = my_col.iter().cloned().unique().collect_vec();
choices_2d.push(choices);
}
// Create QPushButton array and place QPushButton in the cell
let mut button_vec = Vec::new();
for i in 0..col {
let button = QPushButton::new().into_ptr();
button.set_object_name((&qs(i.to_string())));
button.set_text(&qs(my_string_array[0][i as usize]));
table.set_cell_widget(0,i,button);
button_vec.push(button);
}
// Create QStandardItemModel array
let mut model_vec = Vec::new();
for i in 0..col {
let model = QStandardItemModel::new_0a().into_ptr();
model_vec.push(model);
}
// Fill in the QStandardItemModel(s) with choices items
for i in 0..col {
let nrow = choices_2d[i as usize].len();
let item = QStandardItem::new().into_ptr();
item.set_text(&qs("Select All".to_string()));
item.set_checkable(true);
item.set_check_state(CheckState::Checked);
model_vec[i as usize].append_row_q_standard_item(item);
for j in 0..nrow {
let item = QStandardItem::new().into_ptr();
item.set_text(&qs(choices_2d[i as usize][j as usize].to_string()));
item.set_checkable(true);
item.set_check_state(CheckState::Checked);
model_vec[i as usize].append_row_q_standard_item(item);
}
}
// Create Form array
let mut form_vec = Vec::new();
for i in 0..col {
let form = Form::new();
form.treeview.set_model(model_vec[i as usize]);
form_vec.push(form);
}
window.show();
let this = Rc::new(Self {
window,
centralwidget,
gridlayout,
table,
verticallayout,
button_vec,
model_vec,
form_vec,
// old_vec,
// new_vec,
col,
row,
form_index,
});
this.init();
this
}
}
unsafe fn init(self: &Rc<Self>) {
for i in 0..self.col {
self.button_vec[i as usize].clicked().connect(&self.slot_on_button_clicked());
self.model_vec[i as usize].item_changed().connect(&self.slot_on_item_changed());
}
}
#[slot(SlotNoArgs)]
unsafe fn on_button_clicked(self: &Rc<Self>) {
*self.form_index.lock().unwrap() = self.table.current_column() as i32;
let form_index = *self.form_index.lock().unwrap();
self.form_vec[form_index as usize].widget.show();
}
#[slot(SlotOfQStandardItem)]
unsafe fn on_item_changed(self: &Rc<Self>, item_clicked: Ptr<QStandardItem>) {
let form_index: i32 = *self.form_index.lock().unwrap();
let mut item = QStandardItem::new().into_ptr();
if item_clicked.text().to_std_string() == "Select All".to_string() && item_clicked.check_state() == CheckState::Unchecked {
for i in 0..self.model_vec[form_index as usize].row_count_0a()
{
item = self.model_vec[form_index as usize].item_1a(i);
item.set_check_state(CheckState::Unchecked);
}
println!("Unselecting All");
}
if item_clicked.text().to_std_string() == "Select All".to_string() && item_clicked.check_state() == CheckState::Checked {
for i in 0..self.model_vec[form_index as usize].row_count_0a()
{
item = self.model_vec[form_index as usize].item_1a(i);
item.set_check_state(CheckState::Checked);
}
println!("Selecting All");
}
else {
println!("{:?} was clicked", item_clicked.text().to_std_string());
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
fn main() {
QApplication::init(|_| unsafe {
let todo_widget = MyWindow::new();
unsafe { QApplication::exec() }
})
}
from ritual.
Related Issues (20)
- Broken doc link
- MSVC 32bit HOT 1
- MSVC 32-bit surpport HOT 1
- Protected values inaccessible
- QFlags generation fails across crate boundaries
- Help - VSCode auto completion? HOT 1
- Cannot run examples HOT 5
- Ritual doesn't compile. Errors when compiling "amq-proto" HOT 1
- Can't find qmake because it doesn't exist
- looking for SignalOfQlistOfQPointF and SlotOfQlistOfQPointF...
- Unknown Qt Version 5.15.x
- Request for better errors when command is not available (cmake)
- [Question] how to make it safe?
- How to build qt shared library ?
- Exit code 2 from CMake HOT 1
- linux arm 32 supper
- WASM Support HOT 2
- missing `invokeMethod` function for `QMetaObject` HOT 2
- Build other qt libraries with ritual?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ritual.