所以我有一个SnapshotPanel类:public QListWidget,我想动态地添加一个QListWidgetItem,但是当我尝试得到一个segfault时,就可以这样做。我已验证添加项目的代码正确无误,因为在构造SnapshotPanel时可以将其添加到列表中。但是,当通过信号和插槽调用代码时,我无法添加到面板中,因此很了解我所缺少的内容。

这是构造函数:

SnapshotPanel::SnapshotPanel(QWidget *parent):QListWidget(parent)
{

  this->setViewMode(QListWidget::IconMode);
  this->setIconSize(QSize(256,256));
  this->setResizeMode(QListWidget::Adjust);

  QIcon icon("icon.jpeg");
  QListWidgetItem *widget = new QListWidgetItem(icon,"Earth");

  this->addItem(widget);
}


因此,有什么理由让我在通过信号和插槽调用时无法使用以下代码:

{
  QIcon icon("icon.jpeg");
  QListWidgetItem *widget = new QListWidgetItem(icon,"Earth");
  this->addItem(widget);
}

最佳答案

我认为它应该工作。 the documentation.“插槽是正常的C ++函数”

如果使用多个线程,则需要研究连接机制。也许您需要使用排队连接。您将从以下位置更改连接语句:

connect(button, &QPushButton::clicked, this, &MainWidget::on_button_clicked);




connect(button, &QPushButton::clicked, this, &MainWidget::on_button_clicked, Qt::QueuedConnection);


但是请在此处阅读official documentation。一个SO问题(基本上使您回到文档中)is here

关于c++ - 将QListWidgetItem添加到QListWidget,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32769704/

10-11 22:52
查看更多