Building My custom Calender [Moved]
-
Of course: you are creating new buttons on line 13 of your createLayout code, and you do that each time your month changes.
What you need to do, I think, is create your matrix of buttons just once in the constructor your class, and make sure you have enough of them to fit any month. It is not hard to figure out the maximum number of weeks in a month...
Then, in your createLayout code, instead of creating new buttons, you simply set the day number as the text on each of the buttons. You may or may not want to have the buttons that represent dates outside of the current month enabled.
Note: I still don't see how this thread is so different from the existing "Create calendar out of buttons" threads.
-
Hello Andre
can you please give me the idea of how i can create a matrix of buttons just once in the contructor..i just try it you please check whether it is right or wrong..
@QPushButton *button
//code in constructor
button=new QPushButton[6][7];@
regards
Rahul -
no, that is wrong. You can't use new X[][] in C++!
use something like@
QVector< QVector<QPushButton*> >
@or
@
QPushButton* buttons[6][7];
@Then you have to create all the buttons in the constructor. But this is basic C++.
-
hello gerolf
Actually i have declared @QpushButton * buttons[6][7];@
now before used it in my code i have to create a matrix of buttons just once in the contructor so that i can used it in my code given below in place of bold code without using new so that i can simply use button[count][weekday].setText(QString::number(date.day()))
@while(date.month()==selectedDate.month())
{
int weekDay=date.dayOfWeek();
button[count][weekDay] = new QPushButton(QString::number(date.day())); // wrong use as new button is already created
controlsLayout->addWidget(button[count][weekDay], count, weekDay);
date=date.addDays(1);
if (weekDay == 7 && date.month() == selectedDate.month())
count++;}@
with regards
rahul -
Personally, I would do something like this:
in header:
@
QVector<QPushButton*> m_dayButtons;
@@
QGridLayout* layout = new QGridLayout(this);
m_dayButtons.reserve(42);
for (int i(0); i<42; ++i) {
QPushButton* dayButton = new QPushButton();
m_dayButtons.append(dayButton);
int row = i%7;
int column = i/7;
layout->addWidget(dayButton, row, column);
}
addLayout(layout);
@That is: don't actually create a matrix, but just a list of buttons, and just have the layout take care of them being in a matrix form. Then, in the function where you set the month to represent, you simply use the QDate::dayOfWeek() to figure out which item in your list to start with for day number 1.
-
hello andre
as your suggestion i tried it in my code but again the same thing happen...@while(date.month()==selectedDate.month())
{
int weekDay=date.dayOfWeek();QPushButton* dayButton = new QPushButton(); m_dayButtons.append(dayButton); //button[count][weekDay] = new QPushButton(QString::number(date.day())); controlsLayout->addWidget(dayButton, count, weekDay); date=date.addDays(1); if (weekDay == 7 && date.month() == selectedDate.month()) count++; }@
so is i had made some mistake..please check it
-
In that function you post above, you only set the text for each button*. No widget adding or other such manipulations.
*) ok, and perhaps something else that depends on the actual date...
-
Thanks andre..its working..now i want to ask another thing to you.
Actually after adding widget in the gridlayout if i want to clear the Gridlayout on its next call then how it can be done..means if initially i add label in the grid layout but on next call i want to clear the layout and add textbox to it.so how it can be done..please suggest..
-
Sorry, but no. I am not going to take you by the hand every baby step of the way, certainly not with constantly changing requirements.
There are no labels in the grid layout for the calendar. Just create a single widget class that represents the calendar, and only the calendar. If you need other things around it, use composition to put your calendar into this larger widget. This topic is about building a calendar based on a grid of buttons.
-
thanks Andre..
its all done..i will be in touch with you for further query if you dont mind
rahul
-
[quote author="gogoi" date="1305026252"]thanks Andre..
its all done..i will be in touch with you for further query if you dont mind
rahul
[/quote]
-As long as you don't expect me to actually answer personally, or pay me to do so, I don't mind. Otherwise, I suggest you keep your queries addressed generally to the whole community and out in the open.-Edit:
Strike that. I am not going to answer you again. It seems like you are actually the same user as "this":http://developer.qt.nokia.com/member/9847 and "this":http://developer.qt.nokia.com/member/10020 user. We've been over this before, I'm done with this.This topic has been closed as duplicate