How to Develop a Calendar Widget.
-
[quote author="imrrk" date="1302591858"]hi andre,
i opened a new dialog with the below code:
@void Dialog::on_calendarWidget_clicked(QDate date)
{
Dialog1 a(this);
a.show();
a.exec();
}@
hey
dlg.setDateToWorkWith(Date).whether its in built function?,because i didnt find it..[/quote]
Eh, what do you think? I make up an obviously fake dialog name with an obviously fake method name, and you expect that to be in Qt by default. And what should that method do then, exactly? Also: there is no need to call both show() and exec(). Either one will suffice.
[quote]
and one more thing ,i want to know can we change the color of cells ?[/quote]
You mean of your calendar widget, right? Perhaps you could read the documentation of the QCalendarWidget? I just did, and found what you need in less than ten seconds. -
Hello Andre,you r really mistaken,I didnt say that u r fake dialog name should work,I know that,its just an example to understand,I was just asking about the setDateToWorkwith(Date) thats it..
and its great that you have found out it in tensecs..I will also try and will get back to you..Thanks
imrrk -
Problem is, you did not say what you would like to use the date for in your dialog, so how should we know how that method should look? Let alone, how would the Qt developers a couple of years ago have known?
It could be as simple as this:
@
void MyFancyDialog::setDateToWorkWith(QDate date) //you might want to think of a better name than this...
{
//m_ui is a member pointer to your UI class instance
//your .ui has a QLabel on it called "dateLabel"
m_ui->dateLabel->setText(date.toString(Qt::SystemLocaleLongDate));
}
@But perhaps your dialog has to do something way more fancy with the date than that. Since you did not tell us, it is hard to give a better example.
-
Ok,andre I apologize for that,but andre,my question of concern is that,suppose I will give one example here,suppose in a month of 30 or 31 or 28 days ,suppose it will rain on 5 days,and other 5 days it will be hot,another 5 days it will be very cold ,,,and so..on..So suppose the user selects one of the date and he wants to know the above statics,then what should happen is that,all the above climatic conditions should be represnted as different colors on date,the colors should get set on dates depending on the climatic condition...i.e is rain-blue,hot-red,cold-green and so on..so whether this is possible..
thanks
imrrk -
Yes, of course that is possible, but that is something different again than what you asked before about using a separate dialog, and what that dialog is supposed to do. Now, you are talking about styling the QCalendarWidget again. Did you take a look at the documentation of that class yet? Did you find out how you can style the display of a specific date?
Edit:
Just to proof that I am not bulshitting you about that this is perfectly possible with QCalendarWidget, I put this example together:!http://dl.dropbox.com/u/16442531/coloredCalendar.png(Colored calendar example)!
This is what you were after, right?
-
Hi imrrk,
that is all up to you and your application.
You asked about how to handle the click on a calendar date and how to open a dialog. You got the answers. We will not write your application. A little bit is also up to you.The general info on how to react on a calendar click and to open a dialog was given.
-
[quote author="Andre" date="1302601186"]Yes, of course that is possible, but that is something different again than what is supposed to be in a separate dialog, and what that dialog is supposed to do. Now, you are talking about styling the QCalendarWidget again. Did you take a look at the documentation of that class yet? Did you find out how you can style the display of a specific date?[/quote]
Styling the calndar items can only be done by subclassign QCalendarWidget and by overwriting
- virtual void paintCell(QPainter* pPainter, const QRect& rCellRect, const QDate& rDate) const;
-
[quote author="Gerolf" date="1302601571"]
[quote author="Andre" date="1302601186"]Yes, of course that is possible, but that is something different again than what is supposed to be in a separate dialog, and what that dialog is supposed to do. Now, you are talking about styling the QCalendarWidget again. Did you take a look at the documentation of that class yet? Did you find out how you can style the display of a specific date?[/quote]Styling the calndar items can only be done by subclassign QCalendarWidget and by overwriting
- virtual void paintCell(QPainter* pPainter, const QRect& rCellRect, const QDate& rDate) const;
[/quote]
Untrue. The sample I posted above was done without subclassing QCalendarWidget.
- virtual void paintCell(QPainter* pPainter, const QRect& rCellRect, const QDate& rDate) const;
-
[quote author="imrrk" date="1302603498"]Thanks Andre,for ur valuable information,but whether we should use paintcell function or no..if not which function to use..to achieve it..
[/quote]
You really couldn't find the methods in QCalendarWidget that control the format? The "Format" in the method name did not give the function away? And you also did not see this piece of text in the documentation?
[quote]The rendering of the headers, weekdays or single days can be largely customized by setting QTextCharFormat's for some special weekday, a special date or for the rendering of the headers.[/quote]
Note that there are several methods that allow you to set a QTextCharFormat (with similar names), but this is the code I used to create the image I posted above:@
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);//initialize the formats used QTextCharFormat coldDate; coldDate.setBackground(Qt::blue); QTextCharFormat hotDate; hotDate.setBackground(Qt::red); QTextCharFormat wetDate; wetDate.setBackground(Qt::green); QTextCharFormat yellowDate; yellowDate.setBackground(Qt::yellow); //set the dates and the format to use for them ui->calendar->setDateTextFormat(QDate(2011, 4, 1), coldDate); ui->calendar->setDateTextFormat(QDate(2011, 4, 13), wetDate); ui->calendar->setDateTextFormat(QDate(2011, 4, 21), wetDate); ui->calendar->setDateTextFormat(QDate(2011, 4, 27), hotDate); ui->calendar->setDateTextFormat(QDate(2011, 4, 6), yellowDate); ui->calendar->setDateTextFormat(QDate(2011, 4, 28), hotDate);
}
@Obviously, this being a very simple prototype, I simply hard-coded some random dates for this month with some formats. In a real application you'd get the relevant information from somewhere else, and populate the calendar based on that for the dates that are currently visible.
-
Hello Andre,thanks for your code snippet,its setting the color of the cells specified,I have one question,actually I want to set the colors of the cells in Qcalendarwidget by clicking a button say "set",which is present in another dialog,So when i click on set and close the new dialog all the color changes should be reflected in my main-dialog which contains qcalendarwidget..
thanks &Regards
imrrk -
- Hi andre,I have my dialog.ui where I have dragged qcalendar widget and one push button and on click of this pushbutton,I have written the code for setting of colors in different cells,and its working 100% fine.
- Now I have one more " dialog1.ui" and in designer mode again I dragged two radio buttons."1-set" and "2-unset".Now I have two options with radiobuttons for setting and unsetting colors,so i will select radiobutton 1-set and press ok pusshbutton in dialog1.ui,* So when I press this button in dialog1.ui the colors should get set to cells in my previous dialog.ui which contains qcalendarwidget.So i am confused about how to do that.
-
What have you tried so far? Did you use the code snippets that you were shown earlier in this thread, on how to display a dialog based on a click? As you have been told before: we are not going to write your software for you, even if that would be way faster than trying to help you do it yourself.
-
Hello Andre..yes I have used the code snippets,and i know how to display the dialog based on a click,and i can link many forms ,and I am not asking you to write my software,as you are a qt expert,I m asking you,and as you are a qt expert my question may appear silly for you.I dont want you to write my software,but if you have understood my above question,then I request you to tell me the direction so that I can achieve my destination..
thanks&Regards
imrrk -
There is no need to grovel, just a need to understand what you are asking me (us) to do. You are asking me for general help, but you are not showing (only telling when asked) how far you got along yourself. That makes it very hard to give specific help without dropping big amounts of code here in the forum, hoping that that will contain a solution for whatever problem you seem to be having.
So, please, show us the relevant code that you have now for dealing with this issue: how you store the days to highlight now, how you respond to the mouse click, what your dialog looks like, and give a precise description of what it is you're struggling with. Please, do write that description in short, but full sentences. That is, with all the proper punctuation marks like points, comma's where needed (not like the above), and if appropriate, using bullet lists or other devices. Your texts are often very hard to read, and that makes it hard to understand your actual question. The time spend to formulate a good, understandable question is not time that you waste, but is in fact very useful also for yourself to order your thoughts on the topic.
-
Hello Andre,thank you for making understand me the rules of the forum.
So here is my code so far.
@#include "dialog.h"
#include "ui_dialog.h"
#include<QCalendarWidget>
#include<QDate>
#include "dialog1.h"
#include "ui_dialog1.h"
#include<QPainter>Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);}
Dialog::~Dialog()
{
delete ui;
}void Dialog::on set_clicked()
{colddate.setBackground(Qt::yellow);//colddate has been declared in .h file it is a object of QTextCharFormat
hotdate.setBackground(Qt::red);
wetdate.setBackground(Qt::green);
raindate.setBackground(Qt::blue);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,1),colddate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,5),hotdate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,8),wetdate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,9),raindate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,12),colddate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,14),hotdate);
}void Dialog::on unset_clicked()
{colddate.clearBackground();//function to clear the background
hotdate.clearBackground();
wetdate.clearBackground();
raindate.clearBackground();ui->calendarWidget->setDateTextFormat(QDate(2011,4,1),colddate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,5),hotdate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,8),wetdate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,9),raindate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,12),colddate);
ui->calendarWidget->setDateTextFormat(QDate(2011,4,14),hotdate);
}
@
Here I have used the designer for qcalendarwidget and two pushbuttons which i have named set and unset respectively,and wriiten the code for click as shown above.Now these 3 widgets are on one form,and they work perfectly.
But if the push buttons are on different form,and Qcalendar widget on another form,then when i click on these,how can set colors of qcalendarwidget which is in the another form.and also I tried to add backgroundimage to cell using
colddate.BackgroundImageUrl.... -
Declare your own two signals (setClicked() and unsetClicked()) in the second form, emit these signals once the respective button is clicked. In your first dialog create two slots (eg. setWeatherColors() and unsetWeatherColors()). These slots contain the code from on_set_clicked and on_unset_clicked. Connect the two signals from the second dialog to the slots in the first (do this in the very same place where you instantiate the second dialog).
If you do not know how to declare signals and slots: Start reading "here":http://doc.qt.nokia.com/4.7/signalsandslots.html#signals, the page contains a complete example for both.