[英]Qt: Linking my model data with a QListView
我正在編寫我的第一個Qt項目(因此對環境不熟悉),並且已經使用MVC設計模式構建了該項目。 這是一個非常基本的音符管理器/編輯器。 我有一個uiman類(“ UI管理器”),它負責我的UI,還有打開Notes數據庫的功能(這只是要打開的文本文件的列表)
void uiman::openDBDialog(){
QFileDialog dialog;
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setDirectory("/Users/myuserdir/Desktop");
dialog.setFilter(QDir::Files);
dialog.setWindowTitle("Open File");
dialog.setNameFilter("Textie Notes DB(*.db)");
dialog.exec();
QString pathDB = dialog.selectedFiles().first();
model = new notesModel();
model->setNotesDB(new QString(pathDB.toUtf8().constData()));
refreshAll();
}
到現在為止還挺好。 我選擇了數據庫的路徑,並將其提供給我的模型,該模型現在應管理其余部分。 現在, refreshAll()
函數應該采用我打開的列表,並在QListView中顯示它們,但是與QListWidget不同,我無法使用clear()
和append()
隨時解析文件並追加項目。 因此,如何處理從文件中構建名稱的向量(我想)並將其提供給QListView的方法?
抱歉,如果我不清楚,但是官方文檔還不夠清楚。
編輯:這是我的模型,nodesmodel,這是代碼。
notesModel::notesModel(QObject *parent) :
QFileSystemModel(parent)
{
QStringList noteList;
}
void notesModel::setNotesDB(QString *dbpath){
// open the notes database
databasepath = dbpath;
}
QFile* notesModel::getDB(){
if(this->dbFile == NULL)
this->dbFile = new QFile(databasepath- >toUtf8().constData());
return this->dbFile;
}
正如您已經注意到的,您沒有從查看器窗口小部件本身添加/刪除數據,這是在模型中完成的。
您可以使用setModel()方法設置模型。
如果要顯示一個簡單的QString列表,則可以使用:
QStringList strings;
strings << "String 1" << "String 2" << "String 3";
model = new QStringListModel(strings, parent);
view->setModel(model);
// model is managed by parent, and will be deleted when parent is deleted.
// If you create multiple models you might consider other memory management strategy
要讀取一個文本文件,其存儲在一個QStringList中的線條,看到這個答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.