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

C++ Nana How can I access a button on index 0 that is inside a listbox, from a global listbox* pointer?

本文关键字:列表 索引 按钮 访问 娜娜 C++ 全局 指针      更新时间:2023-10-16

我有一个listbox,在第一列它包含一个带有buttontext字段。

我有一个nanalistBox的全局指针:

listbox*                listBox = nullptr;

我从这里检索到这个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_;
};

这是我的thread

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 中(最复杂的功能,这是 Nana 中最复杂的小部件。

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

这是通过将实现虚拟接口listbox::inline_notifier_interface的对象"工厂"附加到列表类别之一的一列来实现的。这是您使用列表框类别功能的地方:

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

这就解释了为什么没有直接的方法来访问内联小部件。也许可以在列表框中实现一个新的接口函数 - 我们将尝试。我似乎有一个私有函数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来反映该更改。

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

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

我知道这不是最佳实践,但它在这里:

创建了一个vectorpointersbuttons

std::vector<button*>    mButtons;

在创建button时,我push_back()vector的引用:

//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());
//lsbox.erase(lsbox.at(pos_));
buttonPressed(btn_, pos_.item);
});
btn_.events().mouse_move([this]
{
//Highlight the item when hovers the button
indicator_->hovered(pos_);
});
mButtons.push_back(&btn_); // <--------- here

现在我可以从外面控制buttons

button* btn = mButtons[index];
if (nullptr != btn)
{
...
}