Displaying partial text of a qlistwidgetitem



  • I have a qlistwidget with items that hold the full path of a file that needs to be either viewed or deleted. So I NEED that full path from the qlistwidgetitem. For aesthetics I would like to only DISPLAY the filename and not the full path in the list while still being able to get the full path from the item (for instance in the itemdoubleclicked signal. Is there a way to do this? Is there another way, like will the qlistwidgetitem also store variable data attached that it does not display?



  • Try adding items with custom Role like:

    //somwhere in header
    enum UserRole 
    {
        FilenameRole,
        FilepathRole
    };
    
    //where you fill your list
    QFile file =... //file you want to add
    QFileInfo info(file);
    QListWidget list = ... // this one is probably initialized somwhere
    QListWidgetItem *item = new QListWidgetItem;
    item->setData(Qt:DisplayRole, info.fileName().); 
    item->setData(Qt::UserRole + FilenameRole, info.fileName().); 
    item->setData(Qt::UserRole + FilepathRole, info.absoluteFilePath());
    

    Access:

    QString data = list.item(row).data(Qt::UserRole + FilenameRole /*Filepath*/)
    

  • Lifetime Qt Champion

    Hi,

    You can use a QStyledItemDelegate and reimplement the displayText to do what you want.

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.