Qt我可以在构造函数中将信号/插槽连接到self吗?

Ste*_*wan 5 c++ qt signals-slots

编辑:与信号/插槽/连接无关.问题是构造函数调用构造函数.

可能有更好的方法来做到这一点 - 我有兴趣听到那些......

我有一个源自QLabel的MyClass.我希望在信号中传递有关派生类的更多数据,而不是基本信号.所以我创建了一个插槽来拦截customContextMenuRequested信号并发出一个包含更多数据的修改后的信号.

当我尝试在构造函数中连接此信号时,我的插槽永远不会被调用.但是,如果我移动策略并将线连接到父窗口小部件(而不是类层次结构父窗口),以便它们在MyClass完全构造之后执行,那么我的插槽将被调用.但是我总是希望连接这个类,它似乎是我想要它的构造函数,而不是指望父类记住这样做.

有什么我做错了吗?或者更好的方法来向信号添加数据?

MyClass::MyClass() : QLabel()
{
    QFont currFont = font();
    currFont.setPointSize(15);
    setFont(currFont);

    setBackgroundRole(QPalette::Mid);

    std::cout << "connecting customContextMenuRequested" << std::endl;
    /** PROBLEM START */
    setContextMenuPolicy(Qt::CustomContextMenu);

    // Is there anything wrong with connecting from "this" to "this" in a constructor?

    QObject::connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
                     this, SLOT(addCellDataToMenuContextRequest(const QPoint&)));
     /* PROBLEM END **/
}


MyClass::MyClass(QString &cellString, int row, int col)
    : QLabel(cellString)
{
    MyClass();
    setRow(row);
    setCol(col);

}

// This one is a slot
void MyClass::addCellDataToMenuContextRequest(const QPoint& pos)
{
        // This doesn't get printed if I connect in my constructor,
        // but it does print if I do the same connect from a parent widget.
    std::cout << "called addCellDataToMenuContextRequest" << std::endl;

    emit customContextMenuRequestedForCell(pos, _row, _col);
}
Run Code Online (Sandbox Code Playgroud)

所以我希望父窗口小部件只查找customContextMenuRequestedForCell,但是现在,父窗口小部件似乎也需要负责customContextMenuRequested.

Ste*_*Chu 5

实际上,如果您使用的是 C++11,您可以调用(某种程度的)另一个构造函数。它被称为委托构造函数。但我认为这不会使问题消失。您的问题似乎是元对象在connect()被调用时未完全构造。此外,您可能需要迁移到 Qt 5 才能使用 C++11。

解决方法是延迟连接,直到对象完全构造好。您可以以零间隔启动计时器。它将在下一个事件循环处理时触发,这肯定会在您的对象完全构造之后。

然后在您的 timerEvent 中,建立连接并终止计时器。

编辑:没有看到您的编辑。看起来你找到了解决方案。那就忽略这个吧。:)

顺便提一句。你没有调用另一个构造函数。您创建了一个临时的 MyClass 对象。