翻譯|使用教程|編輯:龔雪|2023-05-05 10:54:07.230|閱讀 159 次
概述:本文將為大家介紹Qt框架中的自定義排序/篩選模型示例,歡迎下載相關(guān)組件體驗(yàn)~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Qt 是目前最先進(jìn)、最完整的跨平臺(tái)C++開發(fā)工具。它不僅完全實(shí)現(xiàn)了一次編寫,所有平臺(tái)無差別運(yùn)行,更提供了幾乎所有開發(fā)過程中需要用到的工具。如今,Qt已被運(yùn)用于超過70個(gè)行業(yè)、數(shù)千家企業(yè),支持?jǐn)?shù)百萬設(shè)備及應(yīng)用。
自定義排序/篩選模型示例說明了如何子類化來執(zhí)行高級(jí)排序和篩選。
在上文中(點(diǎn)擊這里回顧>>),為大家講解了如何實(shí)現(xiàn)MySortFilterProxyModel類、MySortFilterProxyModel的定義,本文將繼續(xù)講解Window類的定義和實(shí)現(xiàn)!
Qt技術(shù)交流群:166830288 歡迎一起進(jìn)群討論
CustomFilter類繼承了QWidget,并提供了這個(gè)例子的主應(yīng)用程序窗口:
class Window : public QWidget { Q_OBJECT public: Window(); void setSourceModel(QAbstractItemModel *model); private slots: void textFilterChanged(); void dateFilterChanged(); private: MySortFilterProxyModel *proxyModel; QGroupBox *sourceGroupBox; QGroupBox *proxyGroupBox; QTreeView *sourceView; QTreeView *proxyView; QLabel *filterPatternLabel; QLabel *fromLabel; QLabel *toLabel; FilterWidget *filterWidget; QDateEdit *fromDateEdit; QDateEdit *toDateEdit; };
我們實(shí)現(xiàn)了兩個(gè)私有槽,textFilterChanged()和dateFilterChanged(),用于響應(yīng)用戶更改過濾器模式、區(qū)分大小寫或任何日期,此外還實(shí)現(xiàn)了一個(gè)公共的setSourceModel()方便函數(shù)來設(shè)置模型/視圖關(guān)系。
在本示例中,我們選擇在main()函數(shù)中創(chuàng)建和設(shè)置源模型,因此在構(gòu)建主應(yīng)用程序窗口時(shí),假設(shè)源模型已經(jīng)存在,并從創(chuàng)建自定義代理模型的實(shí)例開始:
Window::Window() { proxyModel = new MySortFilterProxyModel(this);
我們?cè)O(shè)置屬性,該屬性保存代理模型是否被動(dòng)態(tài)排序和過濾。通過將該屬性設(shè)置為true,可以確保在源模型的內(nèi)容發(fā)生變化時(shí)對(duì)模型進(jìn)行排序和過濾。
主應(yīng)用程序窗口顯示源模型和代理模型的視圖,源視圖非常簡(jiǎn)單:
sourceView = new QTreeView; sourceView->setRootIsDecorated(false); sourceView->setAlternatingRowColors(true);
QTreeView類提供了樹視圖的默認(rèn)模型/視圖實(shí)現(xiàn),視圖實(shí)現(xiàn)了應(yīng)用程序源模型中項(xiàng)目的樹表示。
sourceLayout->addWidget(sourceView); sourceGroupBox = new QGroupBox(tr("Original Model")); sourceGroupBox->setLayout(sourceLayout);
類提供了樹視圖的默認(rèn)模型/視圖實(shí)現(xiàn),視圖實(shí)現(xiàn)了應(yīng)用程序源模型中項(xiàng)目的樹表示,將視圖小部件添加到我們安裝在相應(yīng)組框上的布局中。
另一方面代理模型視圖包含幾個(gè)控件,這些控件控制轉(zhuǎn)換源模型數(shù)據(jù)結(jié)構(gòu)的各個(gè)方面:
filterWidget = new FilterWidget; filterWidget->setText(tr("Grace|Sports")); connect(filterWidget, &FilterWidget::filterChanged, this, &Window::textFilterChanged); filterPatternLabel = new QLabel(tr("&Filter pattern:")); filterPatternLabel->setBuddy(filterWidget); fromDateEdit = new QDateEdit; fromDateEdit->setDate(QDate(1970, 01, 01)); fromLabel = new QLabel(tr("F&rom:")); fromLabel->setBuddy(fromDateEdit); toDateEdit = new QDateEdit; toDateEdit->setDate(QDate(2099, 12, 31)); toLabel = new QLabel(tr("&To:")); toLabel->setBuddy(toDateEdit); connect(filterWidget, &QLineEdit::textChanged, this, &Window::textFilterChanged); connect(fromDateEdit, &QDateTimeEdit::dateChanged, this, &Window::dateFilterChanged); connect(toDateEdit, &QDateTimeEdit::dateChanged, this, &Window::dateFilterChanged);
請(qǐng)注意,每當(dāng)用戶更改其中一個(gè)過濾選項(xiàng)時(shí),我們必須顯式地重新應(yīng)用該過濾器,這是通過將各種編輯器連接到更新代理模型的函數(shù)來完成的。
proxyView = new QTreeView; proxyView->setRootIsDecorated(false); proxyView->setAlternatingRowColors(true); proxyView->setModel(proxyModel); proxyView->setSortingEnabled(true); proxyView->sortByColumn(1, Qt::AscendingOrder); QGridLayout *proxyLayout = new QGridLayout; proxyLayout->addWidget(proxyView, 0, 0, 1, 3); proxyLayout->addWidget(filterPatternLabel, 1, 0); proxyLayout->addWidget(filterWidget, 1, 1); proxyLayout->addWidget(fromLabel, 3, 0); proxyLayout->addWidget(fromDateEdit, 3, 1, 1, 2); proxyLayout->addWidget(toLabel, 4, 0); proxyLayout->addWidget(toDateEdit, 4, 1, 1, 2); proxyGroupBox = new QGroupBox(tr("Sorted/Filtered Model")); proxyGroupBox->setLayout(proxyLayout);
排序?qū)?由視圖處理,我們所要做的就是通過設(shè)置QTreeView::sortingEnabled屬性(默認(rèn)為false)來啟用代理視圖的排序。然后,我們將所有過濾小部件和代理視圖添加到安裝在相應(yīng)組框上的布局中。
QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(sourceGroupBox); mainLayout->addWidget(proxyGroupBox); setLayout(mainLayout); setWindowTitle(tr("Custom Sort/Filter Model")); resize(500, 450); }
最后,在將兩個(gè)組框放入安裝在主應(yīng)用程序小部件上的另一個(gè)布局中之后,自定義應(yīng)用程序窗口。
如上所述,在main()函數(shù)中創(chuàng)建源模型,調(diào)用Window::setSourceModel()函數(shù)使應(yīng)用程序使用它:
void Window::setSourceModel(QAbstractItemModel *model) { proxyModel->setSourceModel(model); sourceView->setModel(model); for (int i = 0; i < proxyModel->columnCount(); ++i) proxyView->resizeColumnToContents(i); for (int i = 0; i < model->columnCount(); ++i) sourceView->resizeColumnToContents(i); }
()函數(shù)使代理模型處理給定模型中的數(shù)據(jù),在本例中是郵件模型,視圖小部件從 類繼承的() 為視圖設(shè)置要呈現(xiàn)的模型。注意,后一個(gè)函數(shù)還將創(chuàng)建和設(shè)置一個(gè)新的選擇模型。
void Window::textFilterChanged() { FilterWidget::PatternSyntax s = filterWidget->patternSyntax(); QString pattern = filterWidget->text(); switch (s) { case FilterWidget::Wildcard: pattern = QRegularExpression::wildcardToRegularExpression(pattern); break; case FilterWidget::FixedString: pattern = QRegularExpression::escape(pattern); break; default: break; } QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption; if (filterWidget->caseSensitivity() == Qt::CaseInsensitive) options |= QRegularExpression::CaseInsensitiveOption; QRegularExpression regularExpression(pattern, options); proxyModel->setFilterRegularExpression(regularExpression); }
每當(dāng)用戶更改過濾器模式或區(qū)分大小寫時(shí),就調(diào)用textFilterChanged()函數(shù)。
首先檢索首選語法(FilterWidget::PatternSyntax enum用于解釋給定模式的含義),然后確定首選的大小寫敏感性。基于這些參數(shù)和當(dāng)前的過濾器模式,我們?cè)O(shè)置代理模型的filterRegularExpression 屬性。 屬性保存用于過濾源模型內(nèi)容的正則表達(dá)式,注意調(diào)用QSortFilterProxyModel的setfilterregulareexpression()函數(shù)也會(huì)更新模型。
void Window::dateFilterChanged() { proxyModel->setFilterMinimumDate(fromDateEdit->date()); proxyModel->setFilterMaximumDate(toDateEdit->date()); }
每當(dāng)用戶修改有效日期范圍時(shí),就調(diào)用dateFilterChanged()函數(shù),從用戶界面檢索新的日期,并調(diào)用相應(yīng)的函數(shù)(由自定義代理模型提供)來設(shè)置代理模型的最小和最大日期。如上所述,調(diào)用這些函數(shù)也會(huì)更新模型。
在本例中我們通過在main()函數(shù)中創(chuàng)建模型,將應(yīng)用程序與源模型分離開來。首先我們創(chuàng)建應(yīng)用程序,然后創(chuàng)建源模型:
int main(int argc, char *argv[]) { QApplication app(argc, argv); Window window; window.setSourceModel(createMailModel(&window)); window.show(); return app.exec(); }
createMailModel()函數(shù)是為簡(jiǎn)化構(gòu)造函數(shù)而提供的方便函數(shù),它所做的就是創(chuàng)建并返回一個(gè)描述電子郵件集合的模型。該模型是QStandardItemModel類的一個(gè)實(shí)例,也就是說,一個(gè)用于存儲(chǔ)自定義數(shù)據(jù)的通用模型,通常用作標(biāo)準(zhǔn)Qt數(shù)據(jù)類型的存儲(chǔ)庫,每個(gè)郵件描述都使用addMail()(另一個(gè)方便的函數(shù))添加到模型中。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)