如何从 gtkmm3 中的日历小部件获取日期?
How to get the date from a calendar widget in gtkmm3?
所以我不明白如何让这个东西工作。
我已经阅读了 Class reference 但只有两个引用 get_date() return void。如何从日历小部件获取日期?
我现在调用方法错误,这导致了错误。
这是我的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;
};
这是我的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";
}
以及错误
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
这解决了问题。我需要传递参数,然后这些变量将存储日历中的值。
unsigned int year, month, day;
calendar.get_date(year, month, day);
专业提示:正确阅读文档!
所以我不明白如何让这个东西工作。
我已经阅读了 Class reference 但只有两个引用 get_date() return void。如何从日历小部件获取日期?
我现在调用方法错误,这导致了错误。
这是我的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;
};
这是我的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";
}
以及错误
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
这解决了问题。我需要传递参数,然后这些变量将存储日历中的值。
unsigned int year, month, day;
calendar.get_date(year, month, day);
专业提示:正确阅读文档!