implement table (QT)

Thu, 24 Apr 2025 21:19:29 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 24 Apr 2025 21:19:29 +0200
changeset 582
6c86efe60b37
parent 581
7486347f73cf
child 583
30083c6cb198

implement table (QT)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/list.cpp file | annotate | diff | comparison | revisions
ui/qt/model.cpp file | annotate | diff | comparison | revisions
ui/qt/model.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Tue Apr 22 20:40:01 2025 +0200
+++ b/application/main.c	Thu Apr 24 21:19:29 2025 +0200
@@ -920,6 +920,15 @@
     printf("selection [%d]\n", sel->rows[0]);
 }
 
+void* table_getvalue(void *elm, int col) {
+    char *s = elm;
+    if(col == 0) {
+        return s;
+    } else {
+        return "0";
+    }
+}
+
 void application_startup(UiEvent *event, void *data) {
     UiObject *obj = ui_window("My Window", NULL);
     
@@ -937,7 +946,12 @@
         ui_newline(obj);
         
         ui_textarea(obj, .vexpand = TRUE, .vfill = TRUE);
-        ui_listview(obj, .varname = "mylist", .colspan = 2, .fill = TRUE, .onactivate = list_activate);
+        
+        UiModel *model = ui_model(obj->ctx, UI_STRING, "col1", UI_INTEGER, "col2", -1);
+        model->columnsize[0] = -1;
+        model->getvalue = table_getvalue;
+        ui_table(obj, .model = model, .varname = "mylist", .colspan = 2, .fill = UI_ON, .multiselection = TRUE, .fill = TRUE, .onactivate = list_activate);
+        
         ui_newline(obj);
         
         ui_button(obj, .label = "Button Y");
--- a/ui/qt/list.cpp	Tue Apr 22 20:40:01 2025 +0200
+++ b/ui/qt/list.cpp	Thu Apr 24 21:19:29 2025 +0200
@@ -73,3 +73,40 @@
     
     return view;
 }
+
+UIWIDGET ui_table_create(UiObject* obj, UiListArgs args) {
+    UiContainerPrivate *ctn = ui_obj_container(obj);
+    UI_APPLY_LAYOUT(ctn->layout, args);
+    
+    QTreeView *view = new QTreeView();
+    view->setItemsExpandable(false);
+    view->setRootIsDecorated(false);   
+    
+    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.list, args.varname, UI_VAR_LIST);
+    
+    TableModel *model = new TableModel(obj, view, var, args.model);
+    view->setModel(model);
+    
+    if(var) {
+        UiList *list = (UiList*)var->value;
+        list->update = ui_listmodel_update;
+        list->getselection = ui_listmodel_getselection;
+        list->setselection = ui_listmodel_setselection;
+        list->obj = model;
+    }
+    
+    model->setActivationCallback(args.onactivate, args.onactivatedata);
+    model->setSelectionCallback(args.onselection, args.onselectiondata);
+    
+    QItemSelectionModel *s = view->selectionModel();
+    QObject::connect(
+            s,
+            SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
+            model,
+            SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
+    
+    
+    ctn->add(view, false);
+    
+    return view;
+}
--- a/ui/qt/model.cpp	Tue Apr 22 20:40:01 2025 +0200
+++ b/ui/qt/model.cpp	Thu Apr 24 21:19:29 2025 +0200
@@ -99,11 +99,11 @@
 
 
 
-TableModel::TableModel(UiObject *obj, QTreeView *view, UiVar *var, ui_getvaluefunc getvalue){
+TableModel::TableModel(UiObject *obj, QTreeView *view, UiVar *var, UiModel *model){
     this->obj = obj;
     this->view = view;
     this->var = var;
-    this->getvalue = getvalue;
+    this->model = model;
     this->onactivate = nullptr;
     this->onactivatedata = nullptr;
     this->onselection = nullptr;
@@ -134,18 +134,30 @@
     return ui_list_count(list);
 }
 
+int TableModel::columnCount(const QModelIndex &parent) const {
+    return model->columns;
+}
+
 QVariant TableModel::data(const QModelIndex &index, int role) const {
     if(role == Qt::DisplayRole) {
         UiList *ls = (UiList*)var->value;
         void *rowData = ls->get(ls, index.row());
-        if(rowData && getvalue) {
-            void *value = getvalue(rowData, 0);
+        if(rowData && model->getvalue) {
+            void *value = model->getvalue(rowData, index.column());
             return QString::fromUtf8((char*)value); 
         }
     }
     return QVariant();
 }
 
+QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
+    if(role == Qt::DisplayRole) {
+        char *label = model->titles[section];
+        return QString::fromUtf8(label);
+    }
+    return QVariant();
+}
+
 void TableModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
     UiListSelection sel = ui_selection_model_to_selection(view->selectionModel());
     
--- a/ui/qt/model.h	Tue Apr 22 20:40:01 2025 +0200
+++ b/ui/qt/model.h	Thu Apr 24 21:19:29 2025 +0200
@@ -74,7 +74,7 @@
 class TableModel : public QAbstractListModel {
     Q_OBJECT
     
-    ui_getvaluefunc getvalue;
+    UiModel     *model;
     ui_callback onactivate;
     void        *onactivatedata;
     ui_callback onselection;
@@ -85,7 +85,7 @@
     UiVar       *var;
     QTreeView   *view;
     
-    TableModel(UiObject *obj, QTreeView *view, UiVar *var, ui_getvaluefunc getvalue);
+    TableModel(UiObject *obj, QTreeView *view, UiVar *var, UiModel *model);
     
     void setActivationCallback(ui_callback f, void *userdata);
     void setSelectionCallback(ui_callback f, void *userdata);
@@ -93,7 +93,9 @@
     void update(int row);
     
     int rowCount(const QModelIndex &parent = QModelIndex()) const;
+    int columnCount(const QModelIndex &parent = QModelIndex()) const;
     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
     
 public slots:
     void selectionChanged(

mercurial