(为了保存代码的简洁,很多常用的函数(如day(),setDay())没有加上。)
截图:
源代码:
#include <QtGui> enum ComboBoxContent { YEAR, MOUTH, DAY, HOUR, MINUTE, SECOND }; class MyDateTimeEdit : public QWidget { Q_OBJECT private: //6个下拉框,分别表示年月日时分秒 QComboBox *m_ComboxBox[6]; //6个下拉框之间的分隔符 QLabel *m_Label[5]; void SetLabelContent(); void SetComboBoxContent(); public: MyDateTimeEdit(QWidget *parent = 0); ~MyDateTimeEdit(); //为了保存代码的简洁,很多常用的函数(如day(),setDay())没有加上, //有需要的话,就自己加一下吧 public slots: void ClearContent(); QDateTime GetDateTime(); private slots: void YearMouthChangeSlot(int index); }; MyDateTimeEdit::MyDateTimeEdit(QWidget *parent) { QHBoxLayout *layout = new QHBoxLayout; layout->setSizeConstraint(QLayout::SetMinimumSize); for(int i=0; i<6; i++) { m_ComboxBox[i] = new QComboBox(this); if( 0 == i ) m_ComboxBox[i]->setFixedWidth(60); else m_ComboxBox[i]->setFixedWidth(40); layout->addWidget(m_ComboxBox[i]); if( i != 5 ) { m_Label[i] = new QLabel(this); layout->addWidget(m_Label[i]); } } setLayout(layout); SetLabelContent(); SetComboBoxContent(); //当年与月变化时,“日”下拉框的内容也要相应的变化(28~31) connect(m_ComboxBox[YEAR], SIGNAL(currentIndexChanged(int)), this, SLOT(YearMouthChangeSlot(int))); connect(m_ComboxBox[MOUTH], SIGNAL(currentIndexChanged(int)), this, SLOT(YearMouthChangeSlot(int))); } MyDateTimeEdit::~MyDateTimeEdit() { } void MyDateTimeEdit::SetLabelContent() { m_Label[0]->setText("-"); m_Label[1]->setText("-"); m_Label[2]->setText(" "); m_Label[3]->setText(":"); m_Label[4]->setText(":"); } void MyDateTimeEdit::SetComboBoxContent() { for(int i=1970; i<=2020; i++) m_ComboxBox[YEAR]->addItem( QString::number(i) ); for(int i=1; i<=12; i++) m_ComboxBox[MOUTH]->addItem( QString::number(i) ); for(int i=0; i<24; i++) m_ComboxBox[HOUR]->addItem( QString::number(i) ); for(int i=0; i<60; i++) m_ComboxBox[MINUTE]->addItem( QString::number(i) ); for(int i=0; i<60; i++) m_ComboxBox[SECOND]->addItem( QString::number(i) ); ClearContent(); } void MyDateTimeEdit::YearMouthChangeSlot(int index) { if( m_ComboxBox[YEAR]->currentText().isEmpty() || m_ComboxBox[MOUTH]->currentText().isEmpty() ) return; QString oldDay = m_ComboxBox[DAY]->currentText(); QDate date( m_ComboxBox[YEAR]->currentText().toInt(), m_ComboxBox[MOUTH]->currentText().toInt(), 1); m_ComboxBox[DAY]->clear(); int day = date.daysInMonth(); for(int i=1; i<=day; i++) m_ComboxBox[DAY]->addItem( QString::number(i) ); m_ComboxBox[DAY]->setCurrentIndex( m_ComboxBox[DAY]->findText(oldDay) ); } void MyDateTimeEdit::ClearContent() { for(int i=0; i<6; i++) m_ComboxBox[i]->setCurrentIndex(-1); } QDateTime MyDateTimeEdit::GetDateTime() { //获取时间 QDate date( m_ComboxBox[YEAR]->currentText().toInt(), m_ComboxBox[MOUTH]->currentText().toInt(), m_ComboxBox[DAY]->currentText().toInt() ); QTime time( m_ComboxBox[HOUR]->currentText().toInt(), m_ComboxBox[MINUTE]->currentText().toInt(), m_ComboxBox[SECOND]->currentText().toInt() ); return QDateTime(date, time); } #include "main.moc" int main(int argc, char **argv) { QApplication app(argc, argv); MyDateTimeEdit *temp = new MyDateTimeEdit; temp->show(); return app.exec(); }