Rename an item in Qlistview and Qlistwidget



  • I have an listview where several items present (item1..item9) . I want to remane items using doubleclick . If the renamed item name is similar to the already present item name then an warning message should be dispalyed. I tried this way

     connect(ui->listView_messages,SIGNAL(objectNameChanged(QString)),this,SLOT(renameListView(QString)));
    
    void Window::on_listView_Messages_doubleClicked()
    {
    disconnect(ui->listView_Messages,SIGNAL(objectNameChanged(QString)),this,SLOT(renameListView(QString)));
    ui->listView_Messages->setEditTriggers(QAbstractItemView::DoubleClicked);
    connect(ui->listView_Messages,SIGNAL(objectNameChanged(QString)),this,SLOT(renameListView(QString)));
    }
    
    void Window::renameListView(QString str)
    {
        for(int i=0;i<messageList.count();i++)
      {
       if(messageList.at(i).messageName== str)
       {
         QMessageBox *msgBox = new QMessageBox(QMessageBox::Warning,"Warning","Message Name already exists");
                msgBox->exec();
    
            }
        }
    }
    

    What is the proper way to do it ?


  • Lifetime Qt Champion

    Hi,

    You need to make your items editable before you can modify them



  • @SGaist
    using setEditTriggers cant we make it editable ?
    Ok now i have made it editable.
    can i rename using this

    connect(ui->listView_messages,SIGNAL(objectNameChanged(QString)),this,SLOT(renameListView(QString)));
    void Window::renameListView(QString str)
    {
        for(int i=0;i<messageList.count();i++)
      {
       if(messageList.at(i).messageName== str)
       {
         QMessageBox *msgBox = new QMessageBox(QMessageBox::Warning,"Warning","Message Name already exists");
                msgBox->exec();
    
            }
        }
    }

  • Lifetime Qt Champion

    It still depends on the underlaying model. In your case, it seems to be already OK.

    IIRC, if you need validation you should implement your own QStyledItemDelegate and add the validation to your editor



  • @SGaist
    I am not able to prevent the renaming to the already entered name .


  • Lifetime Qt Champion

    That's why you should handle that at the editor level not after


Log in to reply
 

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