QT:日期时间编辑器MyDateTimeEdit

    QT自带的QDateTimeEdit并不是很好用,所以我写了一个基于六个QComboBox的DateTimeWidget,感觉上比QDateTimeEdit好用多了,希望大家喜欢。

思路:
1:建立6个QComboBox,分别用来储存年月日时分秒,还有5个QLabel,用来作它们的分隔符。
2:每个月的天数会随着年份数和月份数变化,所以表示日期的QComboBox的内容也要发生相应的变化。
3:为这个DateTimeWidget加入一些常用的函数,如GetDateTime(),day(), setDay() 

(为了保存代码的简洁,很多常用的函数(如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();
}


你可能感兴趣的:(Date,layout,Class,qt,Signal)