Search code examples
c++gtkmm3

How to get the date from a calendar widget in gtkmm3?


So I don't understand how to get this thing working.

I already read the Class reference but the only two references to get_date() return void. How can I get the date from a calendar widget?.

I now that I'm calling the method wrong and this cause the errors.

This is my Notebook.hpp

#pragma once

#include <gtkmm.h>

class NotebookMain : public Gtk::Window{
    public:
        NotebookMain();
        virtual ~NotebookMain();

    protected:
        // Signal handlers
        void on_notebook_switch_page(Gtk::Widget *page, guint page_num);
        void on_button_add_appointment();

        // Member widgets
        Gtk::Notebook notebookMain;
        Gtk::Label label1, labelTest;
        Gtk::Box boxDatebook, boxCalendar
        Gtk::Calendar calendar;
        Gtk::Button buttonAddAppointment;
};

This is my Notebook.cpp

#include <iostream>
#include "../include/NotebookMain.hpp"

NotebookMain::NotebookMain() : label1("Hello from inside Tab 2!"),
                               labelTest("Hello from inside Appointments"){
    this->set_title("Test");
    this->set_default_size(640, 480);

    // Create Calendar Box
    boxCalendar.set_orientation(Gtk::ORIENTATION_VERTICAL);
    boxCalendar.pack_start(calendar);
    buttonAddAppointment.set_label("Add Appointment");
    boxCalendar.pack_start(buttonAddAppointment);
    // Create Appointments Box
    boxAppointments.set_orientation(Gtk::ORIENTATION_VERTICAL);
    boxAppointments.pack_start(labelTest);
    // Create Datebok Box
    boxDatebook.set_orientation(Gtk::ORIENTATION_HORIZONTAL);
    boxDatebook.pack_start(boxCalendar);
    boxDatebook.pack_end(boxAppointments);

    // Set Notebook
    notebookMain.set_border_width(0);
    // Add Notebook tabs
    notebookMain.append_page(boxDatebook, "Datebook");
    notebookMain.append_page(label1, "Tab 2");

    // Switch tabs
    notebookMain.signal_switch_page().connect(sigc::mem_fun(*this, &NotebookMain::on_notebook_switch_page));
    // Get Date
    buttonAddAppointment.signal_clicked().connect(sigc::mem_fun(*this, &NotebookMain::on_button_add_appointment));

    // Add Main Window (Notebook)
    this->add(notebookMain);
    this->show_all_children();
}

NotebookMain::~NotebookMain(){}

void NotebookMain::on_notebook_switch_page(Gtk::Widget * /* page */, guint page_num){
    std::cout << "Switched to tab with index : " << page_num << std::endl;
}

void NotebookMain::on_button_add_appointment(){
    std::cout << "Button clicked\n";
    std::cout << calendar.get_date() << "\n";
}

And the errors

src/NotebookMain.cpp: In member function ‘void NotebookMain::on_button_add_appointment()’:
src/NotebookMain.cpp:51:23: error: no matching function for call to ‘Gtk::Calendar::get_date()’
   51 |     calendar.get_date();
      |                       ^
In file included from /usr/include/gtkmm-3.0/gtkmm.h:196,
                 from src/../include/NotebookMain.hpp:3,
                 from src/NotebookMain.cpp:3:
/usr/include/gtkmm-3.0/gtkmm/calendar.h:299:8: note: candidate: ‘void Gtk::Calendar::get_date(guint&, guint&, guint&) const’
  299 |   void get_date(guint& year, guint& month, guint& day) const;
      |        ^~~~~~~~
/usr/include/gtkmm-3.0/gtkmm/calendar.h:299:8: note:   candidate expects 3 arguments, 0 provided
/usr/include/gtkmm-3.0/gtkmm/calendar.h:305:8: note: candidate: ‘void Gtk::Calendar::get_date(Glib::Date&) const’
  305 |   void get_date(Glib::Date& date) const;
      |        ^~~~~~~~
/usr/include/gtkmm-3.0/gtkmm/calendar.h:305:8: note:   candidate expects 1 argument, 0 provided
make: *** [makefile:9: NotebookMain.o] Error 1

Solution

  • This solved the problem. I need to pass the arguments, and then those variables will stored the value from the calendar.

    unsigned int year, month, day;
    calendar.get_date(year, month, day);
    

    Pro tip: Read the documentation right!.