Results 1 to 6 of 6

Thread: QListWidgetItem doesn't select

  1. #1
    Join Date
    May 2013
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QListWidgetItem doesn't select

    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:

    screenshot.png

    And the relevant construction code:


    Qt Code:
    1. UserSettingsDialog::UserSettingsDialog(QMainWindow *parent) :
    2. QMainWindow (parent), mStackedWidget (0)
    3. {
    4. setWindowTitle(QString::fromUtf8 ("User Settings"));
    5.  
    6. createPage ("Display Format");
    7. createPage ("Window Size");
    8.  
    9. connect (mListWidget,
    10. SIGNAL (currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
    11. this,
    12. SLOT (slotChangePage (QListWidgetItem*, QListWidgetItem*)));
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void UserSettingsDialog::createPage (const QString &pageName)
    2. {
    3.  
    4. SettingPage *page = new SettingPage (pageName,
    5. CSMSettings::UserSettings::instance().settingModel(), false, this);
    6.  
    7. mStackedWidget->addWidget (&dynamic_cast<QWidget &>(*(page->pageFrame())));
    8.  
    9. //////////////////////////////////
    10. // QListWidget item constructed and added to the list widget here
    11.  
    12. new QListWidgetItem (page->objectName(), mListWidget);
    13.  
    14. //finishing touches
    15. QFontMetrics fm (QApplication::font());
    16. int textWidth = fm.width(page->objectName());
    17.  
    18. mListWidget->setMinimumWidth(textWidth + 50);
    19.  
    20. resize (mStackedWidget->sizeHint());
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to graffy for this useful post:


  3. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QListWidgetItem doesn't select

    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.

  4. The following user says thank you to ChrisW67 for this useful post:


  5. #3
    Join Date
    May 2013
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QListWidgetItem doesn't select

    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.

  6. The following user says thank you to graffy for this useful post:


  7. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QListWidgetItem doesn't select

    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.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class List: public QListWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit List(QWidget *p = 0): QListWidget(p) {
    8. for (int i = 0; i < 5; ++i)
    9. addItem(QString("Item %1").arg(i));
    10.  
    11. qDebug() << Q_FUNC_INFO << "Current row:" << currentRow() << "Current selection:" << selectedItems();
    12. connect(this,
    13. SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
    14. SLOT(showChange()));
    15. }
    16. public slots:
    17. void showChange() {
    18. qDebug() << Q_FUNC_INFO << "Current row:" << currentRow() << "Current selection:" << selectedItems();
    19. }
    20. };
    21.  
    22.  
    23. int main(int argc, char **argv)
    24. {
    25. QApplication app(argc, argv);
    26. List list;
    27. list.show();
    28. return app.exec();
    29. }
    30. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to ChrisW67 for this useful post:


  9. #5
    Join Date
    May 2013
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QListWidgetItem doesn't select

    Qt Code:
    1. 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.
    To copy to clipboard, switch view to plain text mode 

    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...

  10. The following user says thank you to graffy for this useful post:


  11. #6

Similar Threads

  1. QListWidgetItem row id
    By Kyosaur in forum Qt Programming
    Replies: 5
    Last Post: 25th October 2013, 08:39
  2. Replies: 9
    Last Post: 9th November 2010, 11:18
  3. Select statemen with bindvalues in Firebird 2 doesn't work
    By corrado1972 in forum Qt Programming
    Replies: 0
    Last Post: 1st September 2010, 16:59
  4. Replies: 4
    Last Post: 4th August 2010, 21:48
  5. about QListWidgetItem
    By Pang in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2007, 10:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.