1
votes

I have create a QTreeView and which is filed by QTreeWidgetItem. My application is a kind of file browser.

ViewTree is defined as below:

void MainWindow::createTreeView()
{
    TreeViewSection = new QWidget();

    QVBoxLayout *TreeViewLayout = new QVBoxLayout(TreeViewSection);

    MyTree = new TreeWidget();
    contextMenu = new QMenu(MyTree);
    MyTree->setContextMenuPolicy(Qt::ActionsContextMenu);

    addFolderAction = new QAction("Add Folder", contextMenu);
    MyTree->addAction(addFolderAction);
    connect(addFolderAction, SIGNAL(triggered()),this,SLOT(onAddFolderAction()));

    deleteAction = new QAction("Delete", contextMenu);
    MyTree->addAction(deleteAction);
    connect(deleteAction, SIGNAL(triggered()),this,SLOT(onDeleteAction()));

    MyTree->setSortingEnabled(true);
    MyTree->setColumnWidth(0, 400);

    headerItem = new QTreeWidgetItem();

    headerItem->setText(0,QString("File Name"));
    headerItem->setText(1,QString("Last Modified"));
    headerItem->setText(2,QString("Size"));
    MyTree->setHeaderItem(headerItem);

    MyTree->setAcceptDrops(true);
    MyTree->setDragEnabled(true);
    MyTree->setDragDropMode(QAbstractItemView::InternalMove);
    MyTree->setDefaultDropAction(Qt::MoveAction);
    MyTree->setDropIndicatorShown(true);
    MyTree->setDragDropOverwriteMode(true);
    MyTree->setSelectionMode(QAbstractItemView::SingleSelection);

    TreeViewLayout->addWidget(MyTree);

}

I have added an "Add Folder" Button, as defined below for the action:

void MainWindow::onAddFolderAction()
{
    QList<QTreeWidgetItem *> item;
    uint32_t index;

    item = MyTree->selectedItems();
    if (item.empty())
        index = 0;
    else {
        QString str = item[0]->text(0);
        QByteArray latin_str = str.toLatin1();
        char *utf8_text = latin_str.data();

        index = m_device.getIdByName(utf8_text);
    }
    if(m_device.isFolder(index) == true) {
        QTreeWidgetItem* child = new QTreeWidgetItem();
        child->setText(0, "NewFolder");
        child->setText(1, "--");
        child->setText(2, "--");

        item[0]->addChild(child);
        item[0]->setExpanded(true);
      //item[0]->setSelected();

// MyTree->edit(selectedItem());

        m_device.CreateNewFolder("New Folder", index);
    } 
}

As you see, by default I create a "NewFolder" which on a mtp FS system.

What I want to do it before creating the folder, I would like to be able to rename "NewFolder" to what the user want. I don't want to create a pop-up to enter text. What I want is a "kind of renaming" behavior. The folder appears in the treeview, the new folder is selected and the text is allowed to be changed directly. Like on Finder when you made a single click on the folder name, it become selected, and user can change the name in the finder without any pop-up

Thanks

1
IMO this is data model problem. Just set flag Qt::ItemIsEditable on item of data model and default behavior of QTreeView should do the trick.Marek R

1 Answers

1
votes

If you want to edit the item right after creating it you should be able to achieve this behaviour by simply calling the editor on it:

QTreeWidgetItem* child = new QTreeWidgetItem();
child->setText(0, "NewFolder");
child->setText(1, "--");
child->setText(2, "--");
child->setFlags(child->flags() | Qt::ItemIsEditable);

item[0]->addChild(child);
item[0]->setExpanded(true);

MyTree->editItem(child);