QFileSystemModel *model = new QFileSystemModel(this); model->setHeaderData(0, Qt::Vertical, tr("Название"), Qt::DisplayRole); model->setHeaderData(1, Qt::Vertical, tr("Размер"), Qt::DisplayRole); model->setHeaderData(2, Qt::Vertical, tr("Тип"), Qt::DisplayRole); model->setHeaderData(2, Qt::Vertical, tr("Дата изменения"), Qt::DisplayRole); QModelIndex index; index = model->setRootPath("P:\\"); ui->treeView->setModel(model); ui->treeView->setRootIndex(index);
C++ (Qt)#include <QFileSystemModel>#include <QTreeView>#include <QHeaderView>#include <QMap> class FileSystemModel: public QFileSystemModel {private: QMap<int, QVariant> headers; public: bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { headers[section] = value; return true; } QVariant headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { return headers[section]; } return QVariant(); }}; int main(int argc, char *argv[]){ QApplication a(argc, argv); QFileSystemModel *model = new FileSystemModel(); model->setHeaderData(0, Qt::Horizontal, QObject::tr("Название"), Qt::DisplayRole); model->setHeaderData(1, Qt::Horizontal, QObject::tr("Размер"), Qt::DisplayRole); model->setHeaderData(2, Qt::Horizontal, QObject::tr("Тип"), Qt::DisplayRole); model->setHeaderData(3, Qt::Horizontal, QObject::tr("Дата изменения"), Qt::DisplayRole); QTreeView* treeView = new QTreeView(); treeView->setModel(model); treeView->setRootIndex(model->setRootPath("C:\\")); treeView->show(); return a.exec();}
C++ (Qt)QTranslator * translator = new QTranslator();translator->load( fileInfo.dir().path() + "/" + fileName );qApp->installTranslator( translator );
void MyWindow::changeEvent(QEvent *event){ if(event->type() == QEvent::LanguageChange) { mainwindow->ui->retranslateUi(mainwindow); mainwindow->ui->treeView->setLocale(my_win->locale()); } else QMainWindow::changeEvent(event);}
C++ (Qt)#include <QApplication>#include <QTreeView>#include <QFileSystemModel>#include <QTranslator>#include <QLibraryInfo> int main(int argc, char *argv[]){ QApplication app(argc, argv); QTranslator translator; translator.load("qt_ru", QLibraryInfo::location(QLibraryInfo::TranslationsPath)); qApp->installTranslator(&translator); QFileSystemModel model; QTreeView treeView; treeView.setModel(&model); treeView.setRootIndex(model.setRootPath("C:\\")); treeView.show(); return app.exec();}