Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to Develop a Calendar Widget.
Qt 6.11 is out! See what's new in the release blog

How to Develop a Calendar Widget.

Scheduled Pinned Locked Moved Mobile and Embedded
119 Posts 11 Posters 136.1k 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.
  • I Offline
    I Offline
    imrrk
    wrote on last edited by
    #55

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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #56

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

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

        You would use the code for showing a new dialog that I gave you earlier. That is: you create the new dialog, set the data on it that you need to do proper editing, exec() it, and read back the new value through a public method on your dialog that you have created for that purpose.

        What I don't understand in the sample that you are currently showing, is why you are re-setting the actual formats, instead of changing or removing the set format for the date that you are editing. I would keep the hotDate and coldDate formats themselves constant, and just change which one you set on which date. That would make more sense, I think.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          imrrk
          wrote on last edited by
          #58

          Hello Volker,here is my code according to your suggestion.
          dialog1.h//declaring signals

          @#ifndef DIALOG1_H
          #define DIALOG1_H

          #include <QDialog>
          #include<QDate>
          #include<QTextFormat>
          namespace Ui {
          class Dialog1;
          }

          class Dialog1 : public QDialog
          {
          Q_OBJECT

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

          private:
          Ui::Dialog1 *ui;

          private slots:
          // void on_radioButton_clicked();
          signals:
          void setclicked();
          void unsetclicked();

          private slots:
          void on_pushButton_2_clicked();
          void on_pushButton_clicked();
          };

          #endif // DIALOG1_H
          @

          dialog1.cpp//emitting the signals
          @
          #include "dialog1.h"
          #include "ui_dialog1.h"
          #include<QCalendarWidget>
          Dialog1::Dialog1(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Dialog1)
          {
          ui->setupUi(this);
          connect(ui->pushButton,SIGNAL(setclicked()),this,SLOT(setcolors()));
          connect(ui->pushButton_2,SIGNAL(unsetclicked()),this,SLOT(unsetcolors()));
          }

          Dialog1::~Dialog1()
          {
          delete ui;
          }

          void Dialog1::on_pushButton_clicked()
          {
          emit setclicked();
          }

          void Dialog1::on_pushButton_2_clicked()
          {
          emit unsetclicked();
          }
          @

          whether I am correct.?

          thanks
          imrrk

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

            now you have to connect the signals with some slots in your first dialog and that it should be.

            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
            • I Offline
              I Offline
              imrrk
              wrote on last edited by
              #60

              i didnt get you gerolf,can u please ellaborate..

              thanks
              imrrk

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

                You are now emitting signals from Dialog1 (you might want to think of a more descriptive name to improve code readability), but you are not handing these signals in slots. To fix that, you are going to need to define two slots in Dialog, and connect the signals to these slots.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  imrrk
                  wrote on last edited by
                  #62

                  hello andre,those slots i have defined in my previous dialog.cpp..ie setcolors() and unsetcolors(); and the above code is of the another dialog from where i am calling these slots.

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

                    So... if you have the slots (again: you did not show us, you just told us now after asking), and you have the signals (and, I hope, you have connected them), what is then the problem?

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      imrrk
                      wrote on last edited by
                      #64

                      hello andre,these were the same slots which i had show earlier in this post..i was connecting those slots with signals in my second dialog,I hope you r getting me..

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

                        OK. Fine, you are re-using the slots you already had. Great, and sensible I suppose. And does that all work properly now?

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          imrrk
                          wrote on last edited by
                          #66

                          Now Andre...its not working

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

                            Is the problem that you don't see the dialog pop up, or that the dates are not cleared when you click one of the buttons on the dialog?

                            Did you:

                            • actually connect the signals with the slots?
                            • check your output while running to see if the connection worked?
                            • put debug statements or breakpoints at the relevant places to track what breaks down exactly?
                            1 Reply Last reply
                            0
                            • I Offline
                              I Offline
                              imrrk
                              wrote on last edited by
                              #68

                              actually dates colors are not cleared,when i click on the buttons..

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

                                [quote author="Andre" date="1302698169"]Did you:

                                • actually connect the signals with the slots?
                                • check your output while running to see if the connection worked?
                                • put debug statements or breakpoints at the relevant places to track what breaks down exactly?
                                  [/quote]
                                1 Reply Last reply
                                0
                                • I Offline
                                  I Offline
                                  imrrk
                                  wrote on last edited by
                                  #70

                                  i will check it out and get back to u..thanks for your help

                                  1 Reply Last reply
                                  0
                                  • I Offline
                                    I Offline
                                    imrrk
                                    wrote on last edited by
                                    #71

                                    hwy volker i created two signals and i emitted them on click of push buttons,now in which dialog i should connect signals and which will be the sender and reciever ,I m not getting..

                                    regards
                                    imrrk

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

                                      The sender of the signal is typically the object, that sends them, the receivedr is the object that implements the slots.

                                      so If you are inside the code of dlg1, which implements the slots and invoke there a dialog dlgh2 which emiuts the signals you do:

                                      @
                                      dlg1::foo()
                                      {
                                      dlg2 dlg;
                                      connect(&dlg, SIGNAL(fooSignal()), this, SLOT(fooSlot()));
                                      dlg.exec();
                                      }
                                      @

                                      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
                                      • I Offline
                                        I Offline
                                        imrrk
                                        wrote on last edited by
                                        #73

                                        hey gerolf in dialog1.cpp ,i have emmitted two signals on click of push buttons and connected them as shown below..I dont know whether i am correct in this,now I have my maindialog1.cpp where i wriiten the slots for setting colors,now I am not getting how shall i make connections with the signals i emmitted in dialog1.cpp and the slots in my maindialog.cpp,which contains the slots..

                                        @#include "dialog1.h"
                                        #include "ui_dialog1.h"
                                        #include<QCalendarWidget>
                                        Dialog1::Dialog1(QWidget *parent) :
                                        QDialog(parent),
                                        ui(new Ui::Dialog1)
                                        {
                                        ui->setupUi(this);
                                        connect(ui->set,SIGNAL(clicked()),this,SIGNAL(setclicked()));
                                        connect(ui->unset,SIGNAL(clicked()),this,SIGNAL(unsetclicked()));
                                        }

                                        Dialog1::~Dialog1()
                                        {
                                        delete ui;
                                        }

                                        void Dialog1::on_set_clicked()
                                        {
                                        emit setclicked();
                                        }

                                        void Dialog1::on_unset_clicked()
                                        {
                                        emit unsetclicked();
                                        }
                                        @
                                        this my dialog.cpp//
                                        @#include "dialog.h"
                                        #include "ui_dialog.h"
                                        #include<QCalendarWidget>
                                        #include<QDate>
                                        #include "dialog1.h"
                                        #include "ui_dialog1.h"
                                        #include<QPainter>
                                        #include <QtGui/QApplication>

                                        Dialog::Dialog(QWidget *parent) :
                                        QDialog(parent),
                                        ui(new Ui::Dialog)
                                        {
                                        ui->setupUi(this);
                                        //Dialog1 a(this);
                                        connect(&Dialog1,SIGNAL(setclicked(),this,SLOT(setcolors())));
                                        connect(&Dialog1,SIGNAL(unsetclicked(),this,SLOT(unsetcolors())));

                                        //   QCalendarWidget *cal=new QCalendarWidget();
                                        

                                        // cal->setDateEditEnabled(1);
                                        // cal->setFirstDayOfWeek(Qt::Wednesday);

                                        // cal->show();
                                        //ui->calendarWidget->setFirstDayOfWeek(Qt::Thursday);

                                        //QTextCharFormat colddate;
                                        //colddate.setBackground(Qt::yellow);

                                        //QTextCharFormat hotdate;
                                        //hotdate.setBackground(Qt::red);

                                        //QTextCharFormat wetdate;
                                        //wetdate.setBackground(Qt::green);

                                        //QTextCharFormat raindate;
                                        //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);
                                        }

                                        Dialog::~Dialog()
                                        {
                                        delete ui;
                                        }

                                        void Dialog::on_calendarWidget_clicked(QDate date)
                                        {
                                        // ui->label->setText(date.toString());
                                        //ui->textBrowser->setText(date.toString());
                                        Dialog1 a(this);
                                        a.show();
                                        a.exec();

                                        // ui->pushButton->setText(QString::number(date.day()));

                                        }

                                        void Dialog::setcolors()
                                        {

                                        safedate.setBackground(Qt::green);
                                        //safedate.BackgroundImageUrl
                                        

                                        unsafedate.setBackground(Qt::yellow);

                                        fertiledate.setBackground(Qt::red);
                                        
                                        
                                        startdate.setBackground(Qt::blue);
                                        
                                        
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,1),startdate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,5),safedate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,8),unsafedate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,9),fertiledate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,12),unsafedate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,14),safedate);
                                        

                                        }

                                        void Dialog::unsetcolors()
                                        {
                                        startdate.clearBackground();
                                        safedate.clearBackground();
                                        unsafedate.clearBackground();
                                        fertiledate.clearBackground();
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,1),startdate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,5),safedate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,8),unsafedate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,9),fertiledate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,12),unsafedate);
                                        ui->calendarWidget->setDateTextFormat(QDate(2011,4,14),safedate);
                                        }@

                                        regards
                                        imrrk

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

                                          In dialog.cpp, you need to make the connections not on lines 14 and 15, but where you actually create the dialog, after line 57. There you have a reference to both the dialogs, so there you make the connection.

                                          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