Results 1 to 12 of 12

Thread: QListWidget Question

  1. #1
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget Question

    Hi,

    How do I apply a specific property to all of the QListWidget items within a list? I have included a QListWidget on my form in Qt Designer, and I want to be able to edit the name of an item just by double clicking. The form I created has only one item within the list when it is first created, but there are 'add and 'remove' buttons that allow the user to dynamically add/remove more items. While the first item in the list (the one that is automatically created) can be edited, all the items that are added during with the add/remove buttons cannot. What am I doing wrong?

    Oh! While I'm here, I also have another question. How can I make a table with two columns, the first which acts essentially as a lineEdit (in which the user can enter the name of a property, ie 'volume'), and the second which is a double spin-box with a numerical value? I also want to allow the user to add new rows to this table during run-time. I am fairly new to Qt (and C++ programming in general), so simple explanations would be really helpful.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListWidget Question

    Quote Originally Posted by bbad68 View Post
    How do I apply a specific property to all of the QListWidget items within a list? I have included a QListWidget on my form in Qt Designer, and I want to be able to edit the name of an item just by double clicking. The form I created has only one item within the list when it is first created, but there are 'add and 'remove' buttons that allow the user to dynamically add/remove more items. While the first item in the list (the one that is automatically created) can be edited, all the items that are added during with the add/remove buttons cannot. What am I doing wrong?
    Those items need to have the Qt::ItemIsEditable flag set.

    Oh! While I'm here, I also have another question. How can I make a table with two columns, the first which acts essentially as a lineEdit (in which the user can enter the name of a property, ie 'volume'), and the second which is a double spin-box with a numerical value? I also want to allow the user to add new rows to this table during run-time. I am fairly new to Qt (and C++ programming in general), so simple explanations would be really helpful.
    Take a look at the "spin box delegate" example in Qt documentation.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: QListWidget Question

    I suggest not to use a QListWidget but a QListView and implement your own model. While a QListWidget looks more convenient on the first hand, later it is harder to do serious business with it. For example, to make all items editable in a QListModel simply return Qt::ItemIsEditable in the flags-method of the list model.
    It's nice to be important but it's more important to be nice.

  4. #4
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget Question

    Thanks for the advice!

    Okay, I managed to get the list items to be editable, however one other problem remains; let me explain. My form includes a 'Remove' button to remove items from the list. When I click this button, the selected item is removed as expected. However, if I click the button again, even when no item is selected, another item will be deleted. I would like to modify my code so that after removing an item, the item that was above it gets highlighted. Also, if no item is highlighted, I would like the remove button to be disabled. Forgive me for my newb-ness.

    Here is a sample of my code (I hope that it isn't too horrible to look at):

    void warmup::on_removeLayerButton_clicked()
    {
    int currentRow = ui.layerList->currentRow();
    ui.layerList->takeItem(currentRow);


    if( ui.layerList->count() == 0)
    {
    ui.removeLayerButton->setEnabled(false);
    ui.insertLayerButton->setEnabled(false);
    }
    }

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListWidget Question

    I usually connect to a signal QListWidget::currentItemChanged() or QListWidget::currentRowChanged() (or equivalent for model based approach). Enabling or disabling the remove button is trivial then as you just check if the new current item is valid (row>=0 or item!=0). If you want to work on selections, connect to QListWidget::itemSelectionChanged(). You can then query for selected items and do the same thing. For highlighting a row use setCurrentItem() or an equivalent for selection.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QListWidget Question

    In almost all of my apps I put a function setButtons() where I enable/disable all kinds of controls.
    After every action, like inserting records, opening files, closing files, editing fields or making coffee etc, I call setButtons() to put every control in a valid state. Once used to this approach, you'll never have to think about how to keep things in sync again.

  7. #7
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget Question

    For highlighting a row use setCurrentItem() or an equivalent for selection.
    I'm not sure what I'm doing wrong, but even when I set the current item, it does not get highlighted.

    For example:
    Qt Code:
    1. ui.layerList->setCurrentItem(layer7);
    To copy to clipboard, switch view to plain text mode 
    After doing this, the current item is set to layer7, but it isn't highlighted.
    I also tried QListWidgetItem::setSelected(true) but it still doesnt get highlighted.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListWidget Question

    We'd have to see the bigger picture. We don't even know what "layer7" is What is the selection mode of the widget? Is the item selectable at all, what does flags() return for it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget Question

    Here's a snippet of my code (the last 3 lines cane be ignored):

    Qt Code:
    1. void warmup::on_addLayerButton_clicked()
    2. {
    3. int nRows = ui.layerList->count();
    4.  
    5. Qt::ItemFlags flags = Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    6. newLayer->setText(tr("Layer %1").arg(nRows+1));
    7. ui.layerList->addItem(newLayer);
    8. newLayer->setFlags(flags);
    9.  
    10. clearLayerData();
    11. ui.layerList->setCurrentItem(newLayer);
    12. getLayerData(ui.layerList->row(newLayer));
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    I have a QListWidget 'layerList', and when the 'Add' button is clicked, a new QListWidgetItem 'newLayer' is created. I'm trying to figure out how to make it so that when a 'newLayer' is created, is it automatically set as the current item AND highlighted. So far all I have managed to do is set it as the current item. The flags for the 'newLayer' are listed in the code above, the selection mode for 'layerList' is set to SingleSelection, and the selection behavior for 'layerList' is SelectItems. I hope this helps clear up what I'm asking

  10. #10
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: QListWidget Question

    Try setCurrentItem(newLayer, QItemSelectionModel::Select)
    It's nice to be important but it's more important to be nice.

  11. #11
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget Question

    Quote Originally Posted by axeljaeger View Post
    Try setCurrentItem(newLayer, QItemSelectionModel::Select)
    Hmm, still doesn't seem to be working

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListWidget Question

    To me it seems you are doing something wrong if setSelected(true) didn't work. Either the item is not selectable or your code is never ran or something unselects the item after you select it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. New member and a question about QListWidget
    By Pokepasa in forum Qt Programming
    Replies: 2
    Last Post: 4th November 2009, 10:24
  2. QListWidget question
    By rishid in forum Qt Programming
    Replies: 8
    Last Post: 18th January 2008, 13:50
  3. extracting a item from a QListWidget question
    By impeteperry in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2007, 09:08
  4. question regarding qlistwidget
    By amulya in forum Qt Programming
    Replies: 1
    Last Post: 6th October 2006, 07:17
  5. Replies: 1
    Last Post: 7th April 2006, 11:13

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
  •  
Qt is a trademark of The Qt Company.