summaryrefslogtreecommitdiffstats
path: root/examples/widgets/itemviews/frozencolumn
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/itemviews/frozencolumn')
-rw-r--r--examples/widgets/itemviews/frozencolumn/freezetablewidget.cpp165
-rw-r--r--examples/widgets/itemviews/frozencolumn/freezetablewidget.h72
-rw-r--r--examples/widgets/itemviews/frozencolumn/frozencolumn.desktop11
-rw-r--r--examples/widgets/itemviews/frozencolumn/frozencolumn.pro11
-rw-r--r--examples/widgets/itemviews/frozencolumn/grades.qrc5
-rw-r--r--examples/widgets/itemviews/frozencolumn/grades.txt36
-rw-r--r--examples/widgets/itemviews/frozencolumn/main.cpp88
7 files changed, 388 insertions, 0 deletions
diff --git a/examples/widgets/itemviews/frozencolumn/freezetablewidget.cpp b/examples/widgets/itemviews/frozencolumn/freezetablewidget.cpp
new file mode 100644
index 0000000000..ae83c341c4
--- /dev/null
+++ b/examples/widgets/itemviews/frozencolumn/freezetablewidget.cpp
@@ -0,0 +1,165 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+
+#include "freezetablewidget.h"
+
+//! [constructor]
+FreezeTableWidget::FreezeTableWidget(QAbstractItemModel * model)
+{
+ setModel(model);
+ frozenTableView = new QTableView(this);
+
+ init();
+
+ //connect the headers and scrollbars of both tableviews together
+ connect(horizontalHeader(),SIGNAL(sectionResized(int,int,int)), this,
+ SLOT(updateSectionWidth(int,int,int)));
+ connect(verticalHeader(),SIGNAL(sectionResized(int,int,int)), this,
+ SLOT(updateSectionHeight(int,int,int)));
+
+ connect(frozenTableView->verticalScrollBar(), SIGNAL(valueChanged(int)),
+ verticalScrollBar(), SLOT(setValue(int)));
+ connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
+ frozenTableView->verticalScrollBar(), SLOT(setValue(int)));
+
+
+}
+//! [constructor]
+
+FreezeTableWidget::~FreezeTableWidget()
+{
+ delete frozenTableView;
+}
+
+//! [init part1]
+void FreezeTableWidget::init()
+{
+ frozenTableView->setModel(model());
+ frozenTableView->setFocusPolicy(Qt::NoFocus);
+ frozenTableView->verticalHeader()->hide();
+ frozenTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
+
+ viewport()->stackUnder(frozenTableView);
+//! [init part1]
+
+//! [init part2]
+ frozenTableView->setStyleSheet("QTableView { border: none;"
+ "background-color: #8EDE21;"
+ "selection-background-color: #999}"); //for demo purposes
+ frozenTableView->setSelectionModel(selectionModel());
+ for(int col=1; col<model()->columnCount(); col++)
+ frozenTableView->setColumnHidden(col, true);
+
+ frozenTableView->setColumnWidth(0, columnWidth(0) );
+
+ frozenTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ frozenTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ frozenTableView->show();
+
+ updateFrozenTableGeometry();
+
+ setHorizontalScrollMode(ScrollPerPixel);
+ setVerticalScrollMode(ScrollPerPixel);
+ frozenTableView->setVerticalScrollMode(ScrollPerPixel);
+}
+//! [init part2]
+
+
+//! [sections]
+void FreezeTableWidget::updateSectionWidth(int logicalIndex, int, int newSize)
+{
+ if(logicalIndex==0){
+ frozenTableView->setColumnWidth(0,newSize);
+ updateFrozenTableGeometry();
+ }
+}
+
+void FreezeTableWidget::updateSectionHeight(int logicalIndex, int, int newSize)
+{
+ frozenTableView->setRowHeight(logicalIndex, newSize);
+}
+//! [sections]
+
+
+//! [resize]
+void FreezeTableWidget::resizeEvent(QResizeEvent * event)
+{
+ QTableView::resizeEvent(event);
+ updateFrozenTableGeometry();
+ }
+//! [resize]
+
+
+//! [navigate]
+QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction,
+ Qt::KeyboardModifiers modifiers)
+{
+ QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);
+
+ if(cursorAction == MoveLeft && current.column()>0
+ && visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ){
+
+ const int newValue = horizontalScrollBar()->value() + visualRect(current).topLeft().x()
+ - frozenTableView->columnWidth(0);
+ horizontalScrollBar()->setValue(newValue);
+ }
+ return current;
+}
+//! [navigate]
+
+void FreezeTableWidget::scrollTo (const QModelIndex & index, ScrollHint hint){
+ if(index.column()>0)
+ QTableView::scrollTo(index, hint);
+}
+
+
+
+//! [geometry]
+void FreezeTableWidget::updateFrozenTableGeometry()
+{
+ frozenTableView->setGeometry( verticalHeader()->width()+frameWidth(),
+ frameWidth(), columnWidth(0),
+ viewport()->height()+horizontalHeader()->height());
+}
+//! [geometry]
+
+
diff --git a/examples/widgets/itemviews/frozencolumn/freezetablewidget.h b/examples/widgets/itemviews/frozencolumn/freezetablewidget.h
new file mode 100644
index 0000000000..1432c012f5
--- /dev/null
+++ b/examples/widgets/itemviews/frozencolumn/freezetablewidget.h
@@ -0,0 +1,72 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef FREEZETABLEWIDGET_H
+#define FREEZETABLEWIDGET_H
+
+#include <QTableView>
+
+//! [Widget definition]
+class FreezeTableWidget : public QTableView {
+ Q_OBJECT
+
+public:
+ FreezeTableWidget(QAbstractItemModel * model);
+ ~FreezeTableWidget();
+
+
+protected:
+ virtual void resizeEvent(QResizeEvent *event);
+ virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
+ void scrollTo (const QModelIndex & index, ScrollHint hint = EnsureVisible);
+
+private:
+ QTableView *frozenTableView;
+ void init();
+ void updateFrozenTableGeometry();
+
+
+private slots:
+ void updateSectionWidth(int logicalIndex,int, int newSize);
+ void updateSectionHeight(int logicalIndex, int, int newSize);
+
+};
+//! [Widget definition]
+#endif
diff --git a/examples/widgets/itemviews/frozencolumn/frozencolumn.desktop b/examples/widgets/itemviews/frozencolumn/frozencolumn.desktop
new file mode 100644
index 0000000000..3d1e3b430d
--- /dev/null
+++ b/examples/widgets/itemviews/frozencolumn/frozencolumn.desktop
@@ -0,0 +1,11 @@
+[Desktop Entry]
+Encoding=UTF-8
+Version=1.0
+Type=Application
+Terminal=false
+Name=Frozen Column
+Exec=/opt/usr/bin/frozencolumn
+Icon=frozencolumn
+X-Window-Icon=
+X-HildonDesk-ShowInToolbar=true
+X-Osso-Type=application/x-executable
diff --git a/examples/widgets/itemviews/frozencolumn/frozencolumn.pro b/examples/widgets/itemviews/frozencolumn/frozencolumn.pro
new file mode 100644
index 0000000000..34a4a4de8c
--- /dev/null
+++ b/examples/widgets/itemviews/frozencolumn/frozencolumn.pro
@@ -0,0 +1,11 @@
+HEADERS += freezetablewidget.h
+SOURCES += main.cpp freezetablewidget.cpp
+RESOURCES += grades.qrc
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/itemviews/frozencolumn
+sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro
+sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/itemviews/frozencolumn
+INSTALLS += target sources
+QT += widgets
+
diff --git a/examples/widgets/itemviews/frozencolumn/grades.qrc b/examples/widgets/itemviews/frozencolumn/grades.qrc
new file mode 100644
index 0000000000..5f16d56931
--- /dev/null
+++ b/examples/widgets/itemviews/frozencolumn/grades.qrc
@@ -0,0 +1,5 @@
+<RCC>
+ <qresource prefix="/" >
+ <file>grades.txt</file>
+ </qresource>
+</RCC>
diff --git a/examples/widgets/itemviews/frozencolumn/grades.txt b/examples/widgets/itemviews/frozencolumn/grades.txt
new file mode 100644
index 0000000000..e1c6fcc49e
--- /dev/null
+++ b/examples/widgets/itemviews/frozencolumn/grades.txt
@@ -0,0 +1,36 @@
+ France , Norway , YDS , UK(tech.), UK(adj.) , UIAA , Ger , Australia , Finland , Brazil
+
+1, , 5.2, , , I , I , , , Isup
+2, , 5.3, , , II , II , 11, , II
+3, 3, 5.4, , , III , III , 12, , IIsup
+4, 4, 5.5, 4a , VD , IV , IV , 12, , III
+5a , 5-, 5.6, , S , V- , V , 13, 5-, IIIsup
+5b , 5, 5.7, 4b , HS , V , VI , 14, 5, IV
+ , , , 4c , , V+ , , 15, ,
+5c , 5+, 5.8, , VS , VI- , VIIa , 16, 5, IVsup
+6a , 6-, 5.9, 5a , HVS , VI , VIIb , 17, , V
+6a+ , 6-/6 , 5.10a , , E1 , VI+ , VIIc , 18, 6-, VI
+6b , , 5.10b , 5b , , , , 19, , VI/VI+
+6b+ , 6, 5.10c , , E2 , VII- , VIIIa , 20, 6, VIsup/VI+
+6c , 6+, 5.10d , 5c , , VII , VIIIb , 21, , VIsup
+6c+ , 7-, 5.11a , , E3 , VII+ , VIIIc , 22, 6, 7a
+6c+ , 7, 5.11b , , , , , 23, , 7b
+7a , 7+, 5.11c , 6a , E4 , VIII- , IXa , 24, 7-, 7c
+7a , 7+/8- , 5.11d , , , VIII , IXb , , , 7c
+7a+ , 8-, 5.12a , , E5 , VIII+ , IXc , 25, 7, 8a
+7b , 8, 5.12b , 6b , , , , 26, 8-, 8b
+7b+ , 8/8+ , 5.12c , , E6 , IX- , Xa , 27, 8, 8c
+7c , 8+, 5.12d , 6c , , IX , Xb , 28, 8, 9a
+7c+ , 9-, 5.13a , , E7 , IX+ , Xc , 29, 9-, 9b
+8a , , 5.13b , , , , , , 9, 9c
+8a+ , 9-/9 , 5.13c , 7a , , X- , , 30, 9, 10a
+8b , 9, 5.13d , , E8 , X , , 31, 10-, 10b
+8b+ , 9/9+ , 5.14a , , , X+ , , 32, 10, 10c
+8c , 9+, 5.14b , 7b , , , , 33, 10, 11a
+8c+ , 10-, 5.14c , , E9 , XI- , , 34, 11-, 11b
+9a , 10, 5.14d , 7c , , XI , , 35, 11, 11c
+9a+ , , 5.15a , , , XI+ , , , , 12a
+9b , , 5.15b , , , , , , , 12b
+
+# Wikipedia contributors. Grade (climbing). Wikipedia, The Free Encyclopedia. May 15, 2009, 20:42 UTC.
+# Available at: http://en.wikipedia.org/w/index.php?title=Grade_(climbing)&oldid=290165724. Accessed May 28, 2009.
diff --git a/examples/widgets/itemviews/frozencolumn/main.cpp b/examples/widgets/itemviews/frozencolumn/main.cpp
new file mode 100644
index 0000000000..ff50e97db9
--- /dev/null
+++ b/examples/widgets/itemviews/frozencolumn/main.cpp
@@ -0,0 +1,88 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QApplication>
+#include <QStandardItemModel>
+#include <QFile>
+
+#include "freezetablewidget.h"
+
+int main( int argc, char** argv )
+{
+
+ Q_INIT_RESOURCE(grades);
+
+
+ QApplication app( argc, argv );
+ QStandardItemModel *model=new QStandardItemModel();
+
+
+ QFile file(":/grades.txt");
+ QString line;
+ QStringList list;
+ if (file.open(QFile::ReadOnly)) {
+ line = file.readLine(200);
+ list= line.simplified().split(",");
+ model->setHorizontalHeaderLabels(list);
+
+ int row=0;
+ QStandardItem *newItem=0;
+ while(file.canReadLine()){
+ line = file.readLine(200);
+ if(!line.startsWith("#") && line.contains(",")){
+ list= line.simplified().split(",");
+ for(int col=0; col<list.length(); col++){
+ newItem = new QStandardItem(list.at(col));
+ model->setItem(row ,col, newItem);
+ }
+ row++;
+ }
+ }
+ }
+ file.close();
+
+ FreezeTableWidget *tableView = new FreezeTableWidget(model);
+
+ tableView->setWindowTitle(QObject::tr("Frozen Column Example"));
+ tableView->resize(560,680);
+ tableView->show();
+ return app.exec();
+}
+