QTreeView + QFileSystemModel rename implementation, qtreeview

Source: Internet
Author: User

QTreeView + QFileSystemModel rename implementation, qtreeview

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!


How to Use python to implement QTreeView or QTreeWidget?

Use QFileSystemModel:
# Include <QApplication> # include <QFileSystemModel> # include <QTreeView> int main (int argc, char * argv []) {QApplication app (argc, argv); QFileSystemModel model; model. setRootPath (QDir: rootPath (); QTreeView tree; tree. setModel (& model); tree. setRootIndex (model. index (QDir: rootPath (); tree. setHeaderHidden (true); tree. setColumnHidden (1, true); tree. setColumnHidden (2, true); tree. setColumnHidden (3, true); tree. show (); return app.exe c ();}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.