在我的项目中,我有一个QFileDialog Box来选择任何文件夹。选择任何文件夹后,我会选择文件夹路径。现在我想使用路径将选定的文件夹及其子文件夹添加到QTreeWidget。 QTreeWidgets仅显示文件夹,当我点击任何文件夹或子文件夹时,所有文件都应显示在QTableWidgets中。
以下是代码:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
push=new QPushButton;
push->setText("Click");
connect(push,SIGNAL(clicked(bool)),this,SLOT(work()));
QIcon folderIcon(style()->standardPixmap(QStyle::SP_DirClosedIcon));
foldersTreeWidget = new QTreeWidget;
QStringList messageLabels;
messageLabels << tr("Subject") << tr("Sender") << tr("Date");
messagesTableWidget = new QTableWidget;
messagesTableWidget->setHorizontalHeaderLabels(messageLabels);
messagesTableWidget->resizeColumnToContents(0);
messagesTableWidget->resizeColumnToContents(1);
rightSplitter = new QSplitter(Qt::Vertical);
rightSplitter->addWidget(messagesTableWidget);
rightSplitter->addWidget(push);
rightSplitter->setStretchFactor(1, 1);
mainSplitter = new QSplitter(Qt::Horizontal);
mainSplitter->addWidget(foldersTreeWidget);
mainSplitter->addWidget(rightSplitter);
mainSplitter->setStretchFactor(1, 1);
setCentralWidget(mainSplitter);
}
void MainWindow::work()
{
dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
QIcon folderIcon(style()->standardPixmap(QStyle::SP_DirClosedIcon));
addFolder(folderIcon, dir);
}
void MainWindow::addFolder(const QIcon &icon, const QString &name)
{
QTreeWidgetItem *root;
if (foldersTreeWidget->topLevelItemCount() == 0) {
root = new QTreeWidgetItem(foldersTreeWidget);
root->setText(0, tr("Mail"));
foldersTreeWidget->setItemExpanded(root, true);
} else {
root = foldersTreeWidget->topLevelItem(0);
}
QTreeWidgetItem *newItem = new QTreeWidgetItem(root);
newItem->setText(0, name);
newItem->setIcon(0, icon);
if (!foldersTreeWidget->currentItem())
foldersTreeWidget->setCurrentItem(newItem);
}
我还没有完成所有代码部分。主要问题是:
-> I am getting folder path using QFileDialog
-> I want to use the path to add folder and it's subfolders to QTreeWidgets
-> When I click on any folder in QTreeWidgets, I want to display files along with some information like (location, size, name) in QTableWidget.
还有其他方法可以达到这个目的吗?
感谢您的时间。