PDA

View Full Version : QListWidgetItem doesn't select



graffy
14th December 2013, 15:45
Ok,

I'm encountering a problem with the QListWidget and QListWidgetItem. Specifically, the first list widget item can't be selected if I click on the item text. If, however, I click on the blank space to it's right, I can select it just fine. The second item I can select regardless of where I click. What boggles me is that both items are constructed using the same code, so it doesn't seem to be a problem with anything I'm doing.

I'm using Qt 4.8.5, and no, upgrading to 5.x isn't an option, in case someone suggests it. :)

Here's a marked up screenshot illustrating the problem:

9855

And the relevant construction code:



UserSettingsDialog::UserSettingsDialog(QMainWindow *parent) :
QMainWindow (parent), mStackedWidget (0)
{
setWindowTitle(QString::fromUtf8 ("User Settings"));

createPage ("Display Format");
createPage ("Window Size");

connect (mListWidget,
SIGNAL (currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
this,
SLOT (slotChangePage (QListWidgetItem*, QListWidgetItem*)));
}



void UserSettingsDialog::createPage (const QString &pageName)
{

SettingPage *page = new SettingPage (pageName,
CSMSettings::UserSettings::instance().settingModel (), false, this);

mStackedWidget->addWidget (&dynamic_cast<QWidget &>(*(page->pageFrame())));

//////////////////////////////////
// QListWidget item constructed and added to the list widget here

new QListWidgetItem (page->objectName(), mListWidget);

//finishing touches
QFontMetrics fm (QApplication::font());
int textWidth = fm.width(page->objectName());

mListWidget->setMinimumWidth(textWidth + 50);

resize (mStackedWidget->sizeHint());
}

ChrisW67
14th December 2013, 20:40
Nothing in your code snippets has anything to do with selection of an item or items in the list. If you mean selection then you need to show how you have configured the selectionMode() and are determining that the item is not selected.

If you mean by "can't be selected" that the currentItemChanged() is not emitted if you click on the first row then, in all likelihood it is because the first row is already the current item. The changed signal is only emitted if the current index changes.

graffy
14th December 2013, 22:34
Nothing in your code snippets has anything to do with selection of an item or items in the list. If you mean selection then you need to show how you have configured the selectionMode() and are determining that the item is not selected.

Err.. I used the QListWidget, not QListView. That means I'm relying on the internal models that the QListWidget class provides. I do nothing more than populate the QListWidget with two QListWidgetItems and listen for an index change.


If you mean by "can't be selected" that the currentItemChanged() is not emitted if you click on the first row then, in all likelihood it is because the first row is already the current item. The changed signal is only emitted if the current index changes.

You're right that the currentItemChanged() signal is not emitted, but only if I click on the text of the first QListWidgetItem. If I click on the same row, but to the right of the text, the first item *is* selected. Seems like there are two columns/indices at play, but I don't know why that would be.

Anyway, given that I've not done anything unique and I'm using the default implementation of QListWidget, the behavior just doesn't make sense to me.

I did try explicitly setting the selection behavior to both SelectRows and SelectItems. Neither case had any effect on the problem.

ChrisW67
15th December 2013, 01:50
Err.. I used the QListWidget, not QListView
QListWidget is a QListView.

Both have a current item and zero or more selected items: the two lists are not necessarily related. The list has a current item even if selectionMode() == QAbstractItemView::NoSelection.

You seem to be talking about the selection but meaning the current item.

The current item is subtly indicated by a dotted outline around or underline on the cell text (which is black text on white typically unless it also in the selected items). With a populated list like my example the first item is the current item but the selection is empty at start. In the default state the selection will follow actions that affect the current item. Changing the current row by clicking or keyboard will change both the current row and selection. If you want the first item to be in the selection by default simply do that explicitly.

When you click in the blank area of the first row you are adding the clicked row to the selection. It then becomes highlighted, usually with white text on dark background. I assume this is what you mean by "it works". It works regardless of where I click on the item on my KDE desktop with the default Qt4 style. I cannot tell what style or Window manager you are using, but that might well be playing into this apparent behaviour.



#include <QtGui>

class List: public QListWidget
{
Q_OBJECT
public:
explicit List(QWidget *p = 0): QListWidget(p) {
for (int i = 0; i < 5; ++i)
addItem(QString("Item %1").arg(i));

qDebug() << Q_FUNC_INFO << "Current row:" << currentRow() << "Current selection:" << selectedItems();
connect(this,
SIGNAL(currentItemChanged(QListWidgetItem*,QListWi dgetItem*)),
SLOT(showChange()));
}
public slots:
void showChange() {
qDebug() << Q_FUNC_INFO << "Current row:" << currentRow() << "Current selection:" << selectedItems();
}
};


int main(int argc, char **argv)
{
QApplication app(argc, argv);
List list;
list.show();
return app.exec();
}
#include "main.moc"

graffy
15th December 2013, 03:57
It works regardless of where I click on the item on my KDE desktop with the default Qt4 style. I cannot tell what style or Window manager you are using, but that might well be playing into this apparent behaviour.

The window manager may be the problem. I'm using Mint 16 with Cinnamon, which has a troubled performance record in my experience. Thought I'd give it another chance when I upgraded...

jhonathan
2nd January 2016, 11:32
Thanks for the post anyway. It will be useful to me, guess so.