close

 

 

最近一樣很忙,忙到亂七八糟,應該是說,

最近來的需求都是要那種幾天內或非常即時性的任務,

所以就很沒時間來寫寫Blog。

 

最近有收到信,希望能在教學裡面,能夠更詳細點,

好吧.... 我只能說我盡量,寫在註釋裡面,

但畢竟我不是專業寫手,所以,如果有錯誤的地方,在麻煩各位指出囉。

 

 

 

 

 

 

 

Step1. 首先,我們先將UI拉好

然後將Make Dir & Delete 都給予slot

Button→Right Click→Go to slot→Clicked()

// 這邊應該非常簡單,我就快速帶過了

 

 

 

 

 

Step2. DirMod.pro 

Added " QT += widgets "

 

 

 

Step3. dialog.h

1. Add include 

2. you will see the slots : 

void on_pushbutton_clicked();  and void on_pushButton_2_clicked();

3. Added "QDirModel*model";

 

 

 

 

 

 

 

 

Step4. dialog.cpp

You will see, very easy to show your computer information.

 

 


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

    // Step1.
    // First thing we actually need to do is create and populate our model and to do that.
    model = new QDirModel(this);
    model->setReadOnly(false);


    // Step2.
    // that's do the sorting first!
    // 1. Directories to be listed first
    // 2. ignore the case and sort by name
    model->setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name);


    // Step3.
    // Set model to tree view !
    // ( This is my habit, i like write tree view, not treeView )
    ui->treeView->setModel(model);


    // Step4.
    // Set some style
    // 1. set index.
    QModelIndex index = model->index("E:/");
    // 2. we're getting the index of the E Drive and we're going to actually find at the tree view
    // and expand it.
    ui->treeView->expand(index);
    // 3. and we want to scroll the view down to it that way.
    ui->treeView->scrollTo(index);
    // 4. and we want to actually select it.
    ui->treeView->setCurrentIndex(index);
    // 5. and last we want to resize the column to the contents and
    // we just wnat the name or the first column.
    ui->treeView->resizeColumnToContents(0);
}

 

 

 

 

 

and then we added some function to button.

void Dialog::on_pushButton_clicked()
{
    // Make dir
    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 Dialog::on_pushButton_2_clicked()
{
    // Delete
    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()){
        return;
    }
 
    if (model->fileInfo(index).isDir())
    {
        // Dir
        model->rmdir(index);
    }
    else
    {
        // File
        model->remove(index);
    }
}

 

 

That's all. 

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

結語 :

其實這篇我寫的比其他篇都來的久,

往後的教學文章我的風格也會盡量的將解釋寫在註釋裡面,

至於這玩意的心得感想呢.............. 

我還是覺得,Qt實在很強大,哈哈哈哈.... 對於寫MFC很久的人來說,

簡直就是跪者寫完的,

好了,大概就是這樣,五月了,有一堆事情要幹,

這個月份可能文章的產量會降低許多,但我還是會維持一定的品質的 !!!

請各位看官鞭我的時候小力點,

謝謝。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

arrow
arrow
    文章標籤
    qt Qt QDirModel QTreeView
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()