翻譯|使用教程|編輯:龔雪|2023-05-05 10:54:07.230|閱讀 144 次
概述:本文將為大家介紹Qt框架中的自定義排序/篩選模型示例,歡迎下載相關組件體驗~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Qt 是目前最先進、最完整的跨平臺C++開發工具。它不僅完全實現了一次編寫,所有平臺無差別運行,更提供了幾乎所有開發過程中需要用到的工具。如今,Qt已被運用于超過70個行業、數千家企業,支持數百萬設備及應用。
自定義排序/篩選模型示例說明了如何子類化來執行高級排序和篩選。
在上文中(點擊這里回顧>>),為大家講解了如何實現MySortFilterProxyModel類、MySortFilterProxyModel的定義,本文將繼續講解Window類的定義和實現!
Qt技術交流群:166830288 歡迎一起進群討論
CustomFilter類繼承了QWidget,并提供了這個例子的主應用程序窗口:
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; };
我們實現了兩個私有槽,textFilterChanged()和dateFilterChanged(),用于響應用戶更改過濾器模式、區分大小寫或任何日期,此外還實現了一個公共的setSourceModel()方便函數來設置模型/視圖關系。
在本示例中,我們選擇在main()函數中創建和設置源模型,因此在構建主應用程序窗口時,假設源模型已經存在,并從創建自定義代理模型的實例開始:
Window::Window() { proxyModel = new MySortFilterProxyModel(this);
我們設置屬性,該屬性保存代理模型是否被動態排序和過濾。通過將該屬性設置為true,可以確保在源模型的內容發生變化時對模型進行排序和過濾。
主應用程序窗口顯示源模型和代理模型的視圖,源視圖非常簡單:
sourceView = new QTreeView; sourceView->setRootIsDecorated(false); sourceView->setAlternatingRowColors(true);
QTreeView類提供了樹視圖的默認模型/視圖實現,視圖實現了應用程序源模型中項目的樹表示。
sourceLayout->addWidget(sourceView); sourceGroupBox = new QGroupBox(tr("Original Model")); sourceGroupBox->setLayout(sourceLayout);
類提供了樹視圖的默認模型/視圖實現,視圖實現了應用程序源模型中項目的樹表示,將視圖小部件添加到我們安裝在相應組框上的布局中。
另一方面代理模型視圖包含幾個控件,這些控件控制轉換源模型數據結構的各個方面:
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);
請注意,每當用戶更改其中一個過濾選項時,我們必須顯式地重新應用該過濾器,這是通過將各種編輯器連接到更新代理模型的函數來完成的。
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);
排序將由視圖處理,我們所要做的就是通過設置QTreeView::sortingEnabled屬性(默認為false)來啟用代理視圖的排序。然后,我們將所有過濾小部件和代理視圖添加到安裝在相應組框上的布局中。
QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(sourceGroupBox); mainLayout->addWidget(proxyGroupBox); setLayout(mainLayout); setWindowTitle(tr("Custom Sort/Filter Model")); resize(500, 450); }
最后,在將兩個組框放入安裝在主應用程序小部件上的另一個布局中之后,自定義應用程序窗口。
如上所述,在main()函數中創建源模型,調用Window::setSourceModel()函數使應用程序使用它:
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); }
()函數使代理模型處理給定模型中的數據,在本例中是郵件模型,視圖小部件從 類繼承的() 為視圖設置要呈現的模型。注意,后一個函數還將創建和設置一個新的選擇模型。
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); }
每當用戶更改過濾器模式或區分大小寫時,就調用textFilterChanged()函數。
首先檢索首選語法(FilterWidget::PatternSyntax enum用于解釋給定模式的含義),然后確定首選的大小寫敏感性。基于這些參數和當前的過濾器模式,我們設置代理模型的filterRegularExpression 屬性。 屬性保存用于過濾源模型內容的正則表達式,注意調用QSortFilterProxyModel的setfilterregulareexpression()函數也會更新模型。
void Window::dateFilterChanged() { proxyModel->setFilterMinimumDate(fromDateEdit->date()); proxyModel->setFilterMaximumDate(toDateEdit->date()); }
每當用戶修改有效日期范圍時,就調用dateFilterChanged()函數,從用戶界面檢索新的日期,并調用相應的函數(由自定義代理模型提供)來設置代理模型的最小和最大日期。如上所述,調用這些函數也會更新模型。
在本例中我們通過在main()函數中創建模型,將應用程序與源模型分離開來。首先我們創建應用程序,然后創建源模型:
int main(int argc, char *argv[]) { QApplication app(argc, argv); Window window; window.setSourceModel(createMailModel(&window)); window.show(); return app.exec(); }
createMailModel()函數是為簡化構造函數而提供的方便函數,它所做的就是創建并返回一個描述電子郵件集合的模型。該模型是QStandardItemModel類的一個實例,也就是說,一個用于存儲自定義數據的通用模型,通常用作標準Qt數據類型的存儲庫,每個郵件描述都使用addMail()(另一個方便的函數)添加到模型中。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網