Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Building My custom Calender [Moved]
Forum Updated to NodeBB v4.3 + New Features

Building My custom Calender [Moved]

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 6.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gogoi
    wrote on last edited by
    #4

    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();
    

    }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #5

      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.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gogoi
        wrote on last edited by
        #6

        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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #7

          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++.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gogoi
            wrote on last edited by
            #8

            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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #9

              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.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gogoi
                wrote on last edited by
                #10

                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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #11

                  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...

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gogoi
                    wrote on last edited by
                    #12

                    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..

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #13

                      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.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gogoi
                        wrote on last edited by
                        #14

                        thanks Andre..

                        its all done..i will be in touch with you for further query if you dont mind

                        rahul

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #15

                          [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

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved