FilterDemo Class Implementation
The FilterDemo constructor begins by initializing the QListView, the only thing we want to do here is set the horizontal scroll bar policy so the horizontal scroll bar is never shown.
FilterDemo::FilterDemo( QWidget *parent, Qt::WindowFlags flags )
: QListView( parent )
, index( 0 )
{
setWindowFlags( flags );
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
In order to display the QContentSet in the QListView we need to construct a new QContentSetModel. There's no setup required so we simply create it and set it as the model for the QListView.
setModel( new QContentSetModel( &contentSet, this ) );
Next we want to initialize the QSoftMenuBar. We'll remove the label on the Select button as there is no select action and change the label on the Back button to Next.
QSoftMenuBar::menuFor( this );
QSoftMenuBar::setLabel( this, Qt::Key_Select, QSoftMenuBar::NoLabel );
QSoftMenuBar::setLabel( this, Qt::Key_Back, QSoftMenuBar::Next );
Finally we'll display the first filtered view.
nextFilter();
}
The keyPressEvent() event handler processes the key press events for the FilterDemo widget including those from the QSoftMenuBar. When the Back key is pressed it will call the nextFilter() method and accept the key event. If any other key is pressed or nextFilter() returns false it will let QListView handle the event, if handled by QListView the Back key will close the application.
void FilterDemo::keyPressEvent( QKeyEvent *event )
{
if ( event->key() == Qt::Key_Back && nextFilter() ) {
event->accept();
} else {
QListView::keyPressEvent( event );
}
}
The nextFilter() method applies a series of filtering criteria to the QContentSet. When no more filters are left it will return false.
The first filter includes all content with the QContent::Application role.
bool FilterDemo::nextFilter()
{
switch( index++ )
{
case 0:
contentSet.setCriteria( QContentFilter( QContent::Application ) );
return true;
The second filter restricts the existing filtering criteria to only content in the Games category.
case 1:
contentSet.addCriteria( QContentFilter::Category, "Games", QContentFilter::And );
return true;
The third filter replaces the existing criteria with one that includes all content with the image/jpeg MIME type, extends that to also include the image/png MIME type, and then restricts that set to content with the QContent::Document role.
case 2:
contentSet.setCriteria( QContentFilter::MimeType, "image/jpeg" );
contentSet.addCriteria( QContentFilter::mimeType( "image/png" ), QContentFilter::Or );
contentSet.addCriteria( QContentFilter( QContent::Document ), QContentFilter::And );
return true;
The final filter extends the previous one to also include applications in the Games category.
case 3:
contentSet.addCriteria( QContentFilter( QContent::Application )
& QContentFilter::category( "Games" )
, QContentFilter::Or );
Once the final filter view has been displayed, pressing the Back key will exit the application, so change the QSoftMenuBar label to indicate this before returning. Subsequent calls to nextFilter() will return false.
QSoftMenuBar::setLabel( this, Qt::Key_Back, QSoftMenuBar::Back );
return true;
default:
return false;
}
}
Building the Content Filtering application
To install and run the Filter Demo example, carry out the following steps.
- Create a new directory (e.g. $HOME/src/filtering) and copy all the example files to that directory.
mkdir $HOME/src/filtering
cd $HOME/src/filtering
cp -r <Qt-Extended-source-directory>/examples/content/filtering/* .
chmod +w *
- Build the new application.
export QPEDIR=<Qt-Extended-build-directory>
$QPEDIR/bin/qbuild
$QPEDIR/bin/qbuild image
- Run Qt Extended.
$QPEDIR/bin/runqtopia
- Go into the list of Applications and scroll down until you find the Filter Demo application.