调整对象不是小部件类型 GtkBuilder

Posted

技术标签:

【中文标题】调整对象不是小部件类型 GtkBuilder【英文标题】:Adjustment object not a widget type GtkBuilder 【发布时间】:2020-12-01 08:43:24 【问题描述】:

我无法使用我的林间空地文件中的Gtk::Adjustment 小部件。该程序构建并运行时出现以下错误:

(test-glade-spinbutton:227780): gtkmm-CRITICAL **: 13:38:45.769: gtkmm: object `adjustment_width' (type=`gtkmm__GtkAdjustment') (in GtkBuilder file) is not a widget type.

(test-glade-spinbutton:227780): gtkmm-CRITICAL **: 13:38:45.769: gtkmm: Gtk::Builder: widget `adjustment_width' was not found in the GtkBuilder file, or the specified part of it.

** (test-glade-spinbutton:227780): CRITICAL **: 13:38:45.769: Gtk::Builder::get_widget(): dynamic_cast<> failed.

但是,对于这篇文章,我删除了与 Gtk::Adjustment 的交互,因此程序可以实际运行。如果我尝试读取调整小部件的当前值,程序会崩溃。

以下是 C++ 代码:

#include <gtkmm.h>

using Gtk::Adjustment;
using Gtk::Application;
using Gtk::Builder;
using Gtk::Grid;
using Gtk::SpinButton;
using Gtk::Window;

class MyWindow : public Window

    Grid       *main_content;
    Adjustment *adjustment_width;
    SpinButton *width;

public:
    MyWindow()
    
        auto builder = Builder::create_from_file("test-spinbutton.glade");

        builder->get_widget("main_content"    , main_content);
        builder->get_widget("adjustment_width", adjustment_width);
        builder->get_widget("width"           , width);

        add(*main_content);

        present();
    
;

int main()

    auto app = Application::create("domain.reverse.test-spinbutton");

    MyWindow hello;

    return app->run(hello);


和林间空地文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkAdjustment" id="adjustment_height">
    <property name="lower">1</property>
    <property name="upper">2147483647</property>
    <property name="value">1</property>
    <property name="step_increment">1</property>
    <property name="page_increment">10</property>
  </object>
  <object class="GtkAdjustment" id="adjustment_width">
    <property name="lower">1</property>
    <property name="upper">2147483647</property>
    <property name="value">1</property>
    <property name="step_increment">1</property>
    <property name="page_increment">10</property>
  </object>
  <object class="GtkGrid" id="main_content">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <object class="GtkLabel">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_left">24</property>
        <property name="margin_top">6</property>
        <property name="label" translatable="yes">Width</property>
      </object>
      <packing>
        <property name="left_attach">0</property>
        <property name="top_attach">1</property>
      </packing>
    </child>
    <child>
      <object class="GtkLabel">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_left">24</property>
        <property name="margin_top">6</property>
        <property name="label" translatable="yes">Height</property>
      </object>
      <packing>
        <property name="left_attach">0</property>
        <property name="top_attach">2</property>
      </packing>
    </child>
    <child>
      <object class="GtkSpinButton" id="width">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="margin_left">6</property>
        <property name="margin_top">6</property>
        <property name="hexpand">True</property>
        <property name="adjustment">adjustment_width</property>
      </object>
      <packing>
        <property name="left_attach">1</property>
        <property name="top_attach">1</property>
      </packing>
    </child>
    <child>
      <object class="GtkSpinButton" id="height">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="margin_left">6</property>
        <property name="margin_top">6</property>
        <property name="hexpand">True</property>
        <property name="adjustment">adjustment_height</property>
      </object>
      <packing>
        <property name="left_attach">1</property>
        <property name="top_attach">2</property>
      </packing>
    </child>
    <child>
      <object class="GtkLabel">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">Image Size</property>
        <attributes>
          <attribute name="weight" value="bold"/>
        </attributes>
      </object>
      <packing>
        <property name="left_attach">0</property>
        <property name="top_attach">0</property>
      </packing>
    </child>
    <child>
      <object class="GtkToggleButton" id="toggle_ratio_wh">
        <property name="label">gtk-execute</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="margin_left">6</property>
        <property name="margin_top">6</property>
        <property name="use_stock">True</property>
        <property name="always_show_image">True</property>
      </object>
      <packing>
        <property name="left_attach">2</property>
        <property name="top_attach">1</property>
        <property name="height">2</property>
      </packing>
    </child>
    <child>
      <placeholder/>
    </child>
    <child>
      <placeholder/>
    </child>
    <style>
      <class name="dialog-main-content"/>
    </style>
  </object>
</interface>

【问题讨论】:

我能够重现您的问题,很棒的示例代码。 【参考方案1】:

错误表明adjustment_width 不是小部件,就像它的inheritance tree 建议(不像网格或旋转按钮),所以get_widget 将不起作用。您需要使用get_object。例如:

#include <iostream>
#include <gtkmm.h>

using Gtk::Adjustment;
using Gtk::Application;
using Gtk::Builder;
using Gtk::Grid;
using Gtk::SpinButton;
using Gtk::Window;

class MyWindow : public Window

    Grid       *main_content;
    SpinButton *width;
    
    Glib::RefPtr<Adjustment> adjustment_width;

public:
    MyWindow()
    
        auto builder = Builder::create_from_file("test-spinbutton.glade");

        // Widgets:
        builder->get_widget("main_content"    , main_content);
        builder->get_widget("width"           , width);

        // Other objects (non widgets):

        // 1. Get the object from the object three:
        Glib::RefPtr<Glib::Object> adjustmentObject = builder->get_object("adjustment_width");

        // 2. At this point, it is a Glib::Object (which is Adjustment's base type), so we need
        //    to cast:
        adjustment_width = Glib::RefPtr<Adjustment>::cast_dynamic(adjustmentObject);

        // 3. Then we can access it normally:
        std::cout << "The adjustment value is : " << adjustment_width->get_value() << std::endl;

        add(*main_content);

        present();
    
;

int main()

    auto app = Application::create("domain.reverse.test-spinbutton");

    MyWindow hello;

    return app->run(hello);

【讨论】:

下面的所有小部件都以 &lt;object&gt; 标记开头,包括 Gtk::Adjustment... 所以警告消息没有在我的脑海中响起正确的铃声 LOL。谢谢你的回答:) 没问题。你是对的,这是误导。

以上是关于调整对象不是小部件类型 GtkBuilder的主要内容,如果未能解决你的问题,请参考以下文章

Angular Gridster:调整小部件的大小

在 Qtabwidget 中扩展小部件

Qt 在另一个小部件中调整小部件的大小

Azure DevOps - 小部件大小

根据父窗口小部件的宽度和高度放置一个自动调整大小的窗口小部件

将 headerView 添加到小部件上的小部件中的 listView 调整大小 android