如何使用boost :: bind和“this”指针?

时间:2013-06-20 19:45:05

标签: c++ boost boost-bind boost-signals

以下代码输出以下错误:

error C2663: 'boost::signal0<R,Combiner,Group,GroupCompare,SlotFunction>::connect' : 2 overloads have no legal conversion for 'this' pointer
        with
        [
            R=void,
            Combiner=boost::last_value<void>,
            Group=int,
            GroupCompare=std::less<int>,
            SlotFunction=boost::function<void (void)>
        ]

以下是代码:

RepositoryEditor::RepositoryEditor(const boost::signal<void ()>& scenarioHasChangedSignal, QWidget* parent)
    : QDialog(parent),
    m_pActionRemove(new QAction(this))
{
    ui.setupUi(this);

    m_pActionRemove->setText("Remove");
    m_pActionRemove->setToolTip("Remove this device");

    connect(m_pActionRemove , SIGNAL(triggered()),this, SLOT(onActionRemoveClicked()));
    connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(onButtonClicked(QAbstractButton *)));
    connect(ui.treeWidgetDevicesList, SIGNAL(itemSelectionChanged()), this, SLOT(onItemSelectionChanged()));
    connect(this, SIGNAL(itemRemovedFromTree(const Events::VCS::PnPDevice &)), RepositoryManager::getInstance().getRepository(), SLOT(removeDevice(const Events::VCS::PnPDevice &)));
    connect(RepositoryManager::getInstance().getRepository(), SIGNAL(deviceAdded(const DeviceItem &)), this, SLOT(addItemInTree(const DeviceItem &)));
    connect(RepositoryManager::getInstance().getRepository(), SIGNAL(deviceUpdated(const DeviceItem &)), this, SLOT(updateItemInTree(const DeviceItem &)));

    scenarioHasChangedSignal.connect(boost::bind(&RepositoryEditor::updateAllRootItem, this));

    this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
    this->setAttribute( Qt::WA_DeleteOnClose, true );

    initTreeView();
}

void RepositoryEditor::updateAllRootItem()
{
    int index = ui.treeWidgetDevicesList->topLevelItemCount();
    for (int i=0;i<index;i++) 
    {
        RootItem rootItem = (ui.treeWidgetDevicesList->topLevelItem(i))->data(0,Qt::UserRole).value<RootItem>();
        (ui.treeWidgetDevicesList->topLevelItem(i))->setText(0,"test");

    }
}

问题是:如何使用带有“this”指针的bind函数?

谢谢

1 个答案:

答案 0 :(得分:1)

它表示scenarioHasChangedSignal是const,而connect是非const成员函数,不能在const对象上调用。将scenarioHasChangedSignal声明为非const boost::signal<void ()>&