From 764a3dd99072cde27fdf7887cd40f4db54796781 Mon Sep 17 00:00:00 2001 From: Kavindra Devi Palaraja Date: Tue, 23 Jun 2009 10:33:05 +0200 Subject: Doc - Adding Pierre Rossi's frozen column example Reviewed-By: TrustMe --- .../itemviews/frozencolumn/freezetablewidget.cpp | 159 +++++++++++++++++++++ .../itemviews/frozencolumn/freezetablewidget.h | 73 ++++++++++ examples/itemviews/frozencolumn/frozencolumn.pro | 9 ++ examples/itemviews/frozencolumn/grades.qrc | 5 + examples/itemviews/frozencolumn/grades.txt | 35 +++++ examples/itemviews/frozencolumn/main.cpp | 90 ++++++++++++ 6 files changed, 371 insertions(+) create mode 100644 examples/itemviews/frozencolumn/freezetablewidget.cpp create mode 100644 examples/itemviews/frozencolumn/freezetablewidget.h create mode 100644 examples/itemviews/frozencolumn/frozencolumn.pro create mode 100644 examples/itemviews/frozencolumn/grades.qrc create mode 100644 examples/itemviews/frozencolumn/grades.txt create mode 100644 examples/itemviews/frozencolumn/main.cpp (limited to 'examples/itemviews') diff --git a/examples/itemviews/frozencolumn/freezetablewidget.cpp b/examples/itemviews/frozencolumn/freezetablewidget.cpp new file mode 100644 index 0000000000..7a9a8dfa38 --- /dev/null +++ b/examples/itemviews/frozencolumn/freezetablewidget.cpp @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#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()->setResizeMode(QHeaderView::Fixed); + + viewport()->stackUnder(frozenTableView); +//! [init part1] + +//! [init part2] + frozenTableView->setStyleSheet("QTableView { border: none;" + "background-color: #8EDE21;}"); //for demo purposes + frozenTableView->setSelectionModel(selectionModel()); + for(int col=1; colcolumnCount(); 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] + + +//! [geometry] +void FreezeTableWidget::updateFrozenTableGeometry() +{ + frozenTableView->setGeometry( verticalHeader()->width()+frameWidth(), + frameWidth(), columnWidth(0), + viewport()->height()+horizontalHeader()->height()); +} +//! [geometry] + + diff --git a/examples/itemviews/frozencolumn/freezetablewidget.h b/examples/itemviews/frozencolumn/freezetablewidget.h new file mode 100644 index 0000000000..2abae47d2f --- /dev/null +++ b/examples/itemviews/frozencolumn/freezetablewidget.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef FREEZETABLEWIDGET_H +#define FREEZETABLEWIDGET_H + +#include + +//! [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); + +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/itemviews/frozencolumn/frozencolumn.pro b/examples/itemviews/frozencolumn/frozencolumn.pro new file mode 100644 index 0000000000..361de5be3d --- /dev/null +++ b/examples/itemviews/frozencolumn/frozencolumn.pro @@ -0,0 +1,9 @@ +HEADERS += freezetablewidget.h +SOURCES += main.cpp freezetablewidget.cpp +RESOURCES += grades.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/frozencolumn +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/frozencolumn +INSTALLS += target sources diff --git a/examples/itemviews/frozencolumn/grades.qrc b/examples/itemviews/frozencolumn/grades.qrc new file mode 100644 index 0000000000..5f16d56931 --- /dev/null +++ b/examples/itemviews/frozencolumn/grades.qrc @@ -0,0 +1,5 @@ + + + grades.txt + + diff --git a/examples/itemviews/frozencolumn/grades.txt b/examples/itemviews/frozencolumn/grades.txt new file mode 100644 index 0000000000..4b55b47348 --- /dev/null +++ b/examples/itemviews/frozencolumn/grades.txt @@ -0,0 +1,35 @@ + 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/itemviews/frozencolumn/main.cpp b/examples/itemviews/frozencolumn/main.cpp new file mode 100644 index 0000000000..fdefd73654 --- /dev/null +++ b/examples/itemviews/frozencolumn/main.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include +#include +#include + +#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; colsetItem(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(); +} + -- cgit v1.2.3