Add combo box
authorKevin Krammer <kevin.krammer@kdab.com>
Wed, 13 Mar 2013 14:58:19 +0000 (15:58 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Wed, 13 Mar 2013 14:58:30 +0000 (15:58 +0100)
examples/gallery.qml
lib/declarativecomboboxextension.cpp [new file with mode: 0644]
lib/declarativecomboboxextension_p.h [new file with mode: 0644]
lib/declarativewidgetsdocument.cpp
lib/lib.pro

index 4fd9257..942612d 100644 (file)
@@ -18,6 +18,7 @@
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
 
+import QtCore 1.0
 import QtGui 1.0
 
 TabWidget {
@@ -137,6 +138,14 @@ TabWidget {
 
       DateTimeEdit {}
 
+      ComboBox {
+        model: StringListModel {
+          stringList: [ qsTr("Entry 1"), qsTr("Entry 2"), qsTr("Entry 3") ]
+        }
+
+        onCurrentIndexChanged: console.log( "combobox current index=" + currentIndex + ", text=" + currentText );
+      }
+
       Dial {}
 
       ScrollBar {
diff --git a/lib/declarativecomboboxextension.cpp b/lib/declarativecomboboxextension.cpp
new file mode 100644 (file)
index 0000000..4104f9d
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+  Copyright (C) 2012-2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
+  Author: Kevin Krammer, kevin.krammer@kdab.com
+  Author: Tobias Koenig, tobias.koenig@kdab.com
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along
+  with this program; if not, write to the Free Software Foundation, Inc.,
+  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#include "declarativecomboboxextension_p.h"
+
+#include <QComboBox>
+
+DeclarativeComboBoxExtension::DeclarativeComboBoxExtension(QObject *parent)
+  : DeclarativeWidgetExtension(parent)
+{
+}
+
+QComboBox *DeclarativeComboBoxExtension::extendedComboBox() const
+{
+  QComboBox *comboBox = qobject_cast<QComboBox*>(extendedWidget());
+  Q_ASSERT(comboBox);
+
+  return comboBox;
+}
+#include <QDebug>
+void DeclarativeComboBoxExtension::setModel(QAbstractItemModel *model)
+{
+  QComboBox *comboBox = extendedComboBox();
+
+  if (comboBox->model() == model)
+    return;
+
+  comboBox->setModel(model);
+
+  emit modelChanged();
+}
+
+QAbstractItemModel *DeclarativeComboBoxExtension::model() const
+{
+  return extendedComboBox()->model();
+}
diff --git a/lib/declarativecomboboxextension_p.h b/lib/declarativecomboboxextension_p.h
new file mode 100644 (file)
index 0000000..8970038
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+  Copyright (C) 2012-2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
+  Author: Kevin Krammer, kevin.krammer@kdab.com
+  Author: Tobias Koenig, tobias.koenig@kdab.com
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along
+  with this program; if not, write to the Free Software Foundation, Inc.,
+  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#ifndef DECLARATIVECOMBOBOXEXTENSION_P_H
+#define DECLARATIVECOMBOBOCEXTENSION_P_H
+
+#include "declarativewidgetextension.h"
+
+class QComboBox;
+class QAbstractItemModel;
+
+class DeclarativeComboBoxExtension : public DeclarativeWidgetExtension
+{
+  Q_OBJECT
+
+  Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel NOTIFY modelChanged)
+
+  // repeat property declarations, qmlRegisterExtendedType doesn't see the ones from base class
+  Q_PROPERTY(QDeclarativeListProperty<QObject> data READ data DESIGNABLE false)
+
+  Q_CLASSINFO("DefaultProperty", "data")
+
+  public:
+    explicit DeclarativeComboBoxExtension(QObject *parent = 0);
+
+    QComboBox *extendedComboBox() const;
+
+    void setModel(QAbstractItemModel *model);
+    QAbstractItemModel *model() const;
+
+  Q_SIGNALS:
+    void modelChanged();
+};
+
+#endif
index a672184..b32929f 100644 (file)
@@ -25,6 +25,7 @@
 #include "declarativeaction_p.h"
 #include "declarativebuttongroupextension_p.h"
 #include "declarativecolordialog_p.h"
+#include "declarativecomboboxextension_p.h"
 #include "declarativedeclarativecontext_p.h"
 #include "declarativedeclarativeviewextension_p.h"
 #include "declarativefiledialog_p.h"
@@ -55,6 +56,7 @@
 #include <QCalendarWidget>
 #include <QCheckBox>
 #include <QColumnView>
+#include <QComboBox>
 #include <QCommandLinkButton>
 #include <QCoreApplication>
 #include <QDateTimeEdit>
@@ -136,6 +138,7 @@ DeclarativeWidgetsDocument::DeclarativeWidgetsDocument(const QUrl &url, QObject
   qmlRegisterExtendedType<DeclarativeColorDialog, DeclarativeWidgetExtension>("QtGui", 1, 0, "ColorDialog");
   qmlRegisterExtendedType<QColumnView, DeclarativeItemViewExtension>("QtGui", 1, 0, "ColumnView");
   qmlRegisterExtendedType<QCommandLinkButton, DeclarativeWidgetExtension>("QtGui", 1, 0, "CommandLinkButton");
+  qmlRegisterExtendedType<QComboBox, DeclarativeComboBoxExtension>("QtGui", 1, 0, "ComboBox");
   qmlRegisterExtendedType<QDateEdit, DeclarativeWidgetExtension>("QtGui", 1, 0, "DateEdit");
   qmlRegisterExtendedType<QDateTimeEdit, DeclarativeWidgetExtension>("QtGui", 1, 0, "DateTimeEdit");
   qmlRegisterExtendedType<QDeclarativeView, DeclarativeDeclarativeViewExtension>("QtGui", 1, 0, "DeclarativeView");
index f2ebb2a..8892391 100644 (file)
@@ -11,6 +11,7 @@ HEADERS = \
   declarativeboxlayout_p.h \
   declarativebuttongroupextension_p.h \
   declarativecolordialog_p.h \
+  declarativecomboboxextension_p.h \
   declarativedeclarativecontext_p.h \
   declarativedeclarativeviewextension_p.h \
   declarativefiledialog_p.h \
@@ -51,6 +52,7 @@ SOURCES = \
   declarativeboxlayout.cpp \
   declarativebuttongroupextension.cpp \
   declarativecolordialog.cpp \
+  declarativecomboboxextension.cpp \
   declarativedeclarativecontext.cpp \
   declarativedeclarativeviewextension.cpp \
   declarativefiledialog.cpp \