Building My custom Calender [Moved]



  • hello friends

    Actually i want to build my custom calender rather than the calender provided in qt..for that i have desined my own calender by using buttons in to represent each cell.now i found very difficult to get the logic of building calender using buttons..because in qt the logic is given but they have use table row and cols in representing each cell..and move each cell..now can anyone give me the idea of how can i start..the thing i want to know are

    1.what are the things i need to calculate first.

    1. how can i know the day of next month that means in which week (ie monday,tuesday...) it fall based on previous month day

    with regards

    Rahul

    Edit: moved to General and Desktop, as it has nothing to do with mobile; Andre



  • The exact same topics were already discussed here:

    Please refer to:
    http://developer.qt.nokia.com/forums/viewthread/5031/
    http://developer.qt.nokia.com/forums/viewthread/5207
    http://developer.qt.nokia.com/forums/viewthread/5363/

    Is this some kind of school assignment?

    I'm closing this thread here, as the other threads are already discussing it.

    If you feel it should be re-openened, please either contact me through email, or use the report button and explain why your case is different and deserves to stay open.



  • Due to email contact of submitter, opened again.

    bq. how can i interact with each buttons that i have used in creating my custom
    calender means i want to say suppose i click one button than it will give
    all the information of that buttons ie in which month,which week ie
    sun,mon,tue..etc is shown in textBrowser.

    You have to connect the button signals with a slot in you container widget (or another QObject derived class). You can connect all buttons to one signal and use the method sender() to get the sending button. Then you have access to the object and can do whatever you need :-)



  • hello Gerolf

    I have one more problem..actually when i click from one month to another than position of each buttons change accordingly but now i see that some extra buttons are automatically added at the end or sometimes at the first..means suppose i chnage month from may to june it works but in month of june after 30 again 27,28,29,30,31 buttons are added..i am sending you the logic that i have used in integrating custom calender u please check it where the correction is to be done..

    @void MainWindow::createLayout()
    {

    QDate date(selectedDate.year(), selectedDate.month(), 1);
    
    count=0;
    pal->setColor(QPalette::ButtonText, Qt::red);
    while(date.month()==selectedDate.month())
    {
               int weekDay=date.dayOfWeek();
               button[count][weekDay] = new QPushButton(QString::number(date.day()));
               controlsLayout->addWidget(button[count][weekDay], count, weekDay);
               date=date.addDays(1);
               if (weekDay == 7 && date.month() == selectedDate.month())
                 count++;
    
    }
    

    }@

    calling of function on each click
    @void MainWindow::monthForward(int month)
    {

    selectedDate = QDate(selectedDate.year(),month, selectedDate.day());
    
    createLayout();
    

    }@



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.