首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么glibmm没有显示正确的时间间隔?

为什么glibmm没有显示正确的时间间隔?
EN

Stack Overflow用户
提问于 2022-10-28 17:22:58
回答 1查看 18关注 0票数 0

我在接口上得到了非常大的值,在使用Glib::DateTime::difference()时,该函数生成负数

控制器类的初始化程序:

代码语言:javascript
复制
UI_Controller::UI_Controller(Gtk::Builder * refference, Gtk::Application * app)
{
    view_limit_time = new Gtk::Calendar();
    view_limit_time->set_hexpand(true);
    deffine_application (app);
    this->refference = refference;
    refference->get_widget("main_window",this->content_relations);
    widgets = refference->get_objects();
    Glib::ustring widget_names = "";
    //dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,0))
    for (int i=0; i < widgets.size(); i++){
        widget_names = widget_names+dynamic_cast<Gtk::Widget*>(widgets.at(i).get())->get_name()+"\n";
        if (dynamic_cast<Gtk::Buildable*>(widgets.at(i).get())->get_name() == (Glib::ustring) "timer_grid"){
            Gtk::Grid * button_access = dynamic_cast<Gtk::Grid*>(widgets.at(i).get());
            grid_counter = i;
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(0,2))->signal_clicked().connect(sigc::bind<Gtk::Label*>(sigc::mem_fun(*this,&UI_Controller::start_timer),dynamic_cast<Gtk::Label*>(button_access->get_child_at(0,1)),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::stop_timer),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(2,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::restart_timer),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(3,0))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::add_timer),button_access ) );
            button_access->set_size_request(700,100);
            //dynamic_cast<Gtk::ScrolledWindow*>((button_access->get_ancestor (GTK_TYPE_SCROLLED_WINDOW)))->set_min_content_height(300);
        }
        if (dynamic_cast<Gtk::Buildable*>(widgets.at(i).get())->get_name() == (Glib::ustring) "counter_grid"){
            Gtk::Grid * button_access = dynamic_cast<Gtk::Grid*>(widgets.at(i).get());
            button_access->attach(*(dynamic_cast<Gtk::Widget*>(view_limit_time)),0,1,2,1);
            //view_limit_time->signal_day_selected().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::start_counter),(button_access->get_child_at(2,1)),i,(button_access->get_child_at(0,1))) );
            grid_counter = i;
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(0,2))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::start_counter),(button_access->get_child_at(2,1)),i,(button_access->get_child_at(0,1))) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::stop_counter),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(2,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::restart_counter),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(3,0))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::add_counter),button_access ) );
            button_access->set_size_request(700,300);
            //dynamic_cast<Gtk::ScrolledWindow*>((button_access->get_ancestor (GTK_TYPE_SCROLLED_WINDOW)))->set_min_content_height(300);
        }
    }
    app->run();
}

来自控制器的标头:

代码语言:javascript
复制
#ifndef _U_I_CONTROLLER_H_
#define _U_I_CONTROLLER_H_

#include <unistd.h>
#include <gtkmm.h>
#include <unordered_map>
#include <glibmm/datetime.h>
#include <time-keeper.h>
#include <iostream>

#define TIMER_GRID_UI "src/n_time.ui"
#define COUNTER_GRID_UI "src/n_counter.ui"

namespace std{
    template <>
    struct hash<Time_Keeper>
    {
        size_t operator()( Time_Keeper& t) const
        {
            return t.hasheable().hash();
        }
    };
}

class UI_Controller
{
public:
    UI_Controller(Gtk::Builder* refference,Gtk::Application * app);
    void deffine_application(Gtk::Application * app);
    void add_window_to_application(Gtk::Window * window);
protected:

private:
    int grid_counter;
    bool timer_started=false;
    Gtk::Builder * refference;
    Gtk::ApplicationWindow * content_relations;
    Gtk::Application * app;
    Gtk::Calendar * view_limit_time;
    std::vector<Glib::RefPtr<Glib::Object>> widgets;
    std::unordered_map<int,Time_Keeper> bind_time;
    void show_window(Gtk::Window *window);
    void start_timer(Gtk::Label * selected, int position);
    void stop_timer(int i) { (bind_time[i]).stop_timer (); };
    void restart_timer(int i) { (bind_time[i]).reset_timer ();};
    void add_timer(Gtk::Widget * selected);
    bool timeout_timer(Gtk::Label * display,int position);
    bool timeout_counter(Gtk::Label * display,int position, Glib::DateTime when);
    int get_index(Glib::RefPtr<Glib::Object> target);
    void start_counter (Gtk::Widget * selected, int position, Gtk::Widget * set_when);
    void stop_counter (int i){ (bind_time[i]).stop_counter();};
    void restart_counter(int i){ return ;};
    void add_counter(Gtk::Widget * selected);
    /*sigc::connection Glib::SignalTimeout::connect(const sigc::slot<bool()>& slot,
                                    unsigned int interval, int priority = Glib::PRIORITY_DEFAULT);*/
};

#endif // _U_I_CONTROLLER_H_

与柜台有关的可诉职能:

代码语言:javascript
复制
void UI_Controller::start_counter(Gtk::Widget * selected, int position, Gtk::Widget * set_when){
    guint selected_year, selected_month, selected_day;
    if (bind_time.find(position) == bind_time.end() ){
        bind_time [position] = *(new Time_Keeper());
    }
    //Glib::DateTime when = ( Glib::DateTime::create_from_iso8601(( (dynamic_cast<Gtk::Entry*>(set_when))->get_text())) );
    (dynamic_cast<Gtk::Calendar*>(set_when))->get_date(selected_year,selected_month,selected_day);
    Glib::DateTime when = (Glib::DateTime::create_utc(selected_year,selected_month,selected_day,1,1,1));
    //refference->get_widget("timer_display",display);
    //if (!timer_started){
        sigc::slot<bool()> my_slot = sigc::bind(sigc::mem_fun(*this,
                  &UI_Controller::timeout_counter), (dynamic_cast<Gtk::Label*>(selected)), position,when);
        auto conn = Glib::signal_timeout().connect(my_slot, 1000);
        timer_started = true;
    //}
}

计算Time_Keeper类上时间差的函数:

代码语言:javascript
复制
Glib::ustring Time_Keeper::display_counter(Glib::DateTime when){
    counter_active = true;
    Glib::DateTime now = Glib::DateTime::create_now_utc();
    size_t seconds = ( ((size_t)(when.difference(now))) /1000000);
    std::cout << now.get_year() << std::endl;

    size_t hours = (seconds/3600);
    seconds%=3600;
    size_t minutes=seconds/60;
    seconds%=60;
    
    return ((Glib::ustring) ( std::to_string(hours)+":" \
        +std::to_string(minutes)+":" \
        +std::to_string(seconds) ));
}

ui文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkApplicationWindow" id="main_window">
    <property name="can-focus">False</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">13</property>
        <property name="homogeneous">True</property>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="shadow-type">in</property>
            <child>
              <object class="GtkViewport">
                <property name="visible">True</property>
                <property name="can-focus">False</property>
                <child>
                  <object class="GtkBox">
                    <property name="visible">True</property>
                    <property name="can-focus">False</property>
                    <property name="orientation">vertical</property>
                    <child>
                      <object class="GtkLabel">
                        <property name="visible">True</property>
                        <property name="can-focus">False</property>
                        <property name="label" translatable="yes">current activity time</property>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <!-- n-columns=4 n-rows=3 -->
                      <object class="GtkGrid" id="timer_grid">
                        <property name="visible">True</property>
                        <property name="can-focus">False</property>
                        <property name="hexpand">True</property>
                        <property name="vexpand">True</property>
                        <child>
                          <object class="GtkEntry">
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="hexpand">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">0</property>
                            <property name="width">3</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton" id="start_timer">
                            <property name="label" translatable="yes">Start</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton" id="stop_timer">
                            <property name="label" translatable="yes">Stop</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">1</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton" id="restart_timer">
                            <property name="label" translatable="yes">Restart</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">2</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton" id="add_timer">
                            <property name="label" translatable="yes">+</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">3</property>
                            <property name="top-attach">0</property>
                            <property name="height">3</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkLabel" id="timer_display">
                            <property name="visible">True</property>
                            <property name="can-focus">False</property>
                            <property name="hexpand">True</property>
                            <property name="vexpand">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">1</property>
                            <property name="width">3</property>
                          </packing>
                        </child>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                      </packing>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="shadow-type">in</property>
            <child>
              <object class="GtkViewport">
                <property name="visible">True</property>
                <property name="can-focus">False</property>
                <child>
                  <object class="GtkBox">
                    <property name="visible">True</property>
                    <property name="can-focus">False</property>
                    <property name="orientation">vertical</property>
                    <child>
                      <object class="GtkLabel">
                        <property name="visible">True</property>
                        <property name="can-focus">False</property>
                        <property name="label" translatable="yes">activity limit</property>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <!-- n-columns=4 n-rows=3 -->
                      <object class="GtkGrid" id="counter_grid">
                        <property name="visible">True</property>
                        <property name="can-focus">False</property>
                        <property name="hexpand">True</property>
                        <property name="vexpand">True</property>
                        <child>
                          <object class="GtkEntry">
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="hexpand">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">0</property>
                            <property name="width">3</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton">
                            <property name="label" translatable="yes">Start</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton">
                            <property name="label" translatable="yes">Stop</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">1</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton">
                            <property name="label" translatable="yes">Restart</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">2</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton">
                            <property name="label" translatable="yes">+</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">3</property>
                            <property name="top-attach">0</property>
                            <property name="height">3</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkLabel">
                            <property name="visible">True</property>
                            <property name="can-focus">False</property>
                            <property name="hexpand">True</property>
                            <property name="vexpand">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">2</property>
                            <property name="top-attach">1</property>
                          </packing>
                        </child>
                        <child>
                          <placeholder/>
                        </child>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                      </packing>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
EN

回答 1

Stack Overflow用户

发布于 2022-10-31 13:49:05

在行(dynamic_cast<Gtk::Calendar*>(set_when))->get_date(selected_year,selected_month,selected_day);上,月份被设置为1单位,这使时间差的计算变得混乱。当输出函数get_day_of_year与Glib::DateTime对象"now“和" when”的差异时,我发现它显示了很高的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74238857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档