翻譯|使用教程|編輯:龔雪|2023-12-27 10:29:35.180|閱讀 166 次
概述:本文將為大家介紹如何使用Qt Widget小部件中的QCalendarWidget來(lái)實(shí)現(xiàn)日歷功能,歡迎下載最新版組件體驗(yàn)~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Qt 是目前最先進(jìn)、最完整的跨平臺(tái)C++開(kāi)發(fā)工具。它不僅完全實(shí)現(xiàn)了一次編寫,所有平臺(tái)無(wú)差別運(yùn)行,更提供了幾乎所有開(kāi)發(fā)過(guò)程中需要用到的工具。如今,Qt已被運(yùn)用于超過(guò)70個(gè)行業(yè)、數(shù)千家企業(yè),支持?jǐn)?shù)百萬(wàn)設(shè)備及應(yīng)用。
本文中的CalendarWidget示例展示了QCalendarWidget的用法。
 
 
QCalendarWidget一次顯示一個(gè)日歷月,并允許用戶選擇一個(gè)日期。日歷由四個(gè)組件組成:一個(gè)允許用戶更改顯示月份的導(dǎo)航欄、一個(gè)網(wǎng)格、其中每個(gè)單元格表示一個(gè)月中的一天,以及兩個(gè)顯示星期名稱和星期數(shù)字的標(biāo)題。
Qt技術(shù)交流群:166830288 歡迎一起進(jìn)群討論
CalendarWidget示例顯示一個(gè)QCalendarWidget,并允許用戶使用QComboBoxes、QCheckBoxes和QDateEdits配置其外觀和行為。此外,用戶可以影響單個(gè)日期和標(biāo)題的格式。
QCalendarWidget的屬性總結(jié)如下所示:
本示例包含一個(gè)類Window,它創(chuàng)建并布局QCalendarWidget和其他讓用戶配置QCalendarWidget的小部件。
下面是Window類的定義:
	
class Window : public QWidget
{
Q_OBJECT
public:
Window(QWidget *parent = nullptr);
private slots:
void localeChanged(int index);
void firstDayChanged(int index);
void selectionModeChanged(int index);
void horizontalHeaderChanged(int index);
void verticalHeaderChanged(int index);
void selectedDateChanged();
void minimumDateChanged(QDate date);
void maximumDateChanged(QDate date);
void weekdayFormatChanged();
void weekendFormatChanged();
void reformatHeaders();
void reformatCalendarPage();
private:
void createPreviewGroupBox();
void createGeneralOptionsGroupBox();
void createDatesGroupBox();
void createTextFormatsGroupBox();
QComboBox *createColorComboBox();
QGroupBox *previewGroupBox;
QGridLayout *previewLayout;
QCalendarWidget *calendar;
QGroupBox *generalOptionsGroupBox;
QLabel *localeLabel;
QLabel *firstDayLabel;
...
QCheckBox *mayFirstCheckBox;
};
	
與表示自包含窗口的類一樣,大多數(shù)API都是私有的。當(dāng)我們?cè)趫?zhí)行過(guò)程中偶然發(fā)現(xiàn)私有成員時(shí),將對(duì)其進(jìn)行審查。
現(xiàn)在讓我們回顧一下類的實(shí)現(xiàn),從構(gòu)造函數(shù)開(kāi)始:
	
Window::Window(QWidget *parent)
: QWidget(parent)
{
createPreviewGroupBox();
createGeneralOptionsGroupBox();
createDatesGroupBox();
createTextFormatsGroupBox();
QGridLayout *layout = new QGridLayout;
layout->addWidget(previewGroupBox, 0, 0);
layout->addWidget(generalOptionsGroupBox, 0, 1);
layout->addWidget(datesGroupBox, 1, 0);
layout->addWidget(textFormatsGroupBox, 1, 1);
layout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(layout);
previewLayout->setRowMinimumHeight(0, calendar->sizeHint().height());
previewLayout->setColumnMinimumWidth(0, calendar->sizeHint().width());
setWindowTitle(tr("Calendar Widget"));
}
	
我們首先使用四個(gè)私有的create…GroupBox()函數(shù)創(chuàng)建四個(gè)qgroupbox及其子部件(包括QCalendarWidget),如下所述,然后我們?cè)赒GridLayout中安排組框。
我們將網(wǎng)格布局的調(diào)整大小策略設(shè)置為QLayout::SetFixedSize,以防止用戶調(diào)整窗口大小。在這種模式下,窗口的大小由QGridLayout根據(jù)其內(nèi)容小部件的大小提示自動(dòng)設(shè)置。
為了確保在每次更改QCalendarWidget的屬性(例如,隱藏導(dǎo)航欄、垂直標(biāo)題或網(wǎng)格)時(shí)不會(huì)自動(dòng)調(diào)整窗口的大小,我們將第0行的最小高度和第0列的最小寬度設(shè)置為QCalendarWidget的初始大小。
讓我們來(lái)看看createPreviewGroupBox()函數(shù):
	
void Window::createPreviewGroupBox()
{
previewGroupBox = new QGroupBox(tr("Preview"));
calendar = new QCalendarWidget;
calendar->setMinimumDate(QDate(1900, 1, 1));
calendar->setMaximumDate(QDate(3000, 1, 1));
calendar->setGridVisible(true);
connect(calendar, &QCalendarWidget::currentPageChanged,
this, &Window::reformatCalendarPage);
previewLayout = new QGridLayout;
previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter);
previewGroupBox->setLayout(previewLayout);
}
	
Preview組框只包含一個(gè)小部件:QCalendarWidget。我們?cè)O(shè)置它,將它的currentPageChanged()信號(hào)連接到reformatCalendarPage()插槽,以確保每個(gè)新頁(yè)面都獲得用戶指定的格式。
createGeneralOptionsGroupBox()函數(shù)有點(diǎn)大,并且以相同的方式設(shè)置了幾個(gè)小部件。我們將在這里查看它的部分實(shí)現(xiàn),并跳過(guò)其余部分:
	
void Window::createGeneralOptionsGroupBox()
{
generalOptionsGroupBox = new QGroupBox(tr("General Options"));
localeCombo = new QComboBox;
int curLocaleIndex = -1;
int index = 0;
for (int _lang = QLocale::C; _lang <= QLocale::LastLanguage; ++_lang) {
QLocale::Language lang = static_cast<QLocale::Language>(_lang);
const auto locales =
QLocale::matchingLocales(lang, QLocale::AnyScript, QLocale::AnyTerritory);
for (auto loc : locales) {
QString label = QLocale::languageToString(lang);
auto territory = loc.territory();
label += QLatin1Char('/');
label += QLocale::territoryToString(territory);
if (locale().language() == lang && locale().territory() == territory)
curLocaleIndex = index;
localeCombo->addItem(label, loc);
++index;
}
}
if (curLocaleIndex != -1)
localeCombo->setCurrentIndex(curLocaleIndex);
localeLabel = new QLabel(tr("&Locale"));
localeLabel->setBuddy(localeCombo);
firstDayCombo = new QComboBox;
firstDayCombo->addItem(tr("Sunday"), Qt::Sunday);
firstDayCombo->addItem(tr("Monday"), Qt::Monday);
firstDayCombo->addItem(tr("Tuesday"), Qt::Tuesday);
firstDayCombo->addItem(tr("Wednesday"), Qt::Wednesday);
firstDayCombo->addItem(tr("Thursday"), Qt::Thursday);
firstDayCombo->addItem(tr("Friday"), Qt::Friday);
firstDayCombo->addItem(tr("Saturday"), Qt::Saturday);
firstDayLabel = new QLabel(tr("Wee&k starts on:"));
firstDayLabel->setBuddy(firstDayCombo);
...
	
篇幅有限,更多內(nèi)容持續(xù)關(guān)注,下期見(jiàn)~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)