PDA

View Full Version : QListWidget read only



vinzz38
3rd April 2012, 16:55
Hello,

I want a QListWidget that the user can't change the selected index but he can scroll into the list.
An other thing, i should be able to change the selected index in code behind.
I think about a setEnabled(false) on my QListWidget, but with this the user can't scroll.
Any ideas?

Thanks.

Lykurg
3rd April 2012, 17:18
You can catch mouse events and suppress them when needed. This way the user can't select items but still will be able to scroll.

vinzz38
6th April 2012, 09:18
Mouse events on QListWidget or on QListWidgetItem?

Lykurg
6th April 2012, 09:43
Whatever works.

Spitfire
9th April 2012, 14:12
There's many ways you can get what you want.

If you need only mouse events to be ignored then you can use this:


this->list->viewport()->setAttribute( Qt::WA_TransparentForMouseEvents );

This will not stop user from changing the selection using keyboard though.

To fully suppress user interaction with the widget install an event filter which will stop all mouse events on the viewport and keyboard events going to the widget:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
list = new QListWidget( this );

[...]// setup your lsit

this->list->installEventFilter( this );
this->list->viewport()->installEventFilter( this );
}

bool MainWindow::eventFilter( QObject* o, QEvent* e )
{
bool ret = false;

if( o == this->list &&
( e->type() == QEvent::KeyPress
|| e->type() == QEvent::KeyRelease ) )
{
qDebug() << "key";
ret = true;
}
else if( o == this->list->viewport() &&
( e->type() == QEvent::MouseButtonPress
|| e->type() == QEvent::MouseButtonPress
|| e->type() == QEvent::MouseButtonPress ) )
{
qDebug() << "mouse";
ret = true;
}

return ret;
}

Another way could be disabling the list widget and sticking it in separate scroll area.
Yet another would be setting NoSelection on the widget and providing custom item delegate which would draw item as 'selected' based on a property set on the item.

vinzz38
16th April 2012, 13:49
Thank you Spitfire. it works with this modifications:


bool WorkListViewUI::eventFilter(QObject* o, QEvent* e )
{
bool ret = false;

if( o == pListView &&
( e->type() == QEvent::KeyPress
|| e->type() == QEvent::KeyRelease ) )
{
qDebug() << "key";
ret = true;
}
else if( o == pListView->viewport() &&
( e->type() == QEvent::MouseButtonPress
|| e->type() == QEvent::MouseButtonRelease
|| e->type() == QEvent::MouseMove
|| e->type() == QEvent::MouseButtonDblClick ) )
{
qDebug() << "mouse";
ret = true;
}

return ret;
}

hispeedsurfer
29th August 2013, 12:41
or easy QAbstractItemView::setEventTrigger with QAbstractItemView::NoEditTriggers