您好,登錄后才能下訂單哦!
學習了上一條博客的關于QListWidget的成員函數的用法,下面就有一個相關的簡單小程序來練下手:
程序完成如下圖:
第一步,我們先通過ui設計師拖拽出我們想要的布局:
下面是”c.cpp”下的代碼:我將逐句講解:
#include "c.h"c::c(QWidget *parent) : QMainWindow(parent) { //應用ui設計師布局. ui.setupUi(this); //把兩個按鈕設置為選中. ui.listButton->setChecked(true); ui.upButton->setChecked(true); //列表部件采用排序.默認采用AAA-ZZZ排序. ui.listWidget->setSortingEnabled(true); //往列表部件中加入項組.一共3個. ui.listWidget->addItems(QStringList()<<QString::fromLocal8Bit("北京")<<QString::fromLocal8Bit("上海")<<QString::fromLocal8Bit("杭州")); //設置標題為"QListWidget". this->setWindowTitle(QString::fromLocal8Bit("QListWidget")); //對于前面的三個項設置圖標.可根據實際情況使用. ui.listWidget->item(0)->setIcon(QIcon("Icons/1.jpg")); ui.listWidget->item(1)->setIcon(QIcon("Icons/2.jpg")); ui.listWidget->item(2)->setIcon(QIcon("Icons/3.png")); //設置項的選擇情況. ui.listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //連接信號與槽. connect(ui.addButton, SIGNAL(clicked()), this, SLOT(addItemSlot())); connect(ui.delButton, SIGNAL(clicked()), this, SLOT(delItemSlot())); connect(ui.delAllButton, SIGNAL(clicked()), this, SLOT(delAllItemSlot())); //QLineEdit的信號:void returnPressed()的意思是:當焦點在lineEdit內時,敲擊enter鍵,發出信號. connect(ui.lineEdit, SIGNAL(returnPressed()), this, SLOT(addItemSlot())); connect(ui.listButton, SIGNAL(clicked()), this, SLOT(changeListWidgetViewModeSlot())); connect(ui.iconButton, SIGNAL(clicked()), this, SLOT(changeListWidgetViewModeSlot())); connect(ui.upButton, SIGNAL(clicked()), this, SLOT(changeSortModeSlot())); connect(ui.downButton, SIGNAL(clicked()), this, SLOT(changeSortModeSlot())); } c::~c() { }void c::addItemSlot() { //創建一個QListWidget的項. QListWidgetItem *ite = new QListWidgetItem; //從lineEdit行編輯框中獲取輸入的文字. QString iteText = ui.lineEdit->text(); //一定要檢查是否空,否則也會輸入進去!!! if (iteText.isEmpty()) return; //然后設置項的文本. ite->setText(iteText); //最后把這個項加入到列表部件中去. ui.listWidget->addItem(ite); //清空行編輯器,坐下善后工作. ui.lineEdit->clear(); }void c::delItemSlot() { //直接使用QListWidget部件的刪除函數,通過currentItem()成員函數來獲取當前的選擇項. ui.listWidget->removeItemWidget(ui.listWidget->currentItem()); }void c::delAllItemSlot() { /*//通過循環一個個刪除QListWidget內的項. int count = ui.listWidget->count(); for (int i = count - 1; i >= 0; --i) { QListWidgetItem* ite = ui.listWidget->takeItem(i); if (ite != 0) delete ite; }*/ //也可以直接: ui.listWidget->clear(); }void c::changeListWidgetViewModeSlot() { //判斷對于列表顯示模式的選擇情況.如果選擇了列表模式,則設置列表模式. if (ui.listButton->isChecked()) { ui.listWidget->setViewMode(QListView::ListMode); } //否則選擇圖標模式. else { ui.listWidget->setViewMode(QListView::IconMode); } }void c::changeSortModeSlot() { //判斷循環模式的選擇情況. if (ui.upButton->isChecked()) { ui.listWidget->sortItems(Qt::AscendingOrder); } else { ui.listWidget->sortItems(Qt::DescendingOrder); } }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
最后貼下”c.h”下的代碼:
#ifndef C_H#define C_H#include <QtWidgets/QMainWindow>#include "ui_c.h"#include <QListWidget>#include <QListWidgetItem>#include <QMessageBox>class c : public QMainWindow{ Q_OBJECTpublic: c(QWidget *parent = 0); ~c();private slots: void addItemSlot(); void delItemSlot(); void delAllItemSlot(); void changeListWidgetViewModeSlot(); void changeSortModeSlot();private: Ui::cClass ui; };#endif // C_H1234567891011121314151617181920212223242526272829
main.cpp下的代碼:
#include "c.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]) { QApplication a(argc, argv); c w; w.show(); return a.exec(); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。