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

时间:2010-06-02 22:21:31

标签: c++ wxwidgets

我已经关注了代码:

int column_width = 100;

long indx1 = alist-> InsertColumn(0,L“User Name”,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

谢谢!

3 个答案:

答案 0 :(得分:2)

这是一个显示两列的最小样本。请注意,我正在使用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);

答案 1 :(得分:0)

好的,我终于在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!

答案 2 :(得分:0)

这个示例对我来说是最好的方式,可以在列上插入。

#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)