繁体   English   中英

如何在wxWidgets(C ++代码)中使用wxListCtrl向第二列添加值?

[英]How to add value to second column using wxListCtrl in wxWidgets (C++ code)?

我下面的代码:

int column_width = 100;

long indx1 = alist-> InsertColumn(0,L“用户名”,wxLIST_FORMAT_LEFT,column_width);

long indx2 = alist-> InsertColumn(1,L“ User ID”,wxLIST_FORMAT_LEFT,column_width);

long itemIndex1 = alist-> InsertItem(indx1,L“ John Smith”,-1);

alist-> SetItem(indx1,1,L“ jsmith”);

我希望在第一行中看到两列,其中“用户名”和“用户ID”分别以“ John Smith”和“ jsmith”作为值。 相反,我只在“用户名”列下看到“ John Smith”,而在“用户ID”列下什么都没有。 这是显示结果的快照的链接: http : //drop.io/agtyt6s

谢谢!

这是显示两列的最小示例。 请注意,我在SetItem方法中使用InsertItem方法返回的索引项。

#include <wx/wx.h>

class Frame : public wxFrame
{
public:
  Frame():
    wxFrame(NULL, wxNewId(), _("App"))
  {
    wxBoxSizer * box = new wxBoxSizer(wxVERTICAL);

    wxListCtrl * listCtrl = new wxListCtrl(this, wxNewId(), wxDefaultPosition, wxDefaultSize, wxLC_REPORT);
    listCtrl->InsertColumn(0, _("User Name"));
    listCtrl->InsertColumn(1, _("User ID"));

    long index = listCtrl->InsertItem(0, _("John Smith"));
    listCtrl->SetItem(index, 1, _("jsmith"));

    box->Add(listCtrl, 1, wxEXPAND, 0);
    SetSizer(box);
    box->SetSizeHints(this);
    Show(true);
  }
};

class App : public wxApp
{
  bool OnInit()
  {
    SetTopWindow(new Frame());
  }
};

IMPLEMENT_APP(App);

好的,我终于在wxWidgets论坛贡献者的帮助下解决了它。 这是问题所在:我正在使用

alist1 = new wxListCtrl( m_panel1, wxID_ANY, wxDefaultPosition, wxSize( 300,-1 ), wxLC_ICON|wxLC_REPORT|wxLC_SINGLE_SEL );

作为wxListCtrl的样式。 事实证明,wxLC_ICON和wxLC_REPORT是互斥的,因此您只能拥有一个。 当我删除wxLC_ICON时,一切正常! 再次感谢您的帮助small_duck!

这个例子对我来说是插入列的最佳方法。

#include <wx/wx.h>
#include <wx/listctrl.h>
#include <wx/colour.h>

enum { ID_LISTBOOK = 10};

class MyApp:public wxApp {
bool OnInit()
{
if ( !wxApp::OnInit() )
return false;

wxInitAllImageHandlers();
wxFrame* frame   = new wxFrame(NULL, wxID_ANY,"wxListCtrl Insert Colored Items");
wxListCtrl* listCtrl = new wxListCtrl(frame,ID_LISTBOOK, wxDefaultPosition, wxDefaultSize, wxLC_REPORT);
listCtrl->InsertColumn(0, "Name", wxLIST_FORMAT_LEFT);
listCtrl->InsertColumn(1, "Number", wxLIST_FORMAT_LEFT);

wxListItem* item     = new wxListItem();

item->SetBackgroundColour(*wxRED);
item->SetText(wxT("Programmer"));
item->SetId(0);


listCtrl->InsertItem(0, *item);
listCtrl->InsertItem(1, *item);
listCtrl->InsertItem(2, *item);
listCtrl->InsertItem(3, *item);

listCtrl->SetItem(0,0,wxT("1"), -1);
listCtrl->SetItem(0,1,wxT("1"), -1);
listCtrl->SetItem(1,0,wxT("2"), -1);
listCtrl->SetItem(1,1,wxT("2"), -1);
listCtrl->SetItem(2,0,wxT("3"), -1);
listCtrl->SetItem(2,1,wxT("3"), -1);
listCtrl->SetItem(3,0,wxT("4"), -1);
listCtrl->SetItem(3,1,wxT("4"), -1);

frame->Show(true);
return true;
};
};

IMPLEMENT_APP(MyApp)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM