将QListWidgetItem添加到QListWidget

时间:2015-09-24 19:46:49

标签: c++ qt qlistwidget qt-signals qlistwidgetitem

所以我有一个类SnapshotPanel:public QListWidget,我试图动态添加一个QListWidgetItem,但是当我尝试我得到一个段错误时。我已经验证了我添加项目的代码是正确的,因为我可以在构建我的SnapshotPanel时添加到列表中。但是,当通过信号和插槽调用代码时,我无法添加到面板中,因此可以深入了解我所缺少的内容。

这是构造函数:

<?php
// All Copyrights owned 2015. Ask Owner before Copying Code.

header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=testfile.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");

        try { 

            $db = new PDO('mysql:host=localhost;dbname=test', "root", "");

            $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $db = $db;

        } catch (PDOException $e) {             
            die($e);
        }

        try {

            $query = $db->prepare("SELECT * FROM `test` ORDER BY `event_id` ASC;");
            $query->execute();


            while($row = $query->fetch(PDO::FETCH_ASSOC)) 
                $ret[] = $row;

            foreach ($ret as $key => &$entry) 
                $events[$entry['event_id']][$key] = $entry;

        } catch (PDOException $e) {             
            die($e);
        }

        $event = array('',..........,'Band');

        $tot_num=0;
        foreach($events as $ev)
            {   $event_num=0;
                echo "Time"."\n";
                    foreach($ev as $e) {
                        //Multiple echo Statements
                    }
            echo "\n";
            echo "Event Total: "."\t".$event_num."\n"."\n";
        }

    echo $tot_num;



?>

因此,当通过信号和插槽调用时,我无法使用以下代码:

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);
}

1 个答案:

答案 0 :(得分:0)

我认为应该有效。 &#34;插槽是正常的C ++函数&#34; ,符合the documentation.

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

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