Hi tearsofphoenix!
1.you have to press the select button of the mobile phone, then the QListWidget will get the focus, you can then press the up button and down button to move the focus of the QListWidgetItem. How to set the focus so I just use the up/down key to move the focus directly?
I used QListView but I hope it helped you.
I using eventFilter with ListView
Code:
ui.feedsView->installEventFilter(this);
Code:
bool HomeWidget::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui.feedsView)
{
if (event->type() == QEvent::FocusIn )
{ ui.feedsView->selectionModel()->setCurrentIndex(ui.feedsView->model()->index(0,0), QItemSelectionModel::SelectCurrent);
}
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
QRect itemRect = ui.feedsView->visualRect(ui.feedsView->currentIndex());
QSize viewSize = ui.feedsView->viewport()->size();
if ( keyEvent->key() == Qt::Key_Down )
{
if ( itemRect.height() - mySize >= viewSize.height() + this->fontMetrics().height() /2 )
{
if ( ui.feedsView->verticalScrollBar()->value() + viewSize.height() - this->fontMetrics().height() <= ui.feedsView->verticalScrollBar()->maximum())
{ ui.feedsView->verticalScrollBar()->setValue(ui.feedsView->verticalScrollBar()->value() + viewSize.height() - this->fontMetrics().height());
mySize += viewSize.height() - this->fontMetrics().height();
}
else
{
vSpace = ui.feedsView->verticalScrollBar()->maximum() - ui.feedsView->verticalScrollBar()->value();
mySize += ui.feedsView->verticalScrollBar()->maximum() - ui.feedsView->verticalScrollBar()->value();
ui.feedsView->verticalScrollBar()->setValue(ui.feedsView->verticalScrollBar()->maximum());
}
return true;
}
else
{
if ( ui.feedsView->currentIndex().row() < (ui.feedsView->model()->rowCount() - 1) )
{
QModelIndex newIndexDown = ui.feedsView->model()->index(ui.feedsView->currentIndex().row() + 1, ui.feedsView->currentIndex().column());
if (newIndexDown.isValid())
{
ui.feedsView->selectionModel()->setCurrentIndex(ui.feedsView->currentIndex(), QItemSelectionModel::Clear);
ui.feedsView->selectionModel()->setCurrentIndex(newIndexDown, QItemSelectionModel::SelectCurrent);
mySize = 0;
}
return true;
}
}
}
if ( keyEvent->key() == Qt::Key_Up)
{
if ( mySize > 0 )
{
if ( ui.feedsView->verticalScrollBar()->value() == ui.feedsView->verticalScrollBar()->maximum() && vSpace > 0 )
{
ui.feedsView->verticalScrollBar()->setValue(ui.feedsView->verticalScrollBar()->value()- vSpace);
mySize -= vSpace;
}
else
{
ui.feedsView->verticalScrollBar()->setValue(ui.feedsView->verticalScrollBar()->value()- viewSize.height() + this->fontMetrics().height());
mySize += this->fontMetrics().height() - viewSize.height();
}
return true;
}
else
{
if (ui.feedsView->currentIndex().row() >0 )
{
QModelIndex newIndexUp = ui.feedsView->model()->index(ui.feedsView->currentIndex().row() - 1, ui.feedsView->currentIndex().column());
if (newIndexUp.isValid() )
{ ui.feedsView->selectionModel()->setCurrentIndex(newIndexUp, QItemSelectionModel::SelectCurrent);
}
return true;
}
}
}
if ( keyEvent->key() == Qt::Key_Return || keyEvent->key() == 16842752 )
{
// Do some thing when item selected
return true;
}
}
}
return QWidget::eventFilter(obj, event);
}
in file .h I definition :
Code:
int mySize; // init = 0 at constructor
int vSpace; // init = 0 at constructor
It does not seem to be standard but it works fine for me, even with the height of the item is much larger than viewsize of device.
2.The QLineEdit will "eat" the password you have entered, why?
QLineEdit with echoMode "Password" is bug in 4.6.2. I think you can implement QLineEdit to solve this problem (unfortunately I have not tried, if someone has solved this problem please post your solution on here, thank you very much!)