PDA

View Full Version : Qt 5.11 QMainWindow signal not seen by custom widget



ad5xj
7th November 2018, 18:17
UPDATED:

I have created a custom edit widget that is a promoted widget in the QMainWindow. The problem is that the custom edit widget is unable to connect to a created signal emitted by the app in QMainWindow.

mainwindow.h


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow();
~MainWindow();

signals:
void signalRowSelected(QVariant rowdata);
...
};


mainwindow.cpp


emit signalRowSelected(QVariant(vals));


editwidget.hpp


EditWidget:public QWdiget
{
...
private:
QWidget *mw;
}


editwidget.cpp


EditWidget::EditWidget()
{
...
mw = parent;
connect(mw, SIGNAL(signalRowSelected(QVariant rowdata)), this, SLOT(slotDisplayRow(QVariant rowdata))); // (similar problem when only parent is used)
}


As I mentioned before the editwidget is a promoted QWidget in the QMainwindow form so they are in the same GUI QThread.

The chages above now connects to QMainWindow but still does not know of the signal emitted.

What am I missing here?

tuli
7th November 2018, 21:08
You#re missing the Q_OBJECT macr in EditWidget?

You are also not showing your slot definition.

d_stranz
7th November 2018, 23:26
connect(mw, SIGNAL(signalRowSelected(QVariant rowdata)), this, SLOT(slotDisplayRow(QVariant rowdata)));

This is incorrect syntax for a connect() statement. It should be:


connect(mw, SIGNAL(signalRowSelected(QVariant)), this, SLOT(slotDisplayRow(QVariant)));

If you paid attention to the runtime output from the debugger, you would have seen an error message that the connect() call failed.

And have you actually verified that the "mw" used in the connect call really is a pointer to you QMainWindow instance and not some sub-widget (like a QFrame, for example)?

ad5xj
8th November 2018, 13:28
This is incorrect syntax for a connect() statement. It should be:


connect(mw, SIGNAL(signalRowSelected(QVariant)), this, SLOT(slotDisplayRow(QVariant)));

If you paid attention to the runtime output from the debugger, you would have seen an error message that the connect() call failed.

And have you actually verified that the "mw" used in the connect call really is a pointer to you QMainWindow instance and not some sub-widget (like a QFrame, for example)?

Sorry for the confusion of my post. I will attempt to answer as many questions as possible not just this one.
1) The EntryWidget class does have the Q_OBJECT macro in it. I just did not post it to keep it simple and to the point.
2) The slot is not the problem. It is never called because the connect() never links to it.
3) I was assuming it was understood that EditWidget was a promoted widget in QMainWindow as assigned in QtDesigner. That means the parent of EditWidget is a QMainWindow.
4) When compiled there is only a warning that the signal is not known in QMainWindow even though it is defined under signals: in the header and emitted by QMainWindow. The updated syntax compiles to show QMainwindow as mw but the signal is still not known.

I am considering doing this on the fly in the QMainWindow constructor. But it should work as is. I am just not seeing why it does not work.

d_stranz
8th November 2018, 16:14
QWidget *mw;

Your code defines "mw" as pointer to QWidget. QWidget has no "signalRowSelected" signal, so you'll get the compilation warning. The constructor definition you posted does not contain "QWidget * parent" as an argument, but I'll assume that is due to sloppy cut-and-paste and that it is present in your real code.


I was assuming it was understood that EditWidget was a promoted widget in QMainWindow as assigned in QtDesigner. That means the parent of EditWidget is a QMainWindow.

Maybe only in your assumption. The only real test is to call
qobject_cast< MainWindow * >( parent ) and verify that the pointer returned from the cast is non-NULL -at run time-. The compiler will let you write any syntactically valid code you want, but that doesn't mean it executes as you expect.


It is never called because the connect() never links to it.

Which implies that "mw" may not be pointing to the class you think it is.

ad5xj
12th November 2018, 15:40
SOLVED

Many thanks to m_stranz. As it turns out you are right. And it now works.. Sorry to take so long to update.