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

Access UI from another class



  • I am trying to create a class which should access a ui object via a connect function as
    Ui is private but can't get it work:

    I wrote the connect function in the secondary class as follows:
    QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );

    but can't figure out why it is not working.

    AddPlot program is just adding a Widget to a HorizontalLayout


  • Lifetime Qt Champion

    @jipe3001 First of all do this:

    qDebug() << QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );
    

    Does it print true? And can you see any warnings when you start your application?
    Is the SendPlot() signal emitted somewhere?


  • Lifetime Qt Champion

    Hi,

    is MainWindow the name of the class or of an object ?



  • MainWindow is the class of the QMainwindow managed by Ui

    I just need to add A Qwidget to the Horizontal Layout included in MainWindow
    from another class and get out how to access Ui (private) from external class.

    I guess connect is the best way, any help appreciated


  • Lifetime Qt Champion

    You can't connect an object to a class. Both end must be objects.



  • @jipe3001
    because you can not link a class name, you have to link an object or a pointer to the object.
    So, a solution is to pass a pointer (or a reference) to the object who has the type name of your class (MainWindow) to the object who has the type name of the class from where you want to connect SIGNAL/SLOT (if you call the second UI object from inside the MainWindow object, it should be: "this"). Then, just use the pointer or the reference instead of the class name (i think you will probably connect them in the constructor of your class).


Log in to reply