C++ Nana 如何从全局列表框 * 指针访问列表框内索引 0 上的按钮?

标签 c++ button listbox nana

我有一个列表框,它的第一列包含一个带有按钮文本字段。

我有一个指向 nana listBox 的全局指针:

listbox*                listBox = nullptr;

我从 here 中检索了这个 class :

#include <nana/gui.hpp>
#include <nana/gui/widgets/label.hpp>
#include <nana/gui/widgets/button.hpp>
#include <nana/gui/widgets/listbox.hpp>
#include <nana/gui/widgets/textbox.hpp>

#include <nana/gui/wvl.hpp> 
#include <nana/system/platform.hpp>

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <thread>
#include <Windows.h>

using namespace nana;

//Creates a textbox and button
//textbox shows the value of the sub item
//button is used to delete the item.
class inline_widget : public listbox::inline_notifier_interface
{
private:
    //Creates inline widget
    //listbox calls this method to create the widget
    //The position and size of widget can be ignored in this process
    virtual void create(window wd) override
    {
        //Create listbox
        txt_.create(wd);
        txt_.events().click([this]
        {
            //Select the item when clicks the textbox
            indicator_->selected(pos_);
        });

        txt_.events().mouse_move([this]
        {
            //Highlight the item when hovers the textbox
            indicator_->hovered(pos_);
        });

        txt_.events().key_char([this](const arg_keyboard& arg)
        {
            if (arg.key == keyboard::enter)
            {
                //Modify the item when enter is pressed
                arg.ignore = true;
                indicator_->modify(pos_, txt_.caption());
            }
        });
        //Or modify the item when typing
        txt_.events().text_changed([this]()
        {
            indicator_->modify(pos_, txt_.caption());
        });
        //Create button
        btn_.create(wd);
        btn_.caption("Start");
        btn_.enabled(false);
        btn_.events().click([this]
        {
            //Delete the item when button is clicked
            auto & lsbox = dynamic_cast<listbox&>(indicator_->host());
            //buttonPressed(btn_, pos_.item);
        });

        btn_.events().mouse_move([this]
        {
            //Highlight the item when hovers the button
            indicator_->hovered(pos_);
        });
    }

    //Activates the inline widget, bound to a certain item of the listbox
    //The inline_indicator is an object to operate the listbox item,
    //pos is an index object refers to the item of listbox
    virtual void activate(inline_indicator& ind, index_type pos)
    {
        indicator_ = &ind;
        pos_ = pos;
    }

    void notify_status(status_type status, bool status_on) override
    {
        //Sets focus for the textbox when the item is selected
        if ((status_type::selecting == status) && status_on)
            txt_.focus();
    }

    //Sets the inline widget size
    //dimension represents the max size can be set
    //The coordinate of inline widget is a logical coordinate to the sub item of listbox
    void resize(const size& dimension) override
    {
        auto sz = dimension;
        sz.width -= 50;
        txt_.size(sz);

        rectangle r(sz.width + 5, 0, 45, sz.height);
        btn_.move(r);
    }

    //Sets the value of inline widget with the value of the sub item
    virtual void set(const value_type& value)
    {
        //Avoid emitting text_changed to set caption again, otherwise it
        //causes infinite recursion.
        if (txt_.caption() != value)
            txt_.caption(value);
    }

    //Determines whether to draw the value of sub item
    //e.g, when the inline widgets covers the whole background of the sub item,
    //it should return false to avoid listbox useless drawing
    bool whether_to_draw() const override
    {
        return false;
    }

private:
    inline_indicator * indicator_{ nullptr };
    index_type pos_;
    textbox txt_;
    button btn_;
};

这是我的线程:

void theThread()
{
    while (1)
    {
        //How can I access a button on index 0 from my listbox?
        button* mybutton = listBox.at(0). ... ? -->HOW CAN I DO THIS?
        mybutton->caption("I did it!!");
        Sleep(1000);
    }
}

还有我的 main() 函数:

void main()
{
    using namespace nana;
    form fm(nana::API::make_center(750, 500), appear::decorate<appear::taskbar>());


    listbox lsbox(fm, rectangle{ 10, 50, 700, 200 });

    listBox = &lsbox;

    //Create columns
    lsbox.append_header("Col1");
    lsbox.append_header("Col2");
    lsbox.append_header("Col3");
    lsbox.append_header("Col4");
    lsbox.append_header("Col5");

    lsbox.at(0).append({ "text1", "text2", "text3", "text4", "text5" });
    lsbox.at(0).append({ "text1", "text2", "text3", "text4", "text5" });
    lsbox.at(0).append({ "text1", "text2", "text3", "text4", "text5" });

    lsbox.column_at(0).width(200);

    lsbox.at(0).inline_factory(0, pat::make_factory<inline_widget>());

    fm.show();


    std::thread myThread(theThread);

    exec();

    myThread.join();

}

问题:

如何从我的全局 listbox* 访问 listbox 中索引 0 上的 button > 指针?

最佳答案

您正在尝试复杂地使用 nana::listbox 的最复杂的功能(或在 Wiki 中),娜娜中最复杂的小部件。

据我了解,您不会直接将内联小部件“附加”到列表中的每个项目。相反,列表本身会生成有限数量的小部件,并且在每时每刻都将这些小部件中的一个“内联”到每个且仅对每个可见项目。为了做到这一点,列表需要知道如何创建小部件以及如何为每个项目“更新”它,以及小部件如何响应不同的可能事件,以及如何更新相应的项目。

这是通过将实现虚拟接口(interface)的对象“工厂”附加到列表类别之一的列来实现的 listbox::inline_notifier_interface .这是你使用 the function of the listbox category 的地方:

void nana::listbox::cat_proxy::inline_factory (size_type column,                    
               pat::cloneable<pat::abstract_factory<inline_notifier_interface>> factory);

这解释了为什么没有直接的方法来访问内联小部件。也许可以为此在列表框中实现一个新的接口(interface)函数 - 我们会尝试。我似乎有一个私有(private)函数 inline_pane * _m_get_inline_pane(const category_t& cat, std::size_t column_pos) const返回 inline_pane依次包含 std::unique_ptr<inline_notifier_interface> inline_ptr;那个可以测试是否为空,哪个可以用来引用您的按钮。 到目前为止,影响内联小部件的预期方法是更改​​相应项目的内容并使用 inline_notifier_interface以反射(reflect)这种变化。

至于在创建时将对按钮的引用保存到全局 vector 中的解决方法,您可能更喜欢全局 vector inline_widget因为它包含一个 index_type pos_;您可以使用它来搜索该 vector 以查找当前链接到感兴趣项目的元素。当然,您将很难确保 vector 指向事件对象。

您可能需要注意,访问 item_proxy 的方式是通过使用 lsbox.at(0).at(item_number) , 或 lsbox.at(index_pair(0,item_number)) .

关于C++ Nana 如何从全局列表框 * 指针访问列表框内索引 0 上的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45211441/

相关文章:

c++ - 有没有一种方法可以机械地识别哪些操作对移出的对象是安全的?

c++ - 随机数生成器 C++ 动态数组

html - Twitter-Bootstrap 按钮在点击后保持悬停状态

java - 小程序按钮填充区

选择组合框时vb.net列表框闪烁

C++ C4716 初学者错误。了解错误概念但不确定如何使程序运行

c++ - 哪种数据结构最适合实现字典?

asp.net - asp.net按钮和超链接设计的区别

c# - 检测是否在列表框中选择了一个项目(WPF)

WPF MVVM ListBox 不显示最后一条记录