PDA

View Full Version : Want the currentdate in QCalendarWidget to be underlined



ranjit.kadam
19th April 2011, 08:19
hello friends,I want the current date in QCalendar widget to be underlined always ,so how can we do it,because in default calendar widget,it highlights it for the first time but when we change the selection ,the current date disappears,so i want it to be identified by some marker even if we selected a different date in QCalendar widget.


regards
ranjit

high_flyer
19th April 2011, 12:00
Show the code you tried so far.

mcosta
19th April 2011, 12:08
Try with QCalendarWidget::setDateTextFormat.

For example



cw->setDateTextFormat(QDate::currentDate, textFormat);

ranjit.kadam
19th April 2011, 12:26
thanks mcosta,i got it,one morething i need to ask you,can i add a background image to cells of calendarwidget,please help me,i came to know that we have tp re-implement the paintcell method,but how to re-implement it i am not getting

mcosta
19th April 2011, 13:15
You have to inherit a class from QCalendarWidget and reimplement the method QCalendarWidget::paintCell.
For example



void MyCalendarWidget::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
painter->save(); // save standard settings

painter->setBrush (this->myBrush); // myBrush is a class member that store your background image

QCalendarWidget::paintCell (painter, rect, date);

painter->restore(); // restore previous settings
}

ranjit.kadam
19th April 2011, 14:17
thanks i will work on it..

Added after 17 minutes:

Hey mcosta,I have taken a Qcalendarwidget on a form,and in the constructor i have written this code,so whether its correct,please help me out


QPainter *p=new QPainter();
p->begin()
QRect r1(100,200,11,16);
QRect r2(100,200,11,16);
QImage image("C:/speedback.png");
p->drawImage(r1,image,r2);
ui->calendarWidget->paintCell(p ,r1,QDate::currentDate());

regards
ranjit

ranjit.kadam
20th April 2011, 08:22
hello friends please explain me out

mcosta
20th April 2011, 08:29
Are you sure that your code has not compilation errors?

QCalendarWidget::paintCell is protected, so you can't call it.

ranjit.kadam
20th April 2011, 09:41
yes mcosta,i got that error,so i made it public and it got executed ,but i didnt see that image , how to solve this issue? and whether i coded it correctly or something is missing,


thanks
ranjit

mcosta
20th April 2011, 09:48
yes mcosta,i got that error,so i made it public and it got executed


What means???
You cannot modify the QCalendarWidget code.
QCalendarWidget::paintCell is automatically called from QCalendarWidget::paintEvent, so you need to reimplement it.

Try with the code I suggested before

ranjit.kadam
20th April 2011, 10:46
ok i will try and get back to you

mcosta
20th April 2011, 11:32
Sorry,

probably in my code calling QCalendarWidget::paintCell doesn't work.

You have to draw the cell by hands

For Example



void MyCalendarWidget::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
painter->save ();

painter->fillRect (rect, Qt::cyan);
painter->drawText (rect, Qt::AlignCenter | Qt::AlignHCenter,
QString::number (date.day ()));

// More painting

painter->restore ();
}

ranjit.kadam
20th April 2011, 11:36
void Dialog::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
painter->save(); // save standard settings

painter->setBrush (this->myBrush); // myBrush is a class member that store your background image

QRectF target(10.0, 20.0, 80.0, 60.0);
QRectF source(10.0, 20.0, 70.0, 40.0);
QImage image("C:/speedback.png");
QRectF rect(100.0, 200.0, 80.0, 60.0);

painter->drawImage(target, image, source);

ui->calendarWidget->paintCell (painter, rect, date);

painter->restore(); // restore previous settings
}


i tried your code,but i am getting the following errors.

QCalendarwidget ::paintcell is protected..
QRect rect shadows a parameter.

high_flyer
20th April 2011, 11:48
Please use code tags when you post code!

mcosta
20th April 2011, 11:51
You have to inherit a class from QCalendarWidget and reimplement the method QCalendarWidget:: paintCell.

Do you understand what I mean for "inherit a class from QCalendarWidget" ??
If you don't, you have to study about C++ inheritance.

ranjit.kadam
20th April 2011, 12:06
yes mcosta,but i am getting confused because i have not coded for QCalendarwidget,i have directly taken it through designer

mcosta
20th April 2011, 12:46
ok,

create your class inherited from QCalendarWidget and then use Widget Promoting (http://doc.qt.nokia.com/4.7/designer-using-custom-widgets.html#promoting-widgets) in Designer

ranjit.kadam
20th April 2011, 13:30
//widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QtGui>
namespace Ui {
class Widget;
}

class Widget : public QWidget,QCalendarWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

private:
Ui::Widget *ui;

private slots:
void paintEvent(QPaintEvent *);
};

#endif // WIDGET_H


widget.cpp


#include "widget.h"
#include "ui_widget.h"
#include<QPainter>
#include<QFont>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}

Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(Qt::red);
painter.setBrush(Qt::green);
painter.setFont(QFont("Ariel",30));
painter.drawText(rect(),Qt::AlignCenter,"Qt");
QRectF rect(100.0, 200.0, 80.0, 60.0);
int startAngle = 50 * 16;
int spanAngle = 120 * 16;


painter.drawChord(rect, startAngle, spanAngle);
painter.fillRect(10,200,80,60,Qt::green);
painter.drawEllipse(rect);

QRectF target(10.0, 20.0, 80.0, 60.0);
QRectF source(10.0, 20.0, 70.0, 40.0);
QImage image("C:/speedback.png");


painter.drawImage(target, image, source);


painter.end();

}

void Widget::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
painter->save(); // save standard settings

//painter->setBrush (this->myBrush); // myBrush is a class member that store your background image

painter->setBrush(Qt::VerPattern);
QRect rect(100, 200, 80, 60);
painter->drawEllipse(rect);

ui->calendarWidget->paintCell (painter, rect, date);

painter->restore(); // restore previous settings

}


can i do in this way

or this way http://doc.trolltech.com/4.7.1/uitools-multipleinheritance.html

high_flyer
20th April 2011, 13:59
can i do in this way
First indication will be if the code compiles.
So try to compile it first.

ranjit.kadam
20th April 2011, 14:05
no hiflyer,i am getting error,
error:
request for member show ambiguous
candidates are void QWidget::show();

high_flyer
20th April 2011, 14:24
Right, and its not the only one.
be sure to read this carefully:
http://apidocs.meego.com/1.1/core/html/qt4/moc.html#multiple-inheritance-requires-qobject-to-be-first

ranjit.kadam
20th April 2011, 15:11
hey mcosta,please help me out

Added after 5 minutes:

hey hiflyer,where i have gone wrong