Results 1 to 2 of 2

Thread: When windows focused and QListWidget had focus, first item selected, bug?

  1. #1
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default When windows focused and QListWidget had focus, first item selected, bug?

    Hi all,
    If I have a QListWidget and I add for example two or more items, then I click to QListWidget to focus (but not by clicking to some item, just click somewhere else in the QListWidget) it, after that if window loses focus and again gains focus, first item of QListWidget is selected, is this a bug or expected behavior?

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QListWidget>
    4. #include <QString>
    5. #include <QHBoxLayout>
    6. #include <QLabel>
    7. #include <QLineEdit>
    8.  
    9. int main(int argc, char **argv) {
    10. QApplication app(argc, argv);
    11. QMainWindow m_window;
    12.  
    13. QListWidget *m_listWidget = new QListWidget;
    14. QHBoxLayout *m_layout = new QHBoxLayout;
    15. QLineEdit *m_line = new QLineEdit;
    16. QLabel *m_label = new QLabel;
    17. QWidget *m_widget = new QWidget;
    18.  
    19. QObject::connect(m_listWidget, SIGNAL(currentRowChanged(int)), m_label, SLOT(setNum(int)));
    20.  
    21. m_listWidget->addItem("One");
    22. m_listWidget->addItem("Two");
    23.  
    24. m_layout->addWidget(m_line);
    25. m_layout->addWidget(m_label);
    26. m_layout->addWidget(m_listWidget);
    27.  
    28. m_widget->setLayout(m_layout);
    29. m_window.setCentralWidget(m_widget);
    30.  
    31. m_window.show();
    32. app.exec();
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 

    If you compile it and run, then click on the list widget, but not on some item, then switch to another window and back, now first item is selected and currentRowChanged is emitted. (This represents QLabel next to QLineEdit).

    QLineEdit is there only because to avoid focus of QListWidget on start of application.

  2. #2
    Join Date
    Oct 2015
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: When windows focused and QListWidget had focus, first item selected, bug?

    I have same issue, I think this is a bug.
    Workaround for this is to use selectionChanged(QItemSelection,QItemSelection) instead of currentRowChanged(QModelIndex,QModelIndex) and use next code:

    Qt Code:
    1. void YourClass::selectionChangedSlot(const QItemSelection& selection, const QItemSelection&)
    2. {
    3. QModelIndexList selected = selection.indexes();
    4. int row = selected.size() ? selected.first().row() : -1;
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by borzh62; 11th November 2015 at 14:45.

Similar Threads

  1. QListWidget selected item Row id.
    By nagabathula in forum Qt Programming
    Replies: 8
    Last Post: 20th December 2010, 18:08
  2. Replies: 2
    Last Post: 2nd December 2010, 12:27
  3. How to set selected item of a QListWidget?
    By Lawand in forum Qt Programming
    Replies: 9
    Last Post: 5th April 2009, 12:23
  4. Replies: 3
    Last Post: 7th November 2006, 09:35
  5. QListWidget/QTreeWidget, etc selected item color
    By Arthur in forum Qt Programming
    Replies: 4
    Last Post: 15th May 2006, 17:50

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.