add combobox (QT)

Sun, 07 Dec 2025 11:27:44 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 11:27:44 +0100
changeset 958
749a8a36d74b
parent 957
774ee4d7bb1f
child 959
4b2202df31ec

add combobox (QT)

application/demo_bindings.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/demo_bindings.c	Sun Dec 07 11:07:29 2025 +0100
+++ b/application/demo_bindings.c	Sun Dec 07 11:27:44 2025 +0100
@@ -32,7 +32,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifndef UI_WIN32
+#if !defined(UI_WIN32) && !defined(UI_QT)
 
 Document* document_create(int id) {
     Document* doc = ui_document_new(sizeof(Document));
@@ -175,12 +175,18 @@
 
 #ifndef UI_WIN32
 
+#ifdef UI_QT
+int main(int argc, char **argv) {
+    return 0;
+}
+#else
 int main(int argc, char **argv) {
     ui_init(NULL, argc, argv);
     ui_onstartup(application_startup, NULL);
     ui_main();
     return 0;
 }
+#endif
 
 #else
 
--- a/ui/qt/list.cpp	Sun Dec 07 11:07:29 2025 +0100
+++ b/ui/qt/list.cpp	Sun Dec 07 11:27:44 2025 +0100
@@ -32,6 +32,7 @@
 #include <QTreeView>
 #include <QTreeWidgetItem>
 #include <QListView>
+#include <QComboBox>
 
 extern "C" void* ui_strmodel_getvalue(void *elm, int column) {
     return column == 0 ? elm : NULL;
@@ -46,7 +47,7 @@
     return NULL;
 }
 
-UIWIDGET ui_listview_create(UiObject* obj, UiListArgs *args) {
+UIWIDGET ui_listview_create(UiObject *obj, UiListArgs *args) {
     UiContainerPrivate *ctn = ui_obj_container(obj);
     
     QListView *view = new QListView();
@@ -92,7 +93,7 @@
     return view;
 }
 
-UIWIDGET ui_table_create(UiObject* obj, UiListArgs *args) {
+UIWIDGET ui_table_create(UiObject *obj, UiListArgs *args) {
     UiContainerPrivate *ctn = ui_obj_container(obj);
     
     QTreeView *view = new QTreeView();
@@ -141,3 +142,42 @@
     
     return view;
 }
+
+UIWIDGET ui_combobox_create(UiObject *obj, UiListArgs *args) {
+    UiContainerPrivate *ctn = ui_obj_container(obj);
+    
+    QComboBox *view = new QComboBox();
+    UiLayout layout = UI_ARGS2LAYOUT(args);
+    ctn->add(view, layout);
+    
+    ui_getvaluefunc2 getvalue = nullptr;
+    void *getvaluedata = nullptr;
+    if(args->getvalue2) {
+        getvalue = args->getvalue2;
+        getvaluedata = args->getvalue2data;
+    } else if(args->getvalue) {
+        getvalue = getvalue_wrapper;
+        getvaluedata = (void*)args->getvalue;
+    } else {
+        getvalue = getvalue_wrapper;
+        getvaluedata = (void*)ui_strmodel_getvalue;
+    }
+    
+    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->list, args->varname, UI_VAR_LIST);
+    
+    ListModel *model = new ListModel(obj, view, var, getvalue, getvaluedata);
+    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);
+    
+    return view;
+}
--- a/ui/qt/model.cpp	Sun Dec 07 11:07:29 2025 +0100
+++ b/ui/qt/model.cpp	Sun Dec 07 11:27:44 2025 +0100
@@ -29,9 +29,23 @@
 #include "model.h"
 
 
-ListModel::ListModel(UiObject *obj, QListView *view, UiVar *var, ui_getvaluefunc2 getvalue, void *getvaluedata){
+ListModel::ListModel(UiObject *obj, QListView *view, UiVar *var, ui_getvaluefunc2 getvalue, void *getvaluedata) {
     this->obj = obj;
-    this->view = view;
+    this->listview = view;
+    this->combobox = nullptr;
+    this->var = var;
+    this->getvalue = getvalue;
+    this->getvaluedata = getvaluedata;
+    this->onactivate = nullptr;
+    this->onactivatedata = nullptr;
+    this->onselection = nullptr;
+    this->onselectiondata = nullptr;
+}
+
+ListModel::ListModel(UiObject *obj, QComboBox *view, UiVar *var, ui_getvaluefunc2 getvalue, void *getvaluedata) {
+    this->obj = obj;
+    this->combobox = view;
+    this->listview = nullptr;
     this->var = var;
     this->getvalue = getvalue;
     this->getvaluedata = getvaluedata;
@@ -85,7 +99,12 @@
 }
 
 void ListModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
-    UiListSelection sel = ui_selection_model_to_selection(view->selectionModel());
+    UiListSelection sel;
+    if(listview) {
+        sel = ui_selection_model_to_selection(listview->selectionModel());
+    } else {
+        // TODO
+    }
     
     UiEvent event;
     event.obj = obj;
@@ -257,10 +276,19 @@
         QModelIndex index = model->index(sel.rows[i]);
         selection.select(index, index);
     }
-    model->view->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
+    if(model->listview) {
+        model->listview->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
+    } else if(model->combobox) {
+        // TODO
+    }
 }
 
 UiListSelection ui_listmodel_getselection(UiList *list) {
     ListModel *model = (ListModel*)list->obj;
-    return ui_selection_model_to_selection(model->view->selectionModel());
+    if(model->listview) {
+        return ui_selection_model_to_selection(model->listview->selectionModel());
+    } else {
+        UiListSelection sel = { 0, NULL };
+        return sel;
+    }
 }
--- a/ui/qt/model.h	Sun Dec 07 11:07:29 2025 +0100
+++ b/ui/qt/model.h	Sun Dec 07 11:27:44 2025 +0100
@@ -34,6 +34,7 @@
 #include "../common/context.h"
 #include <QListView>
 #include <QTreeView>
+#include <QComboBox>
 #include <QAbstractListModel>
 #include <QAbstractTableModel>
 #include <QAbstractItemModel>
@@ -54,9 +55,11 @@
 public:
     UiObject    *obj;
     UiVar       *var;
-    QListView   *view;
+    QListView   *listview;
+    QComboBox   *combobox;
     
     ListModel(UiObject *obj, QListView *view, UiVar *var, ui_getvaluefunc2 getvalue, void *getvaluedata);
+    ListModel(UiObject *obj, QComboBox *view, UiVar *var, ui_getvaluefunc2 getvalue, void *getvaluedata);
     
     void setActivationCallback(ui_callback f, void *userdata);
     void setSelectionCallback(ui_callback f, void *userdata);

mercurial