C++/Qt/QAbstractItemView

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Subclass QAbstractItemView

<source lang="cpp">

Foundations of Qt Development\Chapter05\singleitemview\main.cpp /*

* Copyright (c) 2006-2007, Johan Thelin
* 
* All rights reserved.
* 
* 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 APress 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.
*
*/
  1. include <QApplication>
  2. include <QSplitter>
  3. include <QTableView>
  4. include <QStandardItemModel>
  5. include "singleitemview.h"

int main( int argc, char **argv ) {

 QApplication app( argc, argv );
 
 QTableView *table = new QTableView();
 SingleItemView *selectionView = new SingleItemView();
 
 QSplitter splitter;
 splitter.addWidget( table );
 splitter.addWidget( selectionView );
 QStandardItemModel model( 5, 2 );
 for( int r=0; r<5; r++ ) 
   for( int c=0; c<2; c++) 
   {
     QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) );      
     model.setItem(r, c, item);
   }
 table->setModel( &model );
 selectionView->setModel( &model );
 selectionView->setSelectionModel( table->selectionModel() );
 splitter.show();
 
 return app.exec();

}

Foundations of Qt Development\Chapter05\singleitemview\singleitemview.cpp /*

* Copyright (c) 2006-2007, Johan Thelin
* 
* All rights reserved.
* 
* 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 APress 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.
*
*/
  1. include <QScrollBar>
  2. include <QGridLayout>
  3. include <QLabel>
  4. include "singleitemview.h"

SingleItemView::SingleItemView( QWidget *parent ) : QAbstractItemView( parent ) {

 QGridLayout *layout = new QGridLayout( this->viewport() );
 label = new QLabel();
 layout->addWidget( label, 0, 0 );
 
 label->setAlignment( Qt::AlignCenter );
 label->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
 label->setText( tr("No data.") );

}

QRect SingleItemView::visualRect( const QModelIndex &index ) const {

 if( selectionModel()->selection().indexes().count() != 1 )
   return QRect();
   
 if( currentIndex() != index )
   return QRect();
   
 return rect();  

} QRegion SingleItemView::visualRegionForSelection( const QItemSelection &selection ) const {

 if( selection.indexes().count() != 1 )
   return QRect();
   
 if( currentIndex() != selection.indexes()[0] )
   return QRect();
   
 return rect();

} bool SingleItemView::isIndexHidden( const QModelIndex &index ) const {

 if( selectionModel()->selection().indexes().count() != 1 )
   return true;
   
 if( currentIndex() != index )
   return true;
   
 return false;

} QModelIndex SingleItemView::indexAt( const QPoint &point ) const {

 if( selectionModel()->selection().indexes().count() != 1 )
   return QModelIndex();
   
 return currentIndex();  

} int SingleItemView::horizontalOffset() const {

 return horizontalScrollBar()->value();

} int SingleItemView::verticalOffset() const {

 return verticalScrollBar()->value();

} QModelIndex SingleItemView::moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) {

 return currentIndex();

} void SingleItemView::setSelection( const QRect &rect, QItemSelectionModel::SelectionFlags flags ) {

 // do nothing

} void SingleItemView::scrollTo( const QModelIndex &index, ScrollHint hint ) {

 // cannot scroll

} void SingleItemView::dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight ) {

 updateText();  

} void SingleItemView::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected ) {

 updateText();

} void SingleItemView::updateText() {

 switch( selectionModel()->selection().indexes().count() )
 {
   case 0:
     label->setText( tr("No data.") );
     break;
     
   case 1:
     label->setText( model()->data( currentIndex() ).toString() );
     break;
     
   default:
     label->setText( tr("Too many items selected.
Can only show one item at a time.
") ); break; }

}

Foundations of Qt Development\Chapter05\singleitemview\singleitemview.h /*

* Copyright (c) 2006-2007, Johan Thelin
* 
* All rights reserved.
* 
* 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 APress 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.
*
*/
  1. ifndef SINGLEITEMVIEW_H
  2. define SINGLEITEMVIEW_H
  3. include <QAbstractItemView>

class QLabel; class SingleItemView : public QAbstractItemView {

 Q_OBJECT
 

public:

 SingleItemView( QWidget *parent = 0 );
 
 QModelIndex indexAt( const QPoint &point ) const;
 void scrollTo( const QModelIndex &index, ScrollHint hint = EnsureVisible );
 QRect visualRect( const QModelIndex &index ) const;

protected:

 int horizontalOffset() const;
 bool isIndexHidden( const QModelIndex &index ) const;
 QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers );
 void setSelection( const QRect &rect, QItemSelectionModel::SelectionFlags flags );
 int verticalOffset() const;
 QRegion visualRegionForSelection( const QItemSelection &selection ) const;
   

protected slots:

 void dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight );
 void selectionChanged( const QItemSelection &selected, const QItemSelection &deselected );
 

private:

 void updateText();
 
 QLabel *label;

};

  1. endif // SINGLEITEMVIEW_H


 </source>