【问题标题】:Setting vector<double> as choice values of wxComboBox将 vector<double> 设置为 wxComboBox 的选择值
【发布时间】:2017-12-18 16:59:23
【问题描述】:

我正在使用 GUI 创建我的第一个 C++ 程序。 我正在使用 Code::Blocks v16.01 和 wxWidgets v3.03。 我发现在 wxComboBox 类的构造函数中,代表选择的类型是 wxArrayString。我曾尝试将向量转换为向量,然后再转换为向量和 wxArrayString,但失败得很惨……

我的问题是如何设置 wxComboBox 对象的默认选择值? 最好我希望它们填充在程序执行期间创建的向量值。

代码如下:

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream> 
using namespace std;

vector <double> vector_double;
vector <string> vector_string;
vector <wxString> vector_wxstring;

void convert_double_to_string(vector<double> &dbl, vector <string> &str)
{
    for (int i = 0; i < dbl.size(); i++)
    {
        ostringstream stream;
        stream << dbl[i];
        str.push_back(stream.str());
    }
}
void convert_string_to_wxString(vector<string> & str, vector <wxString> &wxstr);
{
    for (int i = 0; i < str.size(); i++)
    {
        wxstr.push_back(_(str[i]));
    }
}
void main()
{
    /////////

    // here setting vector_double's values

    //////////

   convert_double_to_string(vector_double, vector_string);
   convert_string_to_wxString(vector<string> vector_string, vector_wxstring);
}

这就是我得到的。但是,字符串到 wxString 的转换不起作用。即使它会,我也不知道如何将它插入到 wxArrayString 中。

【问题讨论】:

  • 您显然需要将每个双精度值转换为字符串,然后将这些字符串放入wxArrayString。您能否发布您的最佳尝试,以便我们了解您在哪里遇到问题?
  • @ChrisW.,请显示一些代码,说明您尝试了什么以及出了什么问题——您遇到了编译器/链接器错误,或者程序没有按照您的预期执行。尽可能精确。
  • 这一行是错误的:convert_string_to_wxString(vector vector_string, vector_wxstring);删除 'vector '
  • 谢谢@ravenspoint,编译成功!我可以使用 ComboBox1->SetValue(vector_wxstring[1]); 设置 wxComboBox 的值但是我仍然不知道如何设置所有其他可能的选择。

标签: c++ wxwidgets


【解决方案1】:

类似的东西:

wxComboBox * cbo = new wxComboBox( ... );
wxArrayString as;
for ( auto& s : vector_string )
  as.Add( s );
cbo->Set( as );

【讨论】:

  • 非常感谢!有用。事实证明,向量 的转换是不必要的。
猜你喜欢
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
相关资源
最近更新 更多