From 59d0bb0a1b1eb363a9761e020fb0ee7b636f9a4d Mon Sep 17 00:00:00 2001 From: Kevin Krammer Date: Wed, 13 Mar 2013 15:58:19 +0100 Subject: [PATCH] Add combo box --- examples/gallery.qml | 9 ++++++ lib/declarativecomboboxextension.cpp | 53 ++++++++++++++++++++++++++++++++++ lib/declarativecomboboxextension_p.h | 52 +++++++++++++++++++++++++++++++++ lib/declarativewidgetsdocument.cpp | 3 ++ lib/lib.pro | 2 + 5 files changed, 119 insertions(+), 0 deletions(-) create mode 100644 lib/declarativecomboboxextension.cpp create mode 100644 lib/declarativecomboboxextension_p.h diff --git a/examples/gallery.qml b/examples/gallery.qml index 4fd9257..942612d 100644 --- a/examples/gallery.qml +++ b/examples/gallery.qml @@ -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 index 0000000..4104f9d --- /dev/null +++ b/lib/declarativecomboboxextension.cpp @@ -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 + +DeclarativeComboBoxExtension::DeclarativeComboBoxExtension(QObject *parent) + : DeclarativeWidgetExtension(parent) +{ +} + +QComboBox *DeclarativeComboBoxExtension::extendedComboBox() const +{ + QComboBox *comboBox = qobject_cast(extendedWidget()); + Q_ASSERT(comboBox); + + return comboBox; +} +#include +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 index 0000000..8970038 --- /dev/null +++ b/lib/declarativecomboboxextension_p.h @@ -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 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 diff --git a/lib/declarativewidgetsdocument.cpp b/lib/declarativewidgetsdocument.cpp index a672184..b32929f 100644 --- a/lib/declarativewidgetsdocument.cpp +++ b/lib/declarativewidgetsdocument.cpp @@ -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 #include #include +#include #include #include #include @@ -136,6 +138,7 @@ DeclarativeWidgetsDocument::DeclarativeWidgetsDocument(const QUrl &url, QObject qmlRegisterExtendedType("QtGui", 1, 0, "ColorDialog"); qmlRegisterExtendedType("QtGui", 1, 0, "ColumnView"); qmlRegisterExtendedType("QtGui", 1, 0, "CommandLinkButton"); + qmlRegisterExtendedType("QtGui", 1, 0, "ComboBox"); qmlRegisterExtendedType("QtGui", 1, 0, "DateEdit"); qmlRegisterExtendedType("QtGui", 1, 0, "DateTimeEdit"); qmlRegisterExtendedType("QtGui", 1, 0, "DeclarativeView"); diff --git a/lib/lib.pro b/lib/lib.pro index f2ebb2a..8892391 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -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 \ -- 1.7.2.5