1. 程式人生 > >QListWidget、QTreeWidget和QTableWidget用法詳細說明

QListWidget、QTreeWidget和QTableWidget用法詳細說明

我們瞭解了 model/view 架構的基本概念。現在我們從最簡單的QListWidgetQTreeWidgetQTableWidget三個類開始瞭解最簡單的 model/view 的使用。這部分內容的確很難組織。首先,從最標準的 model/view 開始,往往會糾結於複雜的程式碼;但是,如果從簡單的 QListWidgetQTreeWidgetQTableWidget開始,由於這三個類都是繼承自各自的 view 類,很難避免 model/view 的相關內容。於是,我們這部分的組織是,首先進行簡單的資料顯示,更復雜的設定則放在後面的章節。

QListWidget

我們要介紹的第一個是QListWidget

。先來看下面的程式碼示例:

 /**************************
     *
     * 下面是listwidget的顯示方式
     *
    ***********************/



    label = new QLabel(nullptr);
    label->setFixedWidth(70);

    listWidget = new QListWidget(nullptr);
    listWidget->setViewMode(QListView::ListMode);//設定list的顯示方式

    //三項中向列表中新增列表項的方式
    new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/8.jpg"), tr("Chrome"), listWidget);
    new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/9.jpg"), tr("Firefox"), listWidget);

    listWidget->addItem(new QListWidgetItem(QIcon( QApplication::applicationDirPath()+"/1.jpg"), tr("IE")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/2.jpg"), tr("Netscape")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/3.jpg"), tr("Opera")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/4.jpg"), tr("Safari")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/5.jpg"), tr("TheWorld")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/6.jpg"), tr("Traveler")));

    QListWidgetItem *newItem = new QListWidgetItem;
    newItem->setIcon(QIcon(QApplication::applicationDirPath()+"/7.jpg"));
    newItem->setText(tr("Maxthon"));
    listWidget->insertItem(3, newItem); //調整位置大小

    int * a = new int(1);
    qDebug()<<"記憶體:"<<*a;

    QListWidgetItem *newItem1 = new QListWidgetItem(*newItem);//引用是傳值
    newItem1->setText("字都是法國");

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(label);
    layout->addWidget(listWidget);

    setLayout(layout);

    connect(listWidget, SIGNAL(currentTextChanged(QString)),
            label, SLOT(setText(QString)));

    qDebug()<<"exe dir:"<< QApplication::applicationDirPath();

QListWidget是簡單的列表元件。當我們不需要複雜的列表時,可以選擇QListWidgetQListWidget中可以新增QListWidgetItem型別作為列表項,QListWidgetItem即可以有文字,也可以有圖示。上面的程式碼顯示了三種向列表中新增列表項的方法(實際是兩種,後兩種其實是一樣的),我們的列表元件是listWidget,那麼,向listWidget新增列表項可以:第一,使用下面的語句

new QListWidgetItem(QIcon(":/Chrome.png"), tr("Chrome"), listWidget);

第二種

listWidget->addItem(new QListWidgetItem(QIcon(":/IE.png"), tr("IE")));
// 或者
QListWidgetItem *newItem = new QListWidgetItem;
newItem->setIcon(QIcon(":/Maxthon.png"));
newItem->setText(tr("Maxthon"));
listWidget->insertItem(3, newItem);

注意這兩種新增方式的區別:第一種需要在構造時設定所要新增到的QListWidget物件;第二種方法不需要這樣設定,而是要呼叫addItem()或者insertItem()自行新增。如果你仔細查閱QListWidgetItem的建構函式,會發現有一個預設的type引數。該引數有兩個合法值:QListWidgetItem::Type(預設)和QListWidgetItem::UserType。如果我們繼承QListWidgetItem,可以設定該引數,作為我們子類的一種區別,以便能夠在QListWidget區別處理不同子類。

我們的程式的執行結果如下:

圖片的位置可以放在任意資料夾下,可以路徑寫的正確,本文中的圖片是放在程式執行的debug的資料夾下的

QTreeWidget

我們要介紹的第二個元件是QTreeWidget。顧名思義,這是用來展示樹型結構(也就是層次結構)的。同前面說的QListWidget類似,這個類需要同另外一個輔助類QTreeWidgetItem一起使用。不過,既然是提供方面的封裝類,即便是看上去很複雜的樹,在使用這個類的時候也是顯得比較簡單的。當不需要使用複雜的QTreeView特性的時候,我們可以直接使用QTreeWidget代替。

下面我們使用程式碼構造一棵樹:

 QTreeWidget *treeWidget = new QTreeWidget(nullptr);
   // treeWidget->setColumnCount(3);//設定是那棵樹下
    QStringList listLabels;
    listLabels<<"資料夾"<<"顯示";
    treeWidget->setHeaderLabels(listLabels);
    treeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);//預設是適當顯示滾動條,現狀總是顯示
    treeWidget->setColumnWidth(0,150);
    treeWidget->setColumnWidth(1,150);
    //設定滾動輪
    treeWidget->setAutoScroll(true);
    treeWidget->hasAutoScroll();
    QScrollBar *scrollBar = new QScrollBar(Qt::Horizontal,nullptr);
    scrollBar->setMouseTracking(true);
    scrollBar->setTracking(true);
    scrollBar->triggerAction(QAbstractSlider::SliderPageStepSub);
    treeWidget->setHorizontalScrollBar(scrollBar);
    treeWidget->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    treeWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
   // treeWidget->setColumnHidden(1,true);

   // treeWidget->setAllColumnsShowFocus(true);顯示鍵盤焦點
    QTreeWidgetItem *root = new QTreeWidgetItem(treeWidget,QStringList()<<QString("Root1")<<"目錄1");
    root->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *root1 = new QTreeWidgetItem(treeWidget,QStringList()<<QString("Root2")<<"目錄2");
    root1->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *root2 = new QTreeWidgetItem(treeWidget,QStringList()<<QString("Root3")<<"目錄3");
    root2->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));

    //new QTreeWidgetItem(root, QStringList(QString("Leaf 1")));
    QTreeWidgetItem *leaf1 = new QTreeWidgetItem(root,QStringList()<<QString("Leaf 1")<<"葉子1.1");
    leaf1->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));

    QTreeWidgetItem *leaf2 = new QTreeWidgetItem(root,QStringList()<<QString("Leaf 2")<<"葉子1.2");
    leaf2->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));

    QTreeWidgetItem *leaf3 = new QTreeWidgetItem(root,QStringList()<<QString("Leaf 3")<<"葉子1.3");
    leaf3->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));


    QTreeWidgetItem *leaf4 = new QTreeWidgetItem(root1,QStringList()<<QString("Leaf 1")<<"葉子2.1");
    leaf4->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *leaf5 = new QTreeWidgetItem(root1,QStringList()<<QString("Leaf 2")<<"葉子2.2");
    leaf5->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *leaf6 = new QTreeWidgetItem(root1,QStringList()<<QString("Leaf 3")<<"葉子2.3");
    leaf6->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));


    QTreeWidgetItem *leaf7 = new QTreeWidgetItem(root2,QStringList()<<QString("Leaf 1")<<"葉子3.1");
    leaf7->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *leaf8 = new QTreeWidgetItem(root2,QStringList()<<QString("Leaf 2")<<"葉子3.1");
    leaf8->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *leaf9 = new QTreeWidgetItem(root2,QStringList()<<QString("Leaf 3")<<"葉子3.3");
    leaf9->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));





    leaf1->setCheckState(0, Qt::Checked);
    leaf2->setCheckState(0, Qt::Checked);
    leaf3->setCheckState(0,Qt::Checked);
    leaf4->setCheckState(0, Qt::Checked);
    leaf5->setCheckState(0, Qt::Checked);
    leaf6->setCheckState(0,Qt::Checked);
    leaf7->setCheckState(0, Qt::Checked);
    leaf8->setCheckState(0, Qt::Checked);
    leaf9->setCheckState(0,Qt::Checked);

    QList<QTreeWidgetItem *> rootList,rootList1,rootList2;
    rootList << root;
    rootList1<< root1;
    rootList2<< root2;
    treeWidget->insertTopLevelItems(0, rootList);
    treeWidget->insertTopLevelItems(1, rootList1);
    treeWidget->insertTopLevelItems(2, rootList2);
   // treeWidget->setHeaderItem(root);
    int nCount = treeWidget->columnCount();

    qDebug()<<"nCount "<<nCount;
    treeWidget->setWindowTitle("檢視 widget");
    treeWidget->show();
   // treeWidget->clear();

    int h = treeWidget->height();
    int w = treeWidget->width();
    qDebug()<<"h w"<<h<<w;

 首先,我們建立了一個QTreeWidget例項。然後我們呼叫setColumnCount()函式設定欄數。這個函式的效果我們會在下文了解到。最後,我們向QTreeWidget新增QTreeWidgetItemQTreeWidgetItem有很多過載的建構函式。我們在這裡看看其中的一個,其餘的請自行查閱文件。這個建構函式的簽名如下:

QTreeWidgetItem(QTreeWidget *parent, const QStringList &strings, int type = Type);

在這段程式碼中,我們建立了作為根的QTreeWidgetItemroot。然後添加了第一個葉節點,之後又新增一個,而這個則設定了可選標記。最後,我們將這個 root 新增到一個QTreeWidgetItem的列表,作為QTreeWidget的資料項。此時你應該想到,既然QTreeWidget接受QList作為項的資料,它就能夠支援多棵樹的一起顯示,而不僅僅是單根樹

這次我們沒有使用setColumnCount(),而是直接使用QStringList設定了 headers,也就是樹的表頭。接下來我們使用的還是QStringList設定資料。這樣,我們實現的是帶有層次結構的樹狀表格。利用這一屬性,我們可以比較簡單地實現類似 Windows 資源管理器的介面。唯一的缺點就是無法在第二列在建立相關的樹結構,並沒有提供相關的api可以操作,並且從現有的api來看,只能實現單列,無法實現多列insertTopLevelItems執行結果如下

 

 

QTableWidget

我們要介紹的最後一個是 QTableWidgetQTableWidget並不比前面的兩個複雜到哪裡去,這點我們可以從程式碼看出來:

首先我們建立了QTableWidget物件,然後設定列數和行數。接下來使用一個QStringList,設定每一列的標題。我們可以通過呼叫setItem()函式來設定表格的單元格的資料。這個函式前兩個引數分別是行索引和列索引,這兩個值都是從 0 開始的,第三個引數則是一個QTableWidgetItem物件。Qt 會將這個物件放在第 row 行第 col 列的單元格中。有關QTableWidgetItem的介紹完全可以參見上面的QListWidgetItemQTreeWidgetItem

具體程式碼的應用點此資原始檔在此