demos中的万年历,节选

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QDate>
#include <QTextBrowser>
class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    void insertCalendar();
    int fontSize;
    QDate selectedDate;
    QTextBrowser *editor;
};
#endif // MAINWINDOW_H
-------------------------------------
#include "mainwindow.h"
#include <QLabel>
#include <QDateTimeEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    selectedDate = QDate::currentDate();
    fontSize = 10;
    QWidget *centralWidget = new QWidget;///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.MM.dd");
 yearEdit->setDateRange(QDate(1753, 1, 1), QDate(8000, 1, 1));
 monthCombo->setCurrentIndex(selectedDate.month() - 1);
 yearEdit->setDate(selectedDate);///
    QLabel *fontSizeLabel = new QLabel(tr("Font size:"));
    QSpinBox *fontSizeSpinBox = new QSpinBox;
    fontSizeSpinBox->setRange(1, 64);
    editor = new QTextBrowser;
    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)));
    fontSizeSpinBox->setValue(10);
    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);
}
MainWindow::~MainWindow()
{
    
}
-----------------------
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}

你可能感兴趣的:(Date,object,Signal)