【问题标题】:how to set a value for the listwidget item?如何为列表小部件项设置值?
【发布时间】:2011-04-01 05:07:35
【问题描述】:

我创建一个列表小部件,在其中添加项目...我的项目是文件名。有什么方法可以存储这个文件名的文件路径吗?我想知道如何为列表小部件中的项目设置特定值?

【问题讨论】:

    标签: qt qlistview qlistwidget


    【解决方案1】:

    你可以使用:

    ListWidgetItem::setData(int role,Qvariant data);
    

    存储您的filePath;

    像这样:

    QString fileName = "hello.txt";
    QString filePath = "/home/user/hello.txt";
    
    //Adding to listWidget
    QListWidgetItem *item =new QListWidgetItem();
    
    item->setText(fileName);
    item->setData(1,filePath);//here role is set as 1
    
    listWidget->addItem(item);
    
    //For taking back that filePath
    
    QString filePath =   listWidget->currentItem()->data(1); //note::: role equals 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 2015-03-24
      • 2011-01-14
      • 2013-08-10
      • 2020-12-08
      相关资源
      最近更新 更多