doc/src/snippets/qtablewidget-using/mainwindow.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 #include "math.h"
       
    44 
       
    45 #include "mainwindow.h"
       
    46 
       
    47 MainWindow::MainWindow()
       
    48 {
       
    49     QMenu *fileMenu = new QMenu(tr("&File"));
       
    50 
       
    51     QAction *quitAction = fileMenu->addAction(tr("E&xit"));
       
    52     quitAction->setShortcut(tr("Ctrl+Q"));
       
    53 
       
    54     QMenu *itemsMenu = new QMenu(tr("&Items"));
       
    55 
       
    56     QAction *sumItemsAction = itemsMenu->addAction(tr("&Sum Items"));
       
    57     QAction *averageItemsAction = itemsMenu->addAction(tr("&Average Items"));
       
    58 
       
    59     menuBar()->addMenu(fileMenu);
       
    60     menuBar()->addMenu(itemsMenu);
       
    61 
       
    62 //! [0]
       
    63     tableWidget = new QTableWidget(12, 3, this);
       
    64 //! [0]
       
    65     tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
       
    66 
       
    67 //! [1]
       
    68     QTableWidgetItem *valuesHeaderItem = new QTableWidgetItem(tr("Values"));
       
    69     tableWidget->setHorizontalHeaderItem(0, valuesHeaderItem);
       
    70 //! [1]
       
    71     valuesHeaderItem->setTextAlignment(Qt::AlignVCenter);
       
    72     QTableWidgetItem *squaresHeaderItem = new QTableWidgetItem(tr("Squares"));
       
    73     squaresHeaderItem->setIcon(QIcon(QPixmap(":/Images/squared.png")));
       
    74     squaresHeaderItem->setTextAlignment(Qt::AlignVCenter);
       
    75 //! [2]
       
    76     QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(tr("Cubes"));
       
    77     cubesHeaderItem->setIcon(QIcon(QPixmap(":/Images/cubed.png")));
       
    78     cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);
       
    79 //! [2]
       
    80     tableWidget->setHorizontalHeaderItem(1, squaresHeaderItem);
       
    81     tableWidget->setHorizontalHeaderItem(2, cubesHeaderItem);
       
    82 
       
    83     connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
       
    84     connect(sumItemsAction, SIGNAL(triggered()), this, SLOT(sumItems()));
       
    85     connect(averageItemsAction, SIGNAL(triggered()), this, SLOT(averageItems()));
       
    86 
       
    87     setupTableItems();
       
    88 
       
    89     setCentralWidget(tableWidget);
       
    90     setWindowTitle(tr("Table Widget"));
       
    91 }
       
    92 
       
    93 void MainWindow::setupTableItems()
       
    94 {
       
    95     for (int row = 0; row < tableWidget->rowCount()-1; ++row) {
       
    96         for (int column = 0; column < tableWidget->columnCount(); ++column) {
       
    97 //! [3]
       
    98     QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
       
    99         pow(row, column+1)));
       
   100     tableWidget->setItem(row, column, newItem);
       
   101 //! [3]
       
   102         }
       
   103     }
       
   104     for (int column = 0; column < tableWidget->columnCount(); ++column) {
       
   105         QTableWidgetItem *newItem = new QTableWidgetItem;
       
   106         newItem->setFlags(Qt::ItemIsEnabled);
       
   107         tableWidget->setItem(tableWidget->rowCount()-1, column, newItem);
       
   108     }
       
   109 }
       
   110 
       
   111 void MainWindow::averageItems()
       
   112 {
       
   113     QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
       
   114     QTableWidgetItem *item;
       
   115     int number = 0;
       
   116     double total = 0;
       
   117 
       
   118     foreach (item, selected) {
       
   119         bool ok;
       
   120         double value = item->text().toDouble(&ok);
       
   121         
       
   122         if (ok && !item->text().isEmpty()) {
       
   123             total += value;
       
   124             number++;
       
   125         }
       
   126     }
       
   127     if (number > 0)
       
   128         tableWidget->currentItem()->setText(QString::number(total/number));
       
   129 }
       
   130 
       
   131 void MainWindow::sumItems()
       
   132 {
       
   133 //! [4]
       
   134     QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
       
   135     QTableWidgetItem *item;
       
   136     int number = 0;
       
   137     double total = 0;
       
   138 
       
   139     foreach (item, selected) {
       
   140         bool ok;
       
   141         double value = item->text().toDouble(&ok);
       
   142 
       
   143         if (ok && !item->text().isEmpty()) {
       
   144             total += value;
       
   145             number++;
       
   146         }
       
   147     }
       
   148 //! [4]
       
   149     if (number > 0)
       
   150         tableWidget->currentItem()->setText(QString::number(total));
       
   151 }