Note that there are some explanatory texts on larger screens.

plurals
  1. POQt can I connect signals/slots to self in constructor?
    text
    copied!<p>EDIT: Not related to signals/slots/connect. Problem was constructor calling constructor.</p> <p>There might be a better way to do this - I'd be interested in hearing those...</p> <p>I have MyClass that is derived from a QLabel. I want to pass more data about the derived class back in the signal than what the base signal does. So I made a slot to intercept the customContextMenuRequested signal and emit a revised one that has more data.</p> <p>When I try to connect up this signal in the constructor, then my slot never gets called. But if I move the Policy and connect lines out to the parent widget(not class hierarchy parent) so they execute after MyClass is fully constructed, then my slot will get called. But I always want that to be connected for this class and it seems like something I would want in it's constructor rather than counting on the parent class to remember to do it.</p> <p>Is there something I'm doing wrong? Or a better way to add data to a signal?</p> <pre><code>MyClass::MyClass() : QLabel() { QFont currFont = font(); currFont.setPointSize(15); setFont(currFont); setBackgroundRole(QPalette::Mid); std::cout &lt;&lt; "connecting customContextMenuRequested" &lt;&lt; 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&amp;)), this, SLOT(addCellDataToMenuContextRequest(const QPoint&amp;))); /* PROBLEM END **/ } MyClass::MyClass(QString &amp;cellString, int row, int col) : QLabel(cellString) { MyClass(); setRow(row); setCol(col); } // This one is a slot void MyClass::addCellDataToMenuContextRequest(const QPoint&amp; 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 &lt;&lt; "called addCellDataToMenuContextRequest" &lt;&lt; std::endl; emit customContextMenuRequestedForCell(pos, _row, _col); } </code></pre> <p>So I would like the parent widget to just look for customContextMenuRequestedForCell but right now, the parent widget seems to need to be responsible for customContextMenuRequested as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload