Search code examples
c++qtqwidget

Can't access a control in a widget from another widget in Qt


I have this widget in Qt:

"connecttoserver.h"
"connecttoserver.cpp"
"connecttoserver.ui"

and this one:

"mainwindow.h"
"mainwindow.cpp"
"mainwindow.ui"

How can I access the "connectToserver" elements from the "mainwindow" widget?

I've tried doing this:

void MainWindow::on_btnLogin_clicked()
{
    connectToserver ccc;
    ccc.ui.txt_mail_ip.text();
}

But it doesn't work.


Solution

  • It's C++: to access one widget member from another widget, just create a method (or a Qt slot, if you prefer) and invoke it from the other widget. For being accessible, the method (or the slot) mut be public, or otherwise the two classes must be friend.