PDA

View Full Version : open (and delete) a file with a button on qtreeview



maxcrive
30th August 2011, 01:15
Hi I have this problem:
I need to open (and delete) a file (it's displayed on a QTreeView) with a button.
At the moment I can only do this operation with a double click, but is not enough.

This is my header:


#ifndef EDITOR_H
#define EDITOR_H

#include <QDialog>
#include <QtCore>
#include <QtGui>
#include <QFileSystemModel>
#include <QCoreApplication>
#include <QFile>

class QString;
class QModelIndex;

namespace Ui {
class editor;
}

class editor : public QDialog
{
Q_OBJECT

public:
explicit editor(QWidget *parent = 0);
~editor();

private slots:
void on_vistaLista_doubleClicked(const QModelIndex &index);
void getDetails(const QModelIndex &index);

void on_cancellaFileEditor_clicked();

private:
Ui::editor *ui;
QFileSystemModel *model;

};

#endif // EDITOR_H


and the program....



#include "editor.h"
#include "ui_editor.h"

editor::editor(QWidget *parent) :
QDialog(parent),
ui(new Ui::editor)
{
ui->setupUi(this);

QStringList lista;
QString directory = qApp->applicationDirPath() + "/download";


model = new QFileSystemModel(this);
model->setFilter(QDir::Files);


ui->vistaLista->setModel(model);
ui->vistaLista->setRootIndex(model->setRootPath(directory));
connect(ui->vistaLista, SIGNAL(clicked(QModelIndex)), this, SLOT(getDetails(QModelIndex)));

}

editor::~editor()
{
delete ui;
}

void editor::on_vistaLista_doubleClicked(const QModelIndex &index)
{
QString url = model->fileInfo(index).filePath();
QDesktopServices::openUrl(QUrl::fromLocalFile(url) );
}

void editor::getDetails(const QModelIndex &index)
{
QString filename=model->fileInfo(index).filePath();
ui->labelTemporaneaEditor->setText(filename);
}



void editor::on_cancellaFileEditor_clicked()
{

//????????????????????????????????? ARGHHHHHH ??????????????????

}

Please help me!!!! Thanks!

ChrisW67
30th August 2011, 02:23
on_cancellaFileEditor_clicked() is, I guess, where you want to delete a file... and also the only part of your program you don't show us. What are you doing in that routine? What are you expecting to happen? What is actually happenning?

maxcrive
30th August 2011, 08:17
Sorry, but yesterday it was late at night (and my english is rusty!).

Yes, on_cancellaFileEditor_clicked() is the slot where i want to delete the file with QFile::remove(const QString &fileName), and I want another slot like on_openFileEditor_clicked() where open file with a default program using QDesktopServices:penUrl(QUrl::fromLocalFile(url)).
I've try get the path of the selected file from the model with getDetails(const QModelIndex &index), it works but only, obviusly, when I click over the file in the TreeView, not if i use the arrow key...and I need that function....
...I hope that I made ​​it clear...

high_flyer
30th August 2011, 14:40
But on what level is your problem?
Is your slot getting called?
Or is the problem getting the file location?
Please explain what the PROBLEM is.

maxcrive
30th August 2011, 16:23
Ok.

The problems are:

1) I've get the file location connecting the signal clicked(QModelIndex) issued by the QtreeView to getDetails, but is not enough. I need to get this information each time the file is selected in the list, regardless of what the select (mouse click or arrow keys). There is a different signal to send?

2) Newly found information (file location), how can I share it with the solts of the buttons?

Thanks.

high_flyer
30th August 2011, 16:47
1. just use itemPressed().
2. use a member?

wysota
31st August 2011, 00:28
I think something like currentIndexChanged() will be better. It should be available via QItemSelectionModel of the view.

maxcrive
31st August 2011, 11:50
Great! currentChanged() works fine thanks.
ItemPressed() works like click(), thanks anyway.

Now remains the second problem. I've tried in many ways, but I can't pass the string filename from getDetails to the other slots (open files and delete files).
Probably I think in the wrong way....any idea(s)?

wysota
31st August 2011, 12:52
Try explaining better what you want and what the problem is. It seems the problem is related to application design and not to using Qt.

maxcrive
31st August 2011, 13:33
Try explaining better what you want and what the problem is. It seems the problem is related to application design and not to using Qt.

Yes, the problem was in the architecture...now works perfectly.

The solution was in currentIndex() ... nearest currentChanged() :) .
This is the solution:


#include "editor.h"
#include "ui_editor.h"

editor::editor(QWidget *parent) :
QDialog(parent),
ui(new Ui::editor)
{
ui->setupUi(this);

QStringList lista;
QString directory = qApp->applicationDirPath() + "/download";

model = new QFileSystemModel(this);
model->setFilter(QDir::Files);

itemModel = new QItemSelectionModel(model);

ui->vistaLista->setModel(model);
ui->vistaLista->setSelectionModel(itemModel);
ui->vistaLista->setRootIndex(model->setRootPath(directory));

connect(ui->bottoneCancellaFileEditor, SIGNAL(clicked()), this, SLOT(on_cancellaFileEditor_clicked()));
connect(ui->bottoneApriEditor, SIGNAL(clicked()), this, SLOT(on_bottoneApriEditor_clicked()));
}

editor::~editor()
{
delete ui;
}

void editor::on_vistaLista_doubleClicked(const QModelIndex &index)
{
QString url = model->fileInfo(index).filePath();
QDesktopServices::openUrl(QUrl::fromLocalFile(url) );
}

void editor::on_cancellaFileEditor_clicked()
{
QString nomeFile=model->fileInfo(itemModel->currentIndex()).filePath();
QFile::remove(nomeFile);
}

void editor::on_bottoneApriEditor_clicked()
{
QString nomeFile=model->fileInfo(itemModel->currentIndex()).filePath();
QDesktopServices::openUrl(QUrl::fromLocalFile(nome File));
}

Thanks to all, are a valuable resource for those who are beginning (in QT and c++) like me.