Search code examples
rubyqtqtruby

Qtruby ListWidgetItem has blank icon after adding data


I am working on a GUI using qtruby. I have a ListWidget that I am filling with ListWidgetItems. So far these have just contained the text that I wanted to display and everything worked fine. I wanted these items to also hold some hidden data to use when they are clicked on. I used ListWidgetItem.setData() to set the data and I can get the data from it when it is clicked just fine. However once I add the data the text being displayed is now shifted over to the right about 4 spaces. When I click on it a little dotted box appears around the text but not the space that was added. It looks like it is a space for an icon but I have not set any icon and I don't want one. How do I get rid of this extra space so that items containing data are lined up with everything?

The code is very straight forward:

item = Qt::ListWidgetItem.new( @grain_strings[index] )
item.setFont( @font )
# TODO this is causing the text to be indented, removing it removes the indent 
item.setData( 1, Qt::Variant.from_value( grain.type ) )
@item_list.insertItem( @end_of_grains+1, item )

Solution

  • The first argument to setData is the role number and for some reason you chose to set it to 1. The documentation says: "The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or QPixmap)". So you are telling Qt to display an icon but you are not giving it a valid icon object.

    Try setting the role to Qt::UserRole, which is "The first role that can be used for application-specific purposes." I am not sure how to access that constant from Ruby. If it is not provided by the qtruby gem, you could use 0x100 I suppose.