PDA

View Full Version : Qdirmodel issue



kamlmish
16th December 2010, 12:04
Hi
I am facing a strange issue in the QDIRMODEL
I have written the below code on the SLOT function for the button SIGNAL( clicked), but when I run the code , the tree doesnt show and when I debug the code
QDirModel model
goes into qgrayraster



QDirModel model;
QTreeView tree;
tree.setModel(model);
tree.setRootIndex(model->index(QDir::homePath()));
tree.setColumnHidden( 1, true );
tree.setColumnHidden( 2, true );
tree.setColumnHidden( 3, true );
tree.setWindowTitle(QObject::tr("Dir View:")+QDir::homePath());
tree.resize(640, 480);
tree.show();


May I know where am I going wrong

Lykurg
16th December 2010, 12:06
Probably basic C++ issue: Have a look at the difference of creating objects on the stack or heap. Also look at the lifetime of these objects!

kamlmish
16th December 2010, 12:14
but even when I do
QDirModel* model = new QDirModel;

I still get the same issue

Lykurg
16th December 2010, 12:23
Then you must show us the full code of your slot.

kamlmish
16th December 2010, 12:28
Class Declaration


class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public:
void createMenu();

public slots:
void FileBrowseWindow();
void ClearButton();

private:
QLabel* namelabel;
QTextEdit* nametext;
QLabel* loclabel;
QTextBrowser* locText;
QPushButton* browse_button;
QPushButton* ok_button;
QPushButton* cancel_button;

};



constructor


MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent)
{
namelabel = new QLabel("Name",this);
nametext = new QTextEdit(this);
loclabel = new QLabel("Location",this);
locText = new QTextBrowser(this);
browse_button = new QPushButton("Browse",this);
ok_button = new QPushButton("Ok",this);
cancel_button = new QPushButton("Cancel",this);
createMenu();
connect(browse_button,SIGNAL(clicked()),SLOT(FileB rowseWindow()),Qt::DirectConnection);
//connect(cancel_button,SIGNAL(clicked()),this,SLOT( ClearButton()),Qt::AutoConnection);
}


SLOT function


void MainWindow::FileBrowseWindow()
{
QDirModel* model = new QDirModel(this);
QTreeView tree;
tree.setModel(model);
tree.setRootIndex(model->index(QDir::homePath()));
tree.setColumnHidden( 1, true );
tree.setColumnHidden( 2, true );
tree.setColumnHidden( 3, true );
tree.setWindowTitle(QObject::tr("Dir View:")+QDir::homePath());
tree.resize(640, 480);
tree.show();

}

Lykurg
16th December 2010, 12:29
Well, it isn't enough to create the model on the heap. You should probably also create the tree on the heap...

kamlmish
17th December 2010, 10:21
thanks
solved the problem