直接上代码
QFileDialog dialog;
dialog.setOption(QFileDialog::DontUseNativeDialog);
auto treeView = dialog.findChild<QTreeView*>(QString("treeView"),Qt::FindChildrenRecursively);
if(treeView){
QStringList headerContents = getHeaderLabels(treeView);
qDebug()<<"header is "<< headerContents;
//header is ("Name", "Size", "Type", "Date Modified")
treeView->model()->sort(headerContents.indexOf("Name"), Qt::AscendingOrder);
}
if (dialog.exec() == QDialog::Accepted) {
// 处理选择的文件
}
QStringList getHeaderLabels(QTreeView *treeView) {
QStringList headerLabels;
if (treeView) {
auto model = treeView->model();
if (model) {
for (int i = 0; i < model->columnCount(); ++i) {
QString headerText = model->headerData(i, Qt::Horizontal).toString();
headerLabels << headerText;
}
}
}
return headerLabels;
}