connect(left,SIGNAL(clicked()),this,SLOT(previousWeek()));
public slots: void previousWeek(); void nextWeek();
#include "week2.h"#include"QDate"#include"QDebug"week2::week2(QWidget *parent): QWidget(parent){ width=100; numberOfWeek=0; setGeometry(0,0,700,30); setMaximumWidth(2000); left=new QPushButton(this); left->setGeometry(0,0,10,30); connect(left,SIGNAL(clicked()),this,SLOT(previousWeek())); right=new QPushButton(this); connect(right,SIGNAL(clicked()),this,SLOT(nextWeek()));}week2::~week2(){ delete left; }void week2::paintEvent(QPaintEvent *){ QStylePainter p(this); QDate date; int dayOfWeek=date.currentDate().dayOfWeek(); QStyleOptionButton monday,tuesday,wednesday,thursday,friday,saturday,sunday; monday.initFrom(this); monday.state=QStyle::State_Raised | QStyle::State_Enabled; monday.rect=QRect(10,0,width-10,30); monday.text=(date.currentDate().addDays(1-dayOfWeek+numberOfWeek).toString()); tuesday.initFrom(this); tuesday.state=QStyle::State_Raised | QStyle::State_Enabled; tuesday.rect=QRect(width,0,width,30); tuesday.text=(date.currentDate().addDays(2-dayOfWeek+numberOfWeek).toString()); wednesday.initFrom(this); wednesday.state=QStyle::State_Raised | QStyle::State_Enabled; wednesday.rect=QRect(width*2,0,width,30); wednesday.text=(date.currentDate().addDays(3-dayOfWeek+numberOfWeek).toString()); thursday.initFrom(this); thursday.state=QStyle::State_Raised | QStyle::State_Enabled; thursday.rect=QRect(width*3,0,width,30); thursday.text=(date.currentDate().addDays(4-dayOfWeek+numberOfWeek).toString()); friday.initFrom(this); friday.state=QStyle::State_Raised | QStyle::State_Enabled; friday.rect=QRect(width*4,0,width,30); friday.text=(date.currentDate().addDays(5-dayOfWeek+numberOfWeek).toString()); saturday.initFrom(this); saturday.state=QStyle::State_Raised | QStyle::State_Enabled; saturday.rect=QRect(width*5,0,width,30); saturday.text=(date.currentDate().addDays(6-dayOfWeek+numberOfWeek).toString()); sunday.initFrom(this); sunday.state=QStyle::State_Raised | QStyle::State_Enabled; sunday.rect=QRect(width*6,0,width-10,30); sunday.text=(date.currentDate().addDays(7-dayOfWeek+numberOfWeek).toString()); right->setGeometry(width*7-10,0,10,30); p.drawControl(QStyle::CE_PushButton,monday); p.drawControl(QStyle::CE_PushButton,tuesday); p.drawControl(QStyle::CE_PushButton,wednesday); p.drawControl(QStyle::CE_PushButton,thursday); p.drawControl(QStyle::CE_PushButton,friday); p.drawControl(QStyle::CE_PushButton,saturday); p.drawControl(QStyle::CE_PushButton,sunday);}void week2::nextWeek(){ numberOfWeek+=7; qDebug()<<numberOfWeek; update();}void week2::previousWeek(){ numberOfWeek-=7; qDebug()<<numberOfWeek; update();}void week2::resize(int w){ width=w; update();}