PDA

View Full Version : [solved]Get filepath and filename from qtreewidget



kamlmish
20th December 2010, 05:27
Hi
I have a strange issue where I need to get filepath and filename on a single click of the item in the qtreewidget



class browserwindow: public QTreeWidget
{
Q_OBJECT
public:
browserwindow(QWidget* parent = 0);
~browserwindow();
//functions
void populate();

public slots:

QModelIndex getDetails(const QModelIndex& index);

public:
QDirModel* model;
QTreeView* tree;
QModelIndex index;

};




#include "browsemodel.h"

browserwindow::browserwindow(QWidget* parent)
:QTreeWidget(parent)
{
model = new QDirModel(this);
tree = new QTreeView(this);
populate();

}

browserwindow::~browserwindow()
{
delete tree;
delete model;
}

void browserwindow::populate()
{
tree->setModel(model);
tree->setRootIndex(model->index(QDir::homePath()));
tree->setWindowTitle("Browse for folder");
tree->resize(820, 7200);
connect(this,SIGNAL(clicked(QModelIndex)),SLOT(get Details(QModelIndex)),Qt::DirectConnection);

}

QModelIndex browserwindow::getDetails(const QModelIndex &index) // to get details on the index on single click
{
QString filename = model->fileName(index);
return model->index(0);

}

Issue is that I get a SIGSERV segmentation fault when I run the above code
my main objective is that , when a user clicks on the "browse" button(add_link_folder.png), the browser should open(browser.png)
when the user single clicks on any file in the browser, it should go back to the (add_link_folder.png) and the location and name shud display the filepath and filename respectivly

Lykurg
20th December 2010, 08:22
You don't need to delete tree and model. They get deleted by Qt. Further what is the purpose of subclassing QTreeWidget and create a QTreeView inside? Also do you have seen QFileDialog?

And where exactly crashes the program?

kamlmish
20th December 2010, 08:57
1) I needed to display the filesystem , and therefore I used a QDirModel and QTreeView to display the filesystem
The filesystem gets displayed in a new window(browser.png)
2) QFileDialog would provide me a dialog box where I can select specific files and directories , but my objective is to display the filesystem in a tree view , select a specific file from the treeview , the selected file name and path would be displayed in a text browser

The crash happens at
connect(this,SIGNAL(clicked(QModelIndex)),SLOT(get Details(QModelIndex)),Qt::DirectConnection);

Lykurg
20th December 2010, 12:18
well within the posted code I don't see an error. Can you post the backtrace since it isn't (most likely) the connection statement.