QTableView - Selecting Item

4,063 views
Skip to first unread message

CarozoDeQuilmes

unread,
Feb 1, 2011, 2:36:38 PM2/1/11
to harbou...@googlegroups.com
Hi Pritpal, a little question about QTableView

When I need select an entire row (Behavior: QAbstractItemView_SelectRows), I use oTableView:selectedRow( nRow ).  It work fine.

When I need select an entire column (Behavior: QAbstractItemView_SelectColumns), I use oTableView:selectedColumn( nCol ).  It work fine.

but, when I need select only an Item (Behavior: QAbstractItemView_SelectItems) I didn't found an according method


Can you help me?

Thanks in advance
--

CdQ

Pritpal Bedi

unread,
Feb 1, 2011, 3:29:17 PM2/1/11
to Harbour Developers
Hi

> When I need select an entire row (Behavior: QAbstractItemView_SelectRows), I
> use oTableView:selectedRow( nRow ).  It work fine.
>
> When I need select an entire column (Behavior:
> QAbstractItemView_SelectColumns), I use oTableView:selectedColumn( nCol ).
> It work fine.
>
> but, when I need select only an Item (Behavior:
> QAbstractItemView_SelectItems) I didn't found an according method

There is no such method as is for this purpose.
However you can experiment with
scrollTo( oQModelIndex )
if you know the ModelIndex of the items tou want to select.

Pritpal Bedi

CarozoDeQuilmes

unread,
Feb 1, 2011, 3:32:54 PM2/1/11
to harbou...@googlegroups.com
Thanks !

CdQ
--
CdQ

francesco perillo

unread,
Feb 1, 2011, 3:36:47 PM2/1/11
to harbou...@googlegroups.com
void QItemSelectionModel::setCurrentIndex ( const QModelIndex & index,
QItemSelectionModel::SelectionFlags command )
???

Bacco

unread,
Feb 1, 2011, 3:37:27 PM2/1/11
to harbou...@googlegroups.com
Hi, CdQ

In Qt you can obtain this using
QModelIndex index = table->model()->index(row, column);
table->selectionModel()->select(index, QItemSelectionModel::Select);

But I really don't know if current HBQt implements all the classes
needed to do this, as I am currently using my own fork of HBQt.
Perhaps Francesco or Pritpal can help.

Regards;
Bacco

Bacco

unread,
Feb 1, 2011, 3:48:19 PM2/1/11
to harbou...@googlegroups.com
Hi, Francesco

> void QItemSelectionModel::setCurrentIndex ( const QModelIndex & index,
> QItemSelectionModel::SelectionFlags command )

This changes the dotted cursor around the cell. Of course if you are
using single cell selection it somewhat works, but it's problematic
with ranges / multi selection. Can you tell me if
QItemSelectionModel::select ( const QModelIndex & index,
QItemSelectionModel::SelectionFlags command )
is working fine with current HBQt?


Thanks,
Bacco

Bacco

unread,
Feb 1, 2011, 3:55:28 PM2/1/11
to harbou...@googlegroups.com
Hi, CdQ

Please, try this:

oSModel := oTableView:selectionModel()
oSModel:select( oTableView:model():index( row, col), 3 /*
QItemSelectionModel::ClearAndSelect */ )

Or this

oSModel := QItemSelectionModel():from( oTableView:selectionModel() )
oSModel:select( oTableView:model():index( row, col), 3 /*
QItemSelectionModel::ClearAndSelect */ )

And this for multiple selection:

oSModel:select( oTableView:model():index( row, col), 3 /*
QItemSelectionModel::ClearAndSelect */ )
oSModel:select( oTableView:model():index( row, col), 2 /*
QItemSelectionModel::Select */ )
oSModel:select( oTableView:model():index( row, col), 2 /*
QItemSelectionModel::Select */ )

Please tell me if it works, I can't test with my current setup.

CarozoDeQuilmes

unread,
Feb 1, 2011, 4:41:21 PM2/1/11
to harbou...@googlegroups.com
This code work fine for my project:

oTableView := QTableView( pParent )
oModel := HBQAbstractItemModel( ........

.......

oTableView:setCurrentIndex( oModel:index( nRow , nCol ) )

Regards
CdQ
--
CdQ

Bacco

unread,
Feb 1, 2011, 4:47:55 PM2/1/11
to harbou...@googlegroups.com
> This code work fine for my project:
>
> oTableView := QTableView( pParent )
> oModel := HBQAbstractItemModel( ........
>
> .......
>
> oTableView:setCurrentIndex( oModel:index( nRow , nCol ) )
>
> Regards
> CdQ

Ok, is not the correct way, but it's your project, so you can do
the way you want.

francesco perillo

unread,
Feb 1, 2011, 4:57:54 PM2/1/11
to harbou...@googlegroups.com
select and current index are not the same...

You can select 10 cells with a program while the index is in a 11th cell...

It depends on what CdQ needs to do...

Bacco

unread,
Feb 1, 2011, 5:10:11 PM2/1/11
to harbou...@googlegroups.com
Hi, Francesco

> You can select 10 cells with a program while the index is in a 11th cell...
> It depends on what CdQ needs to do...

Of course it depends. I'm referring only to the question he asked, of
course. By the way, If you have spare time, and can test and report
one of my suggestions, I'll be glad, as I don't have the original HBQt
here. Maybe someone else is using HBQt and such selection seem a
common need.


Many thanks,
Bacco

francesco perillo

unread,
Feb 1, 2011, 5:26:33 PM2/1/11
to harbou...@googlegroups.com
sorry, can't do now either at the moment....

CarozoDeQuilmes

unread,
Feb 2, 2011, 1:22:41 PM2/2/11
to harbou...@googlegroups.com
Hi Bacco, I modified the testqaim.prg sample to implement your tests:

(you can find changes by search the word "Bacco" or locating "red" text)

The results:

TestBacco1: Work ok
TestBacco2: Cell didn't hilited and the application don't respond anymore (I nedd cancel it)
TestBacco3: Cell didn't hilited and the application don't respond anymore (I nedd cancel it)

//=INI SOURCE===================================================//
/*
 * $Id: testqaim.prg 15705 2010-10-28 10:27:55Z vszakats $
 */

/*
 * Harbour Project source code:
 *
 * Copyright 2010 Carlos Bacco <carlosbacco at gmail.com>
 * www - http://harbour-project.org
 *
 */

#include "hbqtgui.ch"

#include "hbtrace.ch"

#include "common.ch"

STATIC qApp
STATIC oWnd

STATIC oDA, oForeColor, oAlphaColor, oBackBrush, oSize, oPixmap, oIcon, oFont


INIT PROCEDURE Qt_Start()
   qApp := QApplication()
   RETURN

EXIT PROCEDURE Qt_End()
   qApp:quit()
   RETURN

PROCEDURE Main()
   LOCAL tb1, mo1, lay1, lay2, bt1, bt2, bt3

   oWnd := QMainWindow()
   oWnd:resize(600,400 )

   oDA := QWidget()
   oWnd:setCentralWidget( oDA )
   lay1 := QVBoxLayout( oDA )

   /* Here we define some HBQt objects to use in the callback */
   oForeColor := QColor( 0, 50, 100 )                              // Solid blue
   oAlphaColor := QColor( 255, 0, 0 )                              // Transparent Red
   oAlphaColor:setAlpha( 128 )
   oBackBrush := QBrush( QColor( 255, 255, 100 ), Qt_BDiagPattern ) // Yellow Diagonal lines
   oSize := QSize( 80, 30 )                                        // Default header sizes
   oPixmap := QPixMap( hb_dirBase() + "harbour-icon.png" )         // Image
   oIcon := QIcon( oPixMap )                                       // Icon (reusing the image)
   oFont := QFont()                                                // Bold Italic font
   oFont:setBold( .T. )
   oFont:setItalic( .T. )
   /* ------------------------------------------------------- */

   tb1 := QTableView()

   mo1 := HBQAbstractItemModel( {| t, r, x, y| my_aim( t, r, x, y ) } )
   tb1:setModel( mo1 )

   lay1:addWidget( tb1 )

   lay2 := QHBoxLayout()
   lay1:addlayout( lay2 )

   ( bt1 := QPushButton() ):SetText( "Dummy 1" )
   ( bt2 := QPushButton() ):SetText( "Dummy 2" )
   ( bt3 := QPushButton() ):SetText( "Dummy 3" )

   bt1:connect( "clicked()", {|| TestBacco1( @tb1 ) } )
   bt2:connect( "clicked()", {|| TestBacco2( @tb1 ) } )
   bt3:connect( "clicked()", {|| TestBacco3( @tb1 ) } )


   lay2:addWidget( bt1 )
   lay2:addStretch()
   lay2:addWidget( bt2 )
   lay2:addWidget( bt3 )

   oWnd:Show()
   qApp:exec()

   RETURN

STATIC FUNCTION my_aim( t, role, x, y )

   SWITCH t
   CASE HBQT_QAIM_data

      SWITCH role
      CASE Qt_DisplayRole
         IF x == 2 .AND. y == 6
            RETURN "Unselectable"
         ENDIF
         RETURN hb_ntos( x ) + "," + hb_ntos( y )

      CASE Qt_ForegroundRole
         IF x == 7
            RETURN oAlphaColor
         ENDIF
         RETURN oForeColor /* Here we are sending a QColor */

      CASE Qt_BackgroundRole
         IF x == 2
            RETURN oBackBrush /* Here we are sending a QBrush */
         ENDIF
         RETURN NIL

      CASE Qt_TextAlignmentRole
         RETURN Qt_AlignCenter

      CASE Qt_DecorationRole
         IF ( x % 4 ) == 0 .AND. ( y % 4 ) == 0
            RETURN oPixmap /* Here we are sending a QPixmap */
         ELSEIF x == 2 .AND. y > 4 .AND. y < 10
            RETURN oIcon /* Here we are sending a QIcon */
         ELSEIF x == 2 .AND. y == 3
            RETURN oAlphaColor /* Same role, other type */
         ELSEIF x == 2 .AND. y == 4
            RETURN oForeColor /* Same role, other type */
         ENDIF

      CASE Qt_FontRole
         IF x == 1
            RETURN oFont
         ENDIF
         RETURN NIL

      ENDSWITCH

      RETURN NIL

   CASE HBQT_QAIM_flags
      IF x == 2 .AND. y == 6
         RETURN 0 /* cell is unselectable */
      ENDIF
      RETURN NIL

   CASE HBQT_QAIM_headerData
      SWITCH role
      CASE Qt_DisplayRole
         RETURN iif( x == Qt_Horizontal, "H", "V" ) + hb_ntos( y )

      CASE Qt_TextAlignmentRole
         RETURN Qt_AlignCenter

      CASE Qt_SizeHintRole
         RETURN oSize  /* Finally we are sending a QSize */
      ENDSWITCH
      RETURN NIL

   CASE HBQT_QAIM_rowCount
      RETURN 50000

   CASE HBQT_QAIM_columnCount
      RETURN 1000
   ENDSWITCH

   RETURN NIL

Function TestBacco1( oTableView )
   Local oSModel
   oSModel := oTableView:selectionModel()
   oSModel:select( oTableView:model():index( 1, 1), 3 /* QItemSelectionModel::ClearAndSelect */ )
Return

Function TestBacco2( oTableView )
   Local oSModel

   oSModel := QItemSelectionModel():from( oTableView:selectionModel() )
   oSModel:select( oTableView:model():index( 2, 2), 3 /* QItemSelectionModel::ClearAndSelect */ )
Return

Function TestBacco3( oTableView )
   Local oSModel
   oSModel:select( oTableView:model():index( 3, 3), 3 /* QItemSelectionModel::ClearAndSelect */ )
   oSModel:select( oTableView:model():index( 3, 3), 2 /* QItemSelectionModel::Select */ )
   oSModel:select( oTableView:model():index( 3, 3), 2 /* QItemSelectionModel::Select */ )
Return


//=END SOURCE===================================================//

On Tue, Feb 1, 2011 at 5:55 PM, Bacco <carlo...@gmail.com> wrote:



--
CdQ

Bacco

unread,
Feb 2, 2011, 1:46:30 PM2/2/11
to harbou...@googlegroups.com
Hi, CdQ

Thanks for testing. So I conclude you can use this for multiple
selections, if you need in the future:

Function TestBacco1( oTableView )
Local oSModel
oSModel := oTableView:selectionModel()

// This clears previous selection and select a new cell


oSModel:select( oTableView:model():index( 1, 1), 3 /*
QItemSelectionModel::ClearAndSelect */ )

// This selects new cells, but mantain previous selected cells also
(add to selection)
oSModel:select( oTableView:model():index( 1, 2), 2 /*
QItemSelectionModel::Select */ )
oSModel:select( oTableView:model():index( 1, 3), 2 /*


QItemSelectionModel::Select */ )
Return

Also, it would be good if someone adds the QItemSelectionModel:: flags to HBQt


Regards
Bacco

Reply all
Reply to author
Forward
0 new messages