PDA

View Full Version : QListView not showing all items in the model



mclark
1st September 2010, 17:34
Qt: 4.6.2 Commercial
Windows XP

My QListView is not showing all the items in the model even when scrolling to the end. The only way to see all items is to enlarge the window (QDockWidget) to the point beyond where the scrollbar goes away.

I've not seen anything like this mentioned in the posts here, so I believe there is some setup requirement for QListView that I am not using. Has anyone seen anything like this?

:confused:How can I get the view to display all my icons in the model?

I'm using a QListView (in IconMode) which is being fed by a QStandardModel, very simple.


setIconSize( QSize( 32, 32 ) );
setViewMode( QListView::IconMode );
setGridSize( QSize( 100, 80 ) );
setResizeMode( QListView::Adjust );
setSelectionRectVisible( true );
setSpacing( 40 );
setFont( QFont( "Tahoma", 8.25 ) );
setVerticalScrollMode( QAbstractItemView::ScrollPerItem );

// NOTE: the dragDropMode must be set AFTER the viewMode!!!
setDragEnabled( true );
setDragDropMode( QAbstractItemView::DragOnly );
setSelectionMode( QAbstractItemView::ExtendedSelection );

Loading the model:


QStandardItem* pItem = new QStandardItem( sDevName );
pItem->setToolTip( sDevName );
pItem->setData( QVariant( icon ), Qt::DecorationRole );
pItem->setData( QVariant( sCategory ), DEVDATA_CATEGORY );
pItem->setData( QVariant( sDeviceDCID ), DEVDATA_DCID );
m_pDevLibModel->appendRow( pItem );

wysota
1st September 2010, 19:02
Haven't you by any chance forgotten to put your listview in a layout?

mclark
1st September 2010, 19:10
I just add it to a QTabWidget. It scrolls but not to the necessary extent.

So... I need my view in a layout to accomplish that.

Thanks, I'll give it a try.

wysota
1st September 2010, 19:18
You definitely forgot a layout in the tab widget's tab holding the list view.

mclark
1st September 2010, 20:12
I obviously am missing some knowledge here. I've added my view to a layout. Added the tab widget and set the layout on that tab. No obvious change in the scrolling of the view.

Since layouts are not widgets I've created a QWidget in order to add a new tab to the QTabWidget.


// Add the views to layouts
QVBoxLayout* pDevLibLayout = new QVBoxLayout;
pDevLibLayout->addWidget( m_pDevLibLst );

// ...

// Add the tabs
int nTabIdx = m_pDevLibTab->addTab( new QWidget, DL_TAB_ALLDEVS_STR );
m_pDevLibTab->widget( nTabIdx )->setLayout( pDevLibLayout );

I also tried explicitly adding the layout the the QWidget used to create the new tab.


QVBoxLayout* pDevLibLayout = new QVBoxLayout;
pDevLibLayout->addWidget( m_pDevLibLst );
QWidget* pDevLibWidget = new QWidget;
pDevLibWidget->setLayout( pDevLibLayout );

m_pDevLibTab->addTab( pDevLibWidget, DL_TAB_ALLDEVS_STR );

:confused:What is it that I'm not seeing here?

Gh0str1d3r
1st September 2010, 21:48
are you sure you need an extra layout here? Since you did not post how you added you list last time, was it like


m_pDevLibTab->addTab(m_pDevLibLst,"some title");

I did not work too much with listViews yet, but when I e.g. add a QToolBox as a tab to a QTabWidget, the compiler complains if I add an additional layout: QToolBox already has a layout by default. It works perfectly without setting any layout. Maybe this is different for QListViews......

mclark
1st September 2010, 22:03
are you sure you need an extra layout here? Since you did not post how you added you list last time, was it like

m_pDevLibTab->addTab(m_pDevLibLst,"some title");

Well... that is indeed how I first tried adding my QListView to the tab widget.

wysota's suggestion about layouts seems reasonable, although I don't know if QTabWidgets have a built-in default layout. If they do, it wasn't working for some reason. If not, I have not been able to make it work by adding one.

No compiler complaints when adding the layout explicitly.

wysota
1st September 2010, 23:54
If the only widget in your tab widget's tab is the list itself then passing the list view to the addTab() method is enough. Maybe I was wrong about the layout, although I don't think so. Could you post a screenshot of what you have?

mclark
2nd September 2010, 00:08
wysota, this is what I found in the docs for QTabWidget, which seems to verify you're original suggestion of adding a layout.


The normal way to use QTabWidget is to do the following:


Create a QTabWidget.
Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
Insert child widgets into the page widget, using layouts to position them as normal.
Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.

The first image shows all 18 icons.
The second image has the scrollbar all the way down and the last 3 icons are missing (no sorting).

wysota
2nd September 2010, 02:41
Are you using your own model? If so, did you check it with the model tester class available from labs?

mclark
2nd September 2010, 15:24
I am using a QStandardItemModel.

While I couldn't find any references to the model tester class on the Qt website (if you know of a link would you please provide), I am quite sure that all my data (icons/text) are in the model because the view will show all the items if the containing window is large enough.

It seems like the scrollbar is just as confused as I am:confused:

wysota
2nd September 2010, 15:29
I am using a QStandardItemModel.

While I couldn't find any references to the model tester class on the Qt website (if you know of a link would you please provide),
http://labs.trolltech.com/page/Projects/Itemview/Modeltest


It seems like the scrollbar is just as confused as I am:confused:
Or maybe those items are added later and something is preventing the model or the view from refreshing. I'm almost sure that if you change the resize mode of your list to Fixed, those items will be missing as well.

mclark
2nd September 2010, 16:00
Or maybe those items are added later and something is preventing the model or the view from refreshing. I'm almost sure that if you change the resize mode of your list to Fixed, those items will be missing as well.
You are correct about the Fixed resize mode not showing the items.

The (22) items are added during the apps initialization. When the main window becomes visible the model has been filled. No other items are added after that.

Thank you for the link, I shall try the ModelTest code you referenced.

In the meantime, I have done my own simple test; dumped the output from a loop through my model after each item addition.


QStandardItem* pItem;
for ( int i = 0; i < m_pDevLibModel->rowCount(); i++ )
{
for ( int j = 0; j < m_pDevLibModel->columnCount(); j++ )
{
pItem = m_pDevLibModel->item( i, j );
qDebug( " [%d,%d] 0x%x \"%s\"", i, j,
pItem, pItem->text().toAscii().data() );
}
}

After the final item addition, the code yields 22 entries in the model:
[0,0] 0x3856f88 "Unknown Device"
[1,0] 0x3856508 "CEM+ Rack 3"
[2,0] 0x3852628 "CEM+ Rack 14"
[3,0] 0x384ed58 "CEM+ Rack 10"
[4,0] 0x385a0d8 "CEM+"
[5,0] 0x3858f20 "CEM3"
[6,0] 0x385c020 ""
[7,0] 0x14ddb10 "CEM+ Rack 2"
[8,0] 0x15d9ea0 "Paradigm ACP"
[9,0] 0x3864fa8 "CEM+ Rack 1"
[10,0] 0x3870898 "CEM+ Rack 4"
[11,0] 0x3863948 "CEM+ Rack 5"
[12,0] 0x154aba0 "CEM+ Rack 6"
[13,0] 0x386fd88 "DMX/RDM 4-Port Gateway"
[14,0] 0x3874060 "CEM+ Rack 16"
[15,0] 0x385ce00 "CEM+ Rack 7"
[16,0] 0x14fb748 "CEM+ Rack 11"
[17,0] 0x3878940 "CEM+ Rack 12"
[18,0] 0x38688a0 "CEM+ Rack 13"
[19,0] 0x3869d78 "CEM+ Rack 8"
[20,0] 0x387c2f0 "CEM+ Rack 15"
[21,0] 0x387d548 "CEM+ Rack 9"

mclark
2nd September 2010, 21:07
:)SOLVED!:)

My QListView sub-class constructor called setSpacing( 40 ) after calling setGridSize(). While I'm not sure why this upset the normal scrolling of the view, the calls clearly were interfering with each other. Commenting out the setSpacing() call solved the problem.


DevLibView::DevLibView( QWidget* pParent ) : QListView( pParent )
{
setViewMode( QListView::IconMode );
setIconSize( QSize( 32, 32 ) );
setGridSize( QSize( 100, 80 ) );
setResizeMode( QListView::Adjust );
setSelectionRectVisible( true );
//setSpacing( 40 );
setFont( QFont( "Tahoma", 8.25 ) );

// NOTE: the dragDropMode must be set AFTER the viewMode!!!
setDragEnabled( true );
setDragDropMode( QAbstractItemView::DragOnly );
setSelectionMode( QAbstractItemView::ExtendedSelection );
}
The view now scrolls as expected (without having to add a layout to my tab widget)!

Thank you wysota and Gh0str1d3r for your time and suggestions.