向QListWidget的现有项添加文本
Adding text to existing item of QListWidget
所以我想给我的QListWidget
列表添加一个额外的文本,代码如下:
for (int i = 0; i < ui->history->count(); i++ )
{
ui->history->item(i)->text().append(QTime::currentTime().toString());
}
这行不通。
我已经 qDebug
标记了所有列表项的代码:
qDebug() << "item(" << i << ")->text() : " << ui->history->item(i)->text();
之后,我收到了这样的输出:
item( 0 )->text() : "http://www.google.ru/?gfe_rd=cr&ei=cT6wV9PDKI-8zAXjlaCIDw"
item( 1 )->text() : "https://news.google.ru/nwshp?hl=ru&tab=wn"
item( 2 )->text() : "https://news.google.ru/news?pz=1&hl=ru&tab=nn"
item( 3 )->text() : "https://news.google.ru/news?pz=1&hl=ru&tab=nn"
显然这个函数输出item的所有文本,那么为什么我不能在那里附加任何其他字符串呢?
隐式共享确保文本不会被直接更改。您必须显式地设置文本值:
QString txt = ui->history->item(i)->text().append(QTime::currentTime().toString());
ui->history->item(i)->setText (txt);
text()
按值返回文本,而不是按引用返回。您需要使用setText
来修改文本
相关文章:
- 从文本文件在组合框中添加项目
- 定义宏以将前缀 0x 添加到十六进制字符串文本
- 如何调用继承的重载运算符<<并在派生类的输出中添加更多文本?
- 如何从 c++ winrt UWP 应用中的代码将文本添加到富文本块
- c++ 添加具有写入文件错误的文本变量
- 在 MFC 组合框中设置文本,而不将其添加到列表中
- 将时间戳添加到文本文件中的空行中
- 将文本添加到鼠标位置时,请单击“给出分割故障”
- 如何在C++的文本变量中添加变量值
- 使用FreeImage向图像添加文本
- 在OpenCV图像中添加文本标签
- 在每行的开头和结尾添加文本
- CMFCPropertyGridCtrl添加文本
- Qt 5.2 -如何在UI中添加文本链接
- 添加文本在GraphicsView在Qt
- 模态对话框继续添加文本
- 在c++中向Win32编辑框添加文本
- 在分配的内存中添加文本
- 向QListWidget的现有项添加文本
- 可以在FileCommonDialog上添加文本框