PDA

View Full Version : QListWidget Question



bbad68
19th January 2010, 18:40
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!

wysota
19th January 2010, 20:13
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.

axeljaeger
19th January 2010, 21:14
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.

bbad68
20th January 2010, 00:11
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);
}
}

wysota
20th January 2010, 01:25
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.

boudie
20th January 2010, 15:09
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.

bbad68
22nd January 2010, 00:26
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:

ui.layerList->setCurrentItem(layer7);
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.

wysota
22nd January 2010, 00:32
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?

bbad68
22nd January 2010, 01:03
Here's a snippet of my code (the last 3 lines cane be ignored):


void warmup::on_addLayerButton_clicked()
{
int nRows = ui.layerList->count();

QListWidgetItem *newLayer = new QListWidgetItem;
Qt::ItemFlags flags = Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
newLayer->setText(tr("Layer %1").arg(nRows+1));
ui.layerList->addItem(newLayer);
newLayer->setFlags(flags);

clearLayerData();
ui.layerList->setCurrentItem(newLayer);
getLayerData(ui.layerList->row(newLayer));

}

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 :)

axeljaeger
22nd January 2010, 08:53
Try setCurrentItem(newLayer, QItemSelectionModel::Select)

bbad68
22nd January 2010, 17:33
Try setCurrentItem(newLayer, QItemSelectionModel::Select)

Hmm, still doesn't seem to be working :(

wysota
23rd January 2010, 08:52
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.