如何使用Qt连接boost :: scoped_ptr <qwidget>

时间:2015-09-25 11:28:42

标签: c++ qt boost

我正在修改现有程序。我在课堂上.h文件:

boost::scoped_ptr<QWidget> _widget;

我的.cpp文件中有一个方法:

void FaceRecoWidget::createWidget()
{
    _widget.reset(new QDialog);
    QVBoxLayout* layout(new QVBoxLayout);

    // some stuff...

    // Now I want to add a 'Cancel' button. This I what I do:    
    QPushButton* cancel_button(new QPushButton("Cancel"));
    // but next line won't compile because _widget is not a 'const QObject*'
    connect(cancel_button, SIGNAL(clicked()), _widget, SLOT(reject()));

    layout->addWidget(cancel_button);
    _widget->setLayout(layout);
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

connect接受指针,因此您应该传递存储在boost::scoped_ptr中的原始指针:

connect(cancel_button, SIGNAL(clicked()), _widget.get(), SLOT(reject()));