C++ (Qt) connect( treeView, SIGNAL( clicked( QModelIndex ) ), this, SLOT( setNameTree( QModelIndex ) ) ); connect( cFileSystemModel, SIGNAL( fileRenamed( QString, QString, QString ) ), this, SLOT( changeCurrentIndex() ) );}...... cFileSystemModel->mkdir(index, newNameFolder); index = cFileSystemModel->index(newFullNameFolder); treeView->setCurrentIndex(index); treeView->edit(index); setWindowTitle(newNameFolder);} void MainWindow::setNameTree(const QModelIndex &index){ setWindowTitle( cFileSystemModel->fileInfo(index).baseName() );} void MainWindow::changeCurrentIndex(){ setNameTree( treeView->currentIndex() );}
C++ (Qt)void MainWindow::keyReleaseEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Return) { setNameTree( treeView->currentIndex() ); }} void MainWindow::mouseReleaseEvent(QMouseEvent *event){ if(event->button() == Qt::LeftButton) { setNameTree( treeView->currentIndex() ); }}
C++ (Qt)#include <QDebug> #include "MainWindow.h"#include "ui_MainWindow.h" CFileSystemView::CFileSystemView(QWidget *parent) : QTreeView(parent){} CFileSystemView::~CFileSystemView(){} MouseFilter::MouseFilter(QObject *object) : QObject(object){} MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); treeView = new CFileSystemView; cFileSystemModel = new CFileSystemModel; treeMouseFilter = new MouseFilter(treeView); treeView->installEventFilter(treeMouseFilter); treeView->setModel(cFileSystemModel); setCentralWidget(treeView); treeView->setSelectionMode( QAbstractItemView::ExtendedSelection ); treeView->setDragEnabled(true); treeView->setAcceptDrops(true); treeView->setDropIndicatorShown(true); treeView->setDragDropMode( QAbstractItemView::InternalMove ); treeView->setContextMenuPolicy( Qt::CustomContextMenu ); connect( treeView, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( showCustomMenu( const QPoint & ) ) );} MainWindow::~MainWindow(){ delete ui;} void MainWindow::createNewFolder(){ QModelIndex index = treeView->currentIndex(); QString rootForNewFolder; if(!index.isValid()) { index = treeView->rootIndex(); rootForNewFolder = cFileSystemModel->fileInfo(index).absoluteDir().absolutePath(); } else if(cFileSystemModel->fileInfo(index).isFile()) { rootForNewFolder = cFileSystemModel->fileInfo(index).absoluteDir().absolutePath(); } else { rootForNewFolder = cFileSystemModel->filePath(index); } index = cFileSystemModel->index(rootForNewFolder); QString newNameFolder = "FOLDER"; QString newFullNameFolder = cFileSystemModel->filePath(index) + "/FOLDER"; int nName = 0; while( QDir( newFullNameFolder ).exists() ) { newNameFolder = "FOLDER" + QString::number(++nName); newFullNameFolder = cFileSystemModel->filePath(index) + "/" + newNameFolder; } cFileSystemModel->mkdir(index, newNameFolder); index = cFileSystemModel->index(newFullNameFolder); treeView->setCurrentIndex(index); treeView->edit(index); setWindowTitle(newNameFolder);} void MainWindow::setNameTree(){ setWindowTitle( cFileSystemModel->fileInfo(treeView->currentIndex()).baseName() );} void MainWindow::keyReleaseEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Return) setNameTree(); } void MainWindow::showCustomMenu(QPoint const & pos){ m_managerMenu = new QMenu( treeView ); m_managerMenu->addAction( tr( "New Folder" ), this, SLOT(createNewFolder()) ); m_managerMenu->exec( treeView->mapToGlobal( pos ) );} bool MouseFilter::eventFilter(QObject* object, QEvent* event){ if( event->type() == QEvent::MouseButtonPress ) { QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); if(mouseEvent->button() == Qt::LeftButton) { qDebug() << "FILTER"; return true; } else return false; } return false;}
C++ (Qt)#include <QDebug> #include "MainWindow.h"#include "ui_MainWindow.h" CFileSystemView::CFileSystemView(QWidget *parent) : QTreeView(parent){} CFileSystemView::~CFileSystemView(){} MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); treeView = new CFileSystemView(this); cFileSystemModel = new CFileSystemModel(this); treeView->setModel(cFileSystemModel); setCentralWidget(treeView); treeView->setSelectionMode( QAbstractItemView::ExtendedSelection ); treeView->setDragEnabled(true); treeView->setAcceptDrops(true); treeView->setDropIndicatorShown(true); treeView->setDragDropMode( QAbstractItemView::InternalMove ); treeView->setContextMenuPolicy( Qt::CustomContextMenu ); connect( treeView, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( showCustomMenu( const QPoint & ) ) ); connect( treeView, SIGNAL( clicked( QModelIndex ) ), this, SLOT( setNameTree() ) );; connect( treeView, SIGNAL( reName() ), this, SLOT( setNameTree() ) );} MainWindow::~MainWindow(){ delete ui;} void MainWindow::createNewFolder(){ QModelIndex index = treeView->currentIndex(); QString rootForNewFolder; if(!index.isValid()) { index = treeView->rootIndex(); rootForNewFolder = cFileSystemModel->fileInfo(index).absoluteDir().absolutePath(); } else if(cFileSystemModel->fileInfo(index).isFile()) { rootForNewFolder = cFileSystemModel->fileInfo(index).absoluteDir().absolutePath(); } else { rootForNewFolder = cFileSystemModel->filePath(index); } index = cFileSystemModel->index(rootForNewFolder); QString newNameFolder = "FOLDER"; QString newFullNameFolder = cFileSystemModel->filePath(index) + "/FOLDER"; int nName = 0; while( QDir( newFullNameFolder ).exists() ) { newNameFolder = "FOLDER" + QString::number(++nName); newFullNameFolder = cFileSystemModel->filePath(index) + "/" + newNameFolder; } cFileSystemModel->mkdir(index, newNameFolder); index = cFileSystemModel->index(newFullNameFolder); treeView->setCurrentIndex(index); treeView->edit(index); setWindowTitle(newNameFolder);} void MainWindow::setNameTree(){ setWindowTitle( cFileSystemModel->fileInfo(treeView->currentIndex()).baseName() );} void MainWindow::keyReleaseEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Return) { qDebug() << "NAME" << cFileSystemModel->fileInfo(treeView->currentIndex()).baseName(); setNameTree(); }} void MainWindow::showCustomMenu(QPoint const & pos){ m_managerMenu = new QMenu( treeView ); m_managerMenu->addAction( tr( "New Folder" ), this, SLOT(createNewFolder()) ); m_managerMenu->exec( treeView->mapToGlobal( pos ) );} void CFileSystemView::mouseReleaseEvent(QMouseEvent *event){ if(event->button() == Qt::LeftButton) { qDebug() << "NAME" << currentIndex(); emit reName(); } else QTreeView::mouseReleaseEvent(event);}
C++ (Qt)void CFileSystemView::mousePressEvent(QMouseEvent *event){ QTreeView::mousePressEvent(event); emit reName();}