Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Circular dependency error: "MainWindow" does not name a type



  • I create a custom "Widget" and need to reference to "MainWindow" on this custom widget, but I got problem with circular dependency. So how can I make this works:

    mainwindow.h

    #include <QMainWindow>
    #include <QLocalSocket>
    #include <QTcpSocket>
    #include <QAbstractSocket>
    #include <QTableWidgetItem>
    #include <QHostInfo>
    #include <QDateTime>
    #include <QStandardItemModel>
    #include <QNetworkConfigurationManager>
    #include <statusbarwidget.h>
    
    namespace Ui {
    class MainWindow;
    }
    

    statusbarwidget.h

    #include <QWidget>
    #include <QTimer>
    #include <QNetworkConfigurationManager>
    #include <QNetworkReply>
    #include <QNetworkAccessManager>
    #include <mainwindow.h>
    
    namespace Ui {
    class StatusBarWidget;
    }
    

    Why I need that?
    Because in "MainWindow" have a public method to add messages to logger table component.

    Thanks all for reply!


  • Lifetime Qt Champion

    Hi,

    You are including mainwindow.h in statusbarwidget.h and statusbarwidget.h in mainwindow.h, that's your circular dependency.

    In any case, what you are trying to do is bad by design. your public method to add messages should rather be a slot, and you should emit a signal from StatusBarWidget that you will connect in your MainWindow class to the "add messages" slot. That way, StatusBarWidget doesn't need to know anything about MainWindow.



  • This post is deleted!


  • @SGaist I just can't understand how I will connect!
    I litle confused:

    MainWindow.h

    public slots:
        void addToLog(QString message);
    

    StatusBarWidget.h

    signals:
        void addToLog(QString message);
    

    StatusBarWidget.cpp

    connect(this, SIGNAL(addToLog(QString)), MainWindow, SLOT(addToLog(QString)));
    

    But I got error:

    /home/dcbasso/workspace_qt/CronoboxDataCapture/statusbarwidget.cpp:36: error: expected primary-expression before ',' token
         connect(this, SIGNAL(addToLog(QString)), MainWindow, SLOT(addToLog(QString)));
    

    I know that I can't make direct reference (appears to be), but I not sure how to pass the mainwindow refence to the widget!
    I try to cast the parent to Mainwindow, but does not works.



  • Forget it! I pass the wrong parent in widget constructor!
    Know I fix and works great!

    Thank you!


Log in to reply