I have a QString fileName which stores in it a path to a subdirectory (eg. C:/Qt). I also have a treeView inside a dialog box.
Upon starting the application, we browse a file and once the file/folder is selected, I save the path of the file/folder in fileName string. Upon clicking "OK" (QPushButton), I intend to open a new Dialog Box divided into a treeView displaying the subdirectory with the selected folder as the root node of the tree, and a listView displaying all the files/folders within that folder and nothing if empty.
I used the following QFileSystemModel functions to display the C:/ directory (only drive on my pc) :
QString path;
//QFileSystemModel *dirModel;
//QFileSystemModel *fileModel;
//TreeView
dirModel = new QFileSystemModel;
dirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirModel->setRootPath(path);
ui->treeView->setModel(dirModel);
//ListView
fileModel = new QFileSystemModel;
fileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
fileModel->setRootPath(path);
ui->listView->setModel(fileModel);
If I set the path to fileName (QString path = fileName;), nothing changes. I'm assuming QDir::AllDirs is the reason for this.
I looked for other QDir filters (like QDir::Dirs or setFilter) but nothing seems to work.
I even tried QDesktopServices::openUrl, but it just opens the file path in a new window instead of the treeView.
Related
I am trying to list image thumbnails on listwidget . Now I can display a thumbnail . I want to display multiple image thumbnails from a directory.
Here is the code I tried so far.
ui->listWidget->setViewMode(QListWidget::IconMode);
ui->listWidget->setIconSize(QSize(320,240));
ui->listWidget->setResizeMode(QListWidget::Adjust);
ui->listWidget->addItem(new QListWidgetItem(QIcon("image path"),"name"));
You must use QDir, set appropriate filters like *.png, *.jpg and in the end use entryInfoList() returns a QFileInfo list that has the information of the fileName and absoluteFilePath.
ui->listWidget->setViewMode(QListWidget::IconMode);
ui->listWidget->setIconSize(QSize(320,240));
ui->listWidget->setResizeMode(QListWidget::Adjust);
QDir directory("/path/of/directory");
directory.setNameFilters({"*.png", "*.jpg"});
for(const QFileInfo & finfo: directory.entryInfoList()){
QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
ui->listWidget->addItem(item);
}
Here's the code I am using :
void Dialog::on_pushButton_clicked()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Find Files"), QDir::currentPath());
ui->lineEdit->setText(directory);
}
With this, I can display a list of files of a path given directly. How can I make this code display the list of files for a directory/path given by the user?
Declare a QDir like this : QDir dir(directory) and then use QDir::entryList() (which returns a list of the names of all the files and directories in the directory).
I have develop an app using a QTreeWidget and based on QTreeWidgetItem. The app is a file browser-like. Each time I had to display a folder, I create it like this :
item->setText(0, foldername);
item->setText(1,"--");
item->setText(2,"--");
item->setIcon(0,QIcon(":/Images/folder_pic.png"));
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled | Qt::ItemIsEditable);
item->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
and I got this :
and when expand this
This working fine but when I delete the folder "NewFolder", I lose the indicator of the parent folder even if the indicator for this parent has been set to always show the indicator
The delete code is just
delete Item;
where Item is the selected item, in my case the child of the folder apps.
I have try to remove the delete and replace it by something like
ItParent = myItem->parent();
myItem->parent()->removeChild(myItem);
if(ItParent != NULL)
ItParent->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
it's not working as well
Any idea ?
I'm new to qt, and I have a button, if I click on it I want to get a dialog box to choose a path where I want to save a file. My question is, how could I create this kind of dialog box, which returns with a string of the path? I use linux, if it matters with qt:)
ps.: I use only gedit, so I'd like to solve it that way. :)
Use QFileDialog which has several useful static member functions including
QString myDir = QFileDialog::getExistingDirectory();
which returns a directory that you select. I think that is what you want, see the docmentation here
http://qt-project.org/doc/qt-5.0/qtwidgets/qfiledialog.html
In addition to the answer by #Muckle_ewe, there is a the static function QFileDialog::getSaveFileName, which will present the standard open / save file dialog and allow the user to both select the path and input a name for the file.
It's definition is this: -
QString QFileDialog::getSaveFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)
An example of its usage is: -
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
"/home/untitled.png",
tr("Images (*.png *.xpm *.jpg)"));
As the docs state,
This is a convenience static function that will return a file name
selected by the user. The file does not have to exist.
I have a program in which I have a button to get File Dialog like
How can I select a file, get the file name and location, and save that to a string displayed in the ui.The signalclicked(), emitted from the button, is connected to the slot fileSELECT().
........
void MainThread::fileSELECT(){
QString fileName = QFileDialog::getOpenFileName(this,tr("Select video"),"d:\\BMDvideos",tr("Video files (*.avi)"));
}
so when I select an .avi file, how do I get its location in fileName displayed like
d:\BMDvideo\videFile.avi
so I thinks that I got it now. my first code was completly wrong.
void MainThread::fileSelect(){
QString fileName = QFileDialog::getOpenFileName(this,tr("Select video"),"d:\\BMDvideos",tr("Video files (*.avi)"));
QLabel *testLabel = new QLabel(fileName);
BOX->addWidget(testLabel);
}
I can see now the path of the selected file
To get the folder path, you can use QFileDialog::getExistingDirectory, and to get the file-name use QFileDialog::getOpenFileName