这个Qt代码有什么问题? [英] What is wrong with this Qt code?

查看:190
本文介绍了这个Qt代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读MVC教程,并想试用代码,但由于某种原因(我不能弄清楚),它是不工作。



此代码假设在QListWidget中显示当前目录的内容。

  #include< QApplication> 
#include< QFileSystemModel>
#include< QModelIndex>
#include< QListWidget>
#include< QListView>

int main(int argc,char * argv [])
{
QApplication a(argc,argv);

QFileSystemModel * model = new QFileSystemModel;
QString dir = QDir :: currentPath();
model-> setRootPath(dir);
QModelIndex parentIndex = model-> index(dir);
int numRows = model-> rowCount(parentIndex);
QListWidget * list = new QListWidget;
QListWidgetItem * newItem = new QListWidgetItem;

for(int row = 0; row< numRows; ++ row){
QModelIndex index = model-> index(row,0,parentIndex);
QString text = model-> data(index,Qt :: DisplayRole).toString();
newItem-> setText(text);
list-> insertItem(row,newItem);
}

list-> show();
return a.exec();
}


解决方案

/ p>

第一个 Frank Osterfeld的回答。移动:

  QListWidgetItem * newItem = new QListWidgetItem; 


$ b QFileSystemModel 的线程模型有关。从 QFileSystemModel 的文档:


与QDirModel不同,QFileSystemModel使用单独的线程以填充自身,所以它不会导致主线程挂起作为文件系统被查询。调用rowCount()将返回0,直到模型填充目录。



$ b b


注意:QFileSystemModel需要一个GUI应用程序的实例。


QFileSystemModel()将正常工作,直到之后 Qt事件循环正在运行(由 a.exec



在您的情况下, model-> rowCount(parentIndex)

替换 QFileSystemModel

code>与 QDirModel (并删除 model-> setRootPath(dir)不支持)填充列表。


I was reading MVC tutorial and wanted to try out the code, but for some reason (which I'm not able to figure out) it is not working.

This code is suppose to show contents of current directory in QListWidget.

#include <QApplication>
#include <QFileSystemModel>
#include <QModelIndex>
#include <QListWidget>
#include <QListView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QFileSystemModel *model = new QFileSystemModel;
    QString dir = QDir::currentPath();
    model->setRootPath(dir);
    QModelIndex parentIndex = model->index(dir);
    int numRows = model->rowCount(parentIndex);
    QListWidget *list = new QListWidget;
    QListWidgetItem *newItem = new QListWidgetItem;

    for(int row = 0; row < numRows; ++row) {
        QModelIndex index = model->index(row, 0, parentIndex);
        QString text = model->data(index, Qt::DisplayRole).toString();
        newItem->setText(text);
        list->insertItem(row, newItem);
    }

    list->show();
    return a.exec();
}

解决方案

There are 2 problems.

The first described by Frank Osterfeld's answer. Move:

QListWidgetItem *newItem = new QListWidgetItem;

into your loop.

The second has to do with QFileSystemModel's threading model. from the docs for QFileSystemModel:

Unlike the QDirModel, QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory.

and

Note: QFileSystemModel requires an instance of a GUI application.

I don't think QFileSystemModel() will work properly until after the Qt event loop is running (which is started by a.exec() in your example).

In your case, model->rowCount(parentIndex) returns 0, even though there are items in the directory (at least that's what it's doing on my test).

Replacing QFileSystemModel with QDirModel (and removing the model->setRootPath(dir) call, which QDirModel` doesn't support) populates the list.

这篇关于这个Qt代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆