如何使用连接成员函数

时间:2016-09-26 02:37:31

标签: qt signals

我想将tabBarDoubleClicked信号与成员函数连接起来,但编译器不停地咆哮着我:

/home/ron/src/kterminal/sessionstack.cpp:79:56: error: invalid use of non-static member function
         this, SessionStack::editTabLabel(session->id()));
                                                        ^

我不确定如何修复它,我的代码如下:

int SessionStack::addSession(Session::SessionType type)
{
    Session* session = new Session(type, this);
    connect(session, SIGNAL(titleChanged(int,QString)), this, SIGNAL(titleChanged(int,QString)));
    connect(session, SIGNAL(terminalManuallyActivated(Terminal*)), this, SLOT(handleManualTerminalActivation(Terminal*)));
    connect(session, SIGNAL(activityDetected(Terminal*)), m_window, SLOT(handleTerminalActivity(Terminal*)));
    connect(session, SIGNAL(silenceDetected(Terminal*)), m_window, SLOT(handleTerminalSilence(Terminal*)));
    connect(session, SIGNAL(destroyed(int)), this, SLOT(cleanup(int)));

    m_sessions.insert(session->id(), session);

    QString tab_label = QString("Shell (") + QString::number(session->id(), 16) + ")";
    addTab(session->widget(), tr(qPrintable(tab_label)));

    emit sessionAdded(session->id());

    raiseSession(session->id());

    connect(this, tabBarDoubleClicked,
        this, SessionStack::editTabLabel(session->id()));

    return session->id();   
}

void SessionStack::editTabLabel(int tabIndex)
{

双击选项卡时如何调用editTabLabel()

EDIT1

在我的标题中,我声明了以下内容: class SessionStack:public QTabWidget {     Q_OBJECT     Q_CLASSINFO(" D-Bus接口"," org.kde.kterminal")

public:
    explicit SessionStack(QWidget* parent, QWidget* window);
    ~SessionStack();

private slots:
    void tabBarDoubleClicked(int index);
    void editTabLabel(int tabIndex);

};

1 个答案:

答案 0 :(得分:3)

  • 发生错误是因为您忘了在
  • 添加SIGNAL和SLOT宏
  

connect(this,tabBarDoubleClicked,this,SessionStack :: editTabLabel(session-> id()));

  • 连接时无法传递session-> id(),只能在发出信号时传递参数。

  • 请注意,您的代码在运行时无论如何都不会连接,因为信号错过了一个参数(int),因此它与editTabLabel插槽匹配。所以你需要修复它......应该是

    connect(this, SINGAL(tabBarDoubleClicked(int), this, SLOT(editTabLabel(int)));
    
  • 如果内置信号“tabBarDoubleClicked”并且您无法更改它以添加参数,那么您可以考虑使用QSignalMapper http://doc.qt.io/qt-5/qsignalmapper.html