MainWindow::MainWindow() { QMenu *fileMenu = new QMenu(tr("&File")); QAction *saveAction = fileMenu->addAction(tr("&Save...")); saveAction->setShortcut(tr("Ctrl+S")); QAction *quitAction = fileMenu->addAction(tr("E&xit")); quitAction->setShortcut(tr("Ctrl+Q")); QMenu *insertMenu = new QMenu(tr("&Insert")); QAction *calendarAction = insertMenu->addAction(tr("&Calendar")); calendarAction->setShortcut(tr("Ctrl+I")); menuBar()->addMenu(fileMenu); menuBar()->addMenu(insertMenu); editor = new QTextEdit(this); connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile())); connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(calendarAction, SIGNAL(triggered()), this, SLOT(insertCalendar())); setCentralWidget(editor); setWindowTitle(tr("Text Document Writer")); }
//! [0] MainWindow::MainWindow() { selectedDate = QDate::currentDate(); fontSize = 10; QWidget *centralWidget = new QWidget; //! [0] //! [1] QLabel *dateLabel = new QLabel(tr("Date:")); QComboBox *monthCombo = new QComboBox; for (int month = 1; month <= 12; ++month) monthCombo->addItem(QDate::longMonthName(month)); QDateTimeEdit *yearEdit = new QDateTimeEdit; yearEdit->setDisplayFormat("yyyy"); yearEdit->setDateRange(QDate(1753, 1, 1), QDate(8000, 1, 1)); //! [1] monthCombo->setCurrentIndex(selectedDate.month() - 1); yearEdit->setDate(selectedDate); //! [2] QLabel *fontSizeLabel = new QLabel(tr("Font size:")); QSpinBox *fontSizeSpinBox = new QSpinBox; fontSizeSpinBox->setRange(1, 64); fontSizeSpinBox->setValue(10); editor = new QTextBrowser; insertCalendar(); //! [2] //! [3] connect(monthCombo, SIGNAL(activated(int)), this, SLOT(setMonth(int))); connect(yearEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setYear(QDate))); connect(fontSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setFontSize(int))); //! [3] //! [4] QHBoxLayout *controlsLayout = new QHBoxLayout; controlsLayout->addWidget(dateLabel); controlsLayout->addWidget(monthCombo); controlsLayout->addWidget(yearEdit); controlsLayout->addSpacing(24); controlsLayout->addWidget(fontSizeLabel); controlsLayout->addWidget(fontSizeSpinBox); controlsLayout->addStretch(1); QVBoxLayout *centralLayout = new QVBoxLayout; centralLayout->addLayout(controlsLayout); centralLayout->addWidget(editor, 1); centralWidget->setLayout(centralLayout); setCentralWidget(centralWidget); //! [4] }
Mycalendar::Mycalendar(QDialog *parent) : QDialog(parent) { setupUi(this); mycalendat=new MyCTCalendar; mycalendat->InitConnection(QDir::currentPath ()); selectedDate = QDate::currentDate(); for (int month = 1; month <= 12; ++month) monthCombo->addItem(QDate::longMonthName(month)); monthCombo->setCurrentIndex(selectedDate.month() - 1); YearSelect->setDate(selectedDate); insertCalendar(); connect(monthCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setMonth(int))); connect(monthCombo, SIGNAL(activated(int)), this, SLOT(setMonth(int))); connect(YearSelect, SIGNAL(dateChanged(QDate)), this, SLOT(setYear(QDate))); }
//! [17] void MainWindow::setYear(QDate date) { selectedDate = QDate(date.year(), selectedDate.month(), selectedDate.day()); insertCalendar(); }
//! [16] void MainWindow::setMonth(int month) { selectedDate = QDate(selectedDate.year(), month + 1, selectedDate.day()); insertCalendar(); }
//! [15] void MainWindow::setFontSize(int size) { fontSize = size; insertCalendar(); }