Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Displaying partial text of a qlistwidgetitem
Qt 6.11 is out! See what's new in the release blog

Displaying partial text of a qlistwidgetitem

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 872 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phirestalker
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      michelson
      wrote on last edited by
      #2

      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*/)
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved