Results 1 to 6 of 6

Thread: Qt 5.11 QMainWindow signal not seen by custom widget

  1. #1
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    76
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Qt 5.11 QMainWindow signal not seen by custom widget

    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
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow();
    7. ~MainWindow();
    8.  
    9. signals:
    10. void signalRowSelected(QVariant rowdata);
    11. ...
    12. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. emit signalRowSelected(QVariant(vals));
    To copy to clipboard, switch view to plain text mode 

    editwidget.hpp
    Qt Code:
    1. EditWidget:public QWdiget
    2. {
    3. ...
    4. private:
    5. QWidget *mw;
    6. }
    To copy to clipboard, switch view to plain text mode 

    editwidget.cpp
    Qt Code:
    1. EditWidget::EditWidget()
    2. {
    3. ...
    4. mw = parent;
    5. connect(mw, SIGNAL(signalRowSelected(QVariant rowdata)), this, SLOT(slotDisplayRow(QVariant rowdata))); // (similar problem when only parent is used)
    6. }
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by ad5xj; 7th November 2018 at 20:16.

  2. #2
    Join Date
    Jul 2012
    Posts
    187
    Thanks
    9
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt 5.11 QMainWindow signal not seen by custom widget

    You#re missing the Q_OBJECT macr in EditWidget?

    You are also not showing your slot definition.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,812
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt 5.11 QMainWindow signal not seen by custom widget

    connect(mw, SIGNAL(signalRowSelected(QVariant rowdata)), this, SLOT(slotDisplayRow(QVariant rowdata)));
    This is incorrect syntax for a connect() statement. It should be:

    Qt Code:
    1. connect(mw, SIGNAL(signalRowSelected(QVariant)), this, SLOT(slotDisplayRow(QVariant)));
    To copy to clipboard, switch view to plain text mode 

    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)?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    76
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.11 QMainWindow signal not seen by custom widget

    Quote Originally Posted by d_stranz View Post
    This is incorrect syntax for a connect() statement. It should be:

    Qt Code:
    1. connect(mw, SIGNAL(signalRowSelected(QVariant)), this, SLOT(slotDisplayRow(QVariant)));
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,812
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt 5.11 QMainWindow signal not seen by custom widget

    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
    Qt Code:
    1. qobject_cast< MainWindow * >( parent )
    To copy to clipboard, switch view to plain text mode 
    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    76
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.11 QMainWindow signal not seen by custom widget

    SOLVED

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

Similar Threads

  1. Replies: 7
    Last Post: 27th January 2012, 08:30
  2. Replies: 2
    Last Post: 29th June 2011, 16:45
  3. Replies: 2
    Last Post: 20th March 2010, 19:22
  4. QMainWindow and custom widget doesn't show
    By Peppy in forum Qt Programming
    Replies: 9
    Last Post: 26th December 2009, 16:09
  5. Custom Widget - clicked() signal
    By ak in forum Newbie
    Replies: 3
    Last Post: 13th November 2006, 09:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.