Results 1 to 5 of 5

Thread: How do I tell when a QListWidgetItem is checked?

  1. #1
    Join Date
    May 2010
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How do I tell when a QListWidgetItem is checked?

    I am using the following code to create QListWidgetItems:
    Qt Code:
    1. newItem->setText("Item");
    2. newItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    3. newItem->setCheckState(Qt::Unchecked);
    4. ui.listImages->addItem(newItem);
    To copy to clipboard, switch view to plain text mode 

    I now want to do something like:
    Qt Code:
    1. connect(newItem, SIGNAL(toggled(bool)), this, SLOT(handleItemToggle(bool)));
    To copy to clipboard, switch view to plain text mode 
    The problem is that QListWidgetItem does not expose ANY signals, and none of the of the QListWidget signals appear to have anything to do with check marks. Any ideas?

    Thanks!

  2. #2
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I tell when a QListWidgetItem is checked?

    QListWidget does seem to offer the itemClicked and itemPressed signals. You could connect one of these signals to your slot and then check the items clicked state with QListWidgetItem's checkState function. Based on the check state, you could then do whatever you were going to do with the item.

    Any sophisticated behavior beyond that may require you to code up an actual model though.

    Hope that helps (and works - I normally use the model/view interface instead of the convenience widgets).

    (EDIT) Note: Since changing the check state of an item requires a mouse click, it seems convenient to tie it to the itemClicked (or Pressed) signals which are provided. It seems hackish, but when that's all you've got to work with... Otherwise, you would have to set up some sort of polling function based on a timer to check all of the items' check states, and that seems to be a lot of work.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How do I tell when a QListWidgetItem is checked?

    QListWidgetItem does not have any signal at all! You have to use the signal QListWidget::itemChanged() and check there if your item is checked or unchecked.

  4. #4
    Join Date
    May 2010
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default [SOLVED] How do I tell when a QListWidgetItem is checked?

    Posted for the benefit of future readers:
    This solved my issues

    Qt Code:
    1. void LisaViewer::on_listImages_itemChanged(QListWidgetItem* changed)
    2. {
    3. bool checked = changed->checkState() == Qt::Checked;
    4. int index = 0;
    5. for (; ui.listImages->item(index) != changed; index++) ;
    6. QMessageBox::information(NULL,"Clicked", "You " + QString(checked ? "checked" : "unchecked") + " item " + QString::number(index + 1) + ".");
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [SOLVED] How do I tell when a QListWidgetItem is checked?

    Instead of your for loop you can use QListWidget::row(const QListWidgetItem * item).

Similar Threads

  1. Which QCheckBox has been checked?
    By alexandernst in forum Newbie
    Replies: 2
    Last Post: 14th September 2009, 10:09
  2. Delete if row checked
    By wirasto in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2009, 14:16
  3. Two groupbox, if checked one, uncheck other
    By webquinty in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2009, 23:40
  4. Checked item in QTreeWidget?
    By vishal.chauhan in forum Qt Programming
    Replies: 18
    Last Post: 3rd January 2008, 20:18
  5. Which QCheckBox is checked?
    By Craig.Smith in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 14:52

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.