PDA

View Full Version : QDir



smemamian
1st April 2013, 20:44
hi
when i clicked "Create Dir" button...
Why is this program not working?

8878


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDirModel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

private:
Ui::MainWindow *ui;
QDirModel *model ;

};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QInputDialog>
#include <QString>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QDirModel(this) ;
model->setReadOnly(false);
model->setSorting(QDir::DirsFirst |QDir::Name | QDir::IgnoreCase);
ui->treeView->setModel(model);
}

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

void MainWindow::on_pushButton_clicked()
{
//Create
QModelIndex index = ui->treeView->currentIndex();
if(!index.isValid())
return;
QString name = QInputDialog::getText(this,"Name","Enter a name :");
if(!name.isEmpty())
return ;
model->mkdir(index,name);
}

void MainWindow::on_pushButton_2_clicked()
{
//Delete
}

ChrisW67
1st April 2013, 22:48
Why is this program not working?
I am not sure how you expect us to know: you have not told us what you expect it to do, or in what way it is "not working."

Attempting to create a directory with no name is unlikely to be useful. Providing a name is going to do nothing.

smemamian
2nd April 2013, 06:49
i when clicked "Create Dir" button, then i write a name, folder not created :(

BalaQT
2nd April 2013, 07:55
if(!name.isEmpty())
return ;
model->mkdir(index,name);



if(!name.isEmpty()) this should be like this
if(name.isEmpty()).


When the name is not empty, you need to create the folder.

you need to remove the ! before name. isEmpty will return true when the string is empty.


hope it helps,
bala