c++ Gtkmm 3 ComboBoxText如何禁用选项?

hs1ihplo  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(127)

我已经尝试了几种方法来完成这件事,但我卡住了,我可以访问模型,但我不能访问视图部分来设置选项disabled with set_sensitive我找不到任何例子或文档提到如何做到这一点。我有一个comboboxtext对象在combobox变量内,和id内的id。这是我所做的:

auto model = combobox.get_model();
for (auto iter = model->children().begin();
iter != model->children().end(); ++iter) {
    auto row = *iter;
    std::string value;
    row->get_value(1, value);
    if (value == id) {
        // disable the row
        ???
        break;
    }
}

任何帮助都将不胜感激。
我看了文档,我问chatGTP,失败了坏:D找到的例子,但对于Gtkmm 4和不兼容.

vkc1a9a2

vkc1a9a21#

我找不到访问所选行cellRenderer的方法来做一个敏感的false。基本上,我无法访问ComboBoxText的内部。我可以提取模型和它的某些部分,但没有任何可用的。(版本4在模型中提供get_column_types,但版本3没有)
经过大量的尝试和错误,我给予了,使用了一个普通的组合框。我留下了一个示例代码,这样其他人就可以找到一些有用的东西,而不必浪费五天的时间去研究。有几种方法可以做到这一点,但只是一个例子:

// Create a class or just do it inline up you you.
class ModelColumns : public Gtk::TreeModel::ColumnRecord {

public:

    ModelColumns() {
        add(colId); add(colText); add(colSensitive);
    }

    Gtk::TreeModelColumn<Glib::ustring> colId;
    Gtk::TreeModelColumn<Glib::ustring> colText;
    Gtk::TreeModelColumn<bool>          colSensitive;
};

// Create a recordset
ModelColumns record;

// Define the TreeModel, set some data.
auto model = Gtk::ListStore::create(record);
for (auto c = 0; c < 3; ++c) {
    auto row = *(model->append());
    row[record.colId]    = std::to_string(c + 1);
    row[record.colText] = "Text " + std::to_string(c + 1);
    // This row will track the sensitive and will be linked to the view.
    row[record.colSensitive] = true;
}

// Define renderer
Gtk::CellRendererText* renderer = Gtk::manage(new Gtk::CellRendererText());

// Create a ComboBox with the TreeModel and renderer.
auto comboBox = Gtk::make_managed<Gtk::ComboBox>();
comboBox->set_model(model);

// This is only needed if you are using an ID like I did on the model above, other case you can use the row number from 0.
comboBox->set_id_column(0);

// add the renderer and link the columns with the data
comboBox->pack_start(*renderer, false);

comboBox->add_attribute(*renderer, "text", record.colText);
comboBox->add_attribute(*renderer, "sensitive", record.colSensitive);

// Example of finding the active ID (the hard way) I keeped this code, because I was trying to get the cell from there.
int index = 0;
for (auto iter = model->children().begin(); iter != model->children().end(); ++iter) {
    auto row = *iter;
    if (row[record.colId] == "2") {
        comboBox->set_active(index);
        break;
    }
    index++;
}

// Get the active row (id = "2") and set it to disable.
auto a = comboBox->get_active();
// You will say, "why you don't just did this with the ComboBoxText?",
well... the manual said to avoid messing with the layout, also, as I said, I was unable to extract the record out of the model, so "record" is nonexistent.
a->set_value(record.colSensitive, false);

// Now get the active ID (the ez way) and disable it too.
comboBox->set_active_id("3");
a = comboBox->get_active();
// Setting this to false will set senstive to false because they are linked.
a->set_value(record.colSensitive, false);

希望有人能找到答案,看到它会很酷。谢谢。

相关问题