Previously, when I wrote the markdown editor, I wrote a simple resource Browser Based on qtreeview + qfilesystemmodel. Now I want to add a RENAME function to it.
In fact, it is relatively simple to implement rename. First, set setedittriggers (q1_actitemview: editkeypressed) in qtreeview. In this way, you can press F2 on the disk to rename. however, if you want to have a shortcut menu with rename, for example:
What should we do. you need to add a menu here. Of course, it is not the focus. The focus is on how to trigger the RENAME operation. After setting setedittriggers, pressing F2 on the keyboard can trigger the rename. however, we do not know what happened after pressing F2. of course, we can not care about what happened after pressing F2. Since pressing F2 can rename, We will generate an event that can be pressed F2. The code snippet is as follows:
Connect (menu _, & projectexplorermenu: signalrename, [this] () {qstring Path = model _-> filepath (currentindex (); If (path. isnull () {qdebug () <"file path is null"; return ;}// the qdebug () is effective... qkeyevent event (qevent: keypress, QT: key_f2, QT: nomodifier); keypressevent (& event );});
In this example, when the menu selects rename, it sends a signalrename signal. A qkeyevent object is instantiated in the corresponding slot and its key is set to QT: key_f2, that is, the event that generates the press F2, and then transmits the event to the Event Filter keypressevent that handles the press on the keyboard. achieve this goal. however, this is not too clean. If you want to clean it, you can check the source code of qtreeview. in the keypressevent of qtreeview:
void QTreeView::keyPressEvent(QKeyEvent *event){ Q_D(QTreeView); QModelIndex current = currentIndex(); //this is the management of the expansion if (d->isIndexValid(current) && d->model && d->itemsExpandable) { switch (event->key()) { case Qt::Key_Asterisk: { QStack<QModelIndex> parents; parents.push(current); while (!parents.isEmpty()) { QModelIndex parent = parents.pop(); for (int row = 0; row < d->model->rowCount(parent); ++row) { QModelIndex child = d->model->index(row, 0, parent); if (!d->isIndexValid(child)) break; parents.push(child); expand(child); } } expand(current); break; } case Qt::Key_Plus: expand(current); break; case Qt::Key_Minus: collapse(current); break; } } QAbstractItemView::keyPressEvent(event);}
The code is very simple, just a few lines. After you press +,-, and * on the keyboard. + expand,-contract, * recursively expand other Keyboard Events and throw them to the base class for processing (I don't know if this is the case if I don't look at the code ........), let's take a look at what qiniactitemview has done to F2. Here the code is more than a few hundred lines. Although the code is long, the processing part of F2 is still very simple:
void QAbstractItemView::keyPressEvent(QKeyEvent *event){ ... switch (event->key()) { case Qt::Key_F2: if (!edit(currentIndex(), EditKeyPressed, event)) event->ignore(); break; ... } ...}
Originally, the Edit function was used for processing when F2 was pressed. Therefore, the code for directly generating a qkeyevent instance and passing it to keypressevent can be changed:
connect(menu_, &ProjectExplorerMenu::signalRename, [this](){ QString path = model_->filePath(currentIndex()); if(path.isNull()) { qDebug() << "file path is null"; return; } QKeyEvent event(QEvent::KeyPress, Qt::Key_F2, Qt::NoModifier); edit(currentIndex(), EditKeyPressed, &event);});
A lot of times there are no items found in the document. You can see the code and there is no secret under the source code!
Implementation of qtreeview + qfilesystemmodel rename