![](/img/trans.png)
[英]Get QModelIndex from filepath and filename in QFileSystemModel
[英]Getting access to filepath with QFileSystemModel
我在QFileSystemModel
中有一個QListView
,它使我可以選擇文件系統中的項。
選擇項目時,我想在QMessageBox
返回文件路徑。
到目前為止,我有以下代碼:
filemodel = new QFileSystemModel(this);
filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
filemodel->setNameFilters(filters);
filemodel->setNameFilterDisables(false);
filemodel->setRootPath(sPath);
//get file path
QString filepath = filemodel->fileName(index);
QMessageBox::information(this, "title", filepath);
ui->listView->setModel(filemodel);
這將創建文件模型。
我收到此錯誤:
mainwindow.cpp:46:錯誤:沒有匹配的函數調用'QFileSystemModel :: fileName(char *(&)(const char *,int))'
這是解決這個問題的正確方法嗎? 選擇一個項目時返回文件路徑?
編輯@ dunc123
在構造函數中:
connect(ui->listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selectedFile(QItemSelection one, QItemSelection two)));
selectedFile:
void MainWindow::selectedFile(QItemSelection one, QItemSelection two){
QString file_name = filemodel->fileName(ui->listView->selectionModel()->currentIndex());
QMessageBox::information(this, "title", file_name);
}
它會生成並運行,但是當我單擊文件時,出現以下錯誤:
Object::connect: No such slot MainWindow::selectedFile(QItemSelection one, QItemSelection two) in ../Images/mainwindow.cpp:26
Object :: connect:(接收者名稱:“ MainWindow”)
我假設我傳遞變量的方式是錯誤的?
你能幫我嗎?
您需要將QModelIndex對象傳遞給QFileSystemModel的fileName方法。 似乎符號“索引”正在作為函數解析。 猜測您的類中有一個名為index的成員函數。
編輯:這里更大的問題是,當您在QListView中選擇一個項目時,您希望某些事情發生,但是您要在構造函數中放入處理此代碼的代碼。 您需要在班級中創建一個插槽,並將其連接到選定項目時發出的信號。
connect(ui->listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &), this, SLOT(...));
在此插槽中,應調用fileName方法並顯示該信息。 您還需要使filemodel成為類的成員變量,以便您可以從插槽中訪問它。
編輯2:調用connect
時指定插槽的方式不正確,應為:
connect(ui->listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selectedFile(QItemSelection , QItemSelection)));
但是,由於您沒有在插槽中使用任何一個參數,因此實際上您可以將所有參數從插槽中一起刪除,例如,在標頭中將其定義為:
void selectedFile();
並使用以下命令進行連接:
connect(ui->listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selectedFile()));
QT將計算出您不需要信號中的任何一個參數。
您可以使用QItemSelectionModel :: selectedIndexes()函數獲取所選索引的列表。
以下是如何使用它的示例:
QModelIndexList list = ui->listView->selectionModel()->selectedIndexes();
foreach (QModelIndex index, list)
{
QString file_name = fileModel->fileName(index);
}
或者,如果您只能選擇一個項目,則可以使用QItemSelectionModel :: currentIndex函數,如下所示:
QString file_name = fileMode->fileName(ui->listView->selectionModel()->currentIndex());
您也可以將QItemSelectionModel :: selectionChanged信號連接到插槽,並以類似的方式使用它。 QListView
具有selectionModel()函數,可用於檢索QItemSelectionModel
對象。 QItemSelection
具有一個index()函數,該函數返回QModelIndexList
。
QString filepath = filemodel->fileName(index);
mainwindow.cpp:46: error: no matching function for call to 'QFileSystemModel::fileName(char* (&)(const char*, int))'
看起來“索引”是一個函數,而不是QModelIndex ...
您可以使用連接到模型信號“ currentChanged(QModelIndex,QModelIndex)”的插槽來獲取新的選定索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.