在列表部件的Qt C显示阵列的内容++ [英] Display array content in a List Widget Qt C++
问题描述
我要显示我的一些阵列的一个列表控件(项目型)使用Qt和C ++的内容,我想这一点,但它dosent工作:
i want to display some of the content of my array in a List Widget (item based) with Qt and C++, i tried this, but it dosent work :
QString exemple[2] = 'blablabla'
ui->listWidgetResult->addItem(exemple[2].toStdString().c_str());
谢谢!
推荐答案
这不能工作:
QString example[2] = 'blablabla'
首先,
是字符
值,而不是字符串。其次,你声明了两个将QString数组,但将其分配给一个C字符串。你的意思也许是这样的:
First, '
is for char
values, not for strings. Second, you are declaring an array of two QStrings, but assign it to a C string. What you mean is perhaps this:
QString example[2] = {"blabla", "blabla"};
,你其实可以缩写为:
Which you can actually abbreviate to:
QString example[] = {"blabla", "blabla"};
要数组中的每个字符串添加到您的列表控件,则需要单独添加每个之一。此外,没有必要将其转换为C字符串。 QListWidget ::的addItem()采用将QString:
To add each string of the array to your list widget, you need to add each one individually. Also, there's no need to convert to a C string. QListWidget::addItem() takes QStrings:
for (int i = 0; i < sizeof(example); ++i) {
ui->listWidgetResult->addItem(exemple[i]);
}
或者,如果你有一个支持C最近++编译器 - 11:
Or, if you have a recent compiler that supports C++-11:
for (const auto& str : example) {
ui->listWidgetResult->addItem(str);
}
最后,而不是使用普通的数组来保存你将QString,则应该考虑举行他们在一个QStringList中。使用然后你可以简单地传递全QStringList中为addItems()
。
这篇关于在列表部件的Qt C显示阵列的内容++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!