【问题标题】:Adding text to existing item of QListWidget将文本添加到 QListWidget 的现有项目
【发布时间】:2016-12-20 20:41:01
【问题描述】:

所以我想在我的QListWidget 列表中添加一个额外的文本,代码如下:

for (int i = 0; i < ui->history->count(); i++ )
{
    ui->history->item(i)->text().append(QTime::currentTime().toString());
}

这不起作用。

我已经 qDebugged 列出了所有使用此代码的项目:

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"

显然这个函数会输出项目的所有文本,那为什么我不能在其中附加任何其他字符串呢?

【问题讨论】:

  • 也许你必须先使用addItem("something"),然后再制作append :-? (如果你还没有使用它)

标签: c++ qt qt5 qt5.5


【解决方案1】:

隐式共享可确保文本不会被直接更改。您必须明确设置文本值:

QString txt = ui->history->item(i)->text().append(QTime::currentTime().toString());
ui->history->item(i)->setText (txt);

【讨论】:

    【解决方案2】:

    text() 按值返回文本,而不是按引用返回。修改文字需要使用setText

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2023-03-06
      • 2023-03-15
      • 2015-04-26
      • 2020-12-22
      • 2013-02-12
      • 1970-01-01
      相关资源
      最近更新 更多