Search code examples
c++qtqwidgetqlineedit

How can I access widgets from a customized widget?


How can I access widgets from a customized widget?

For example:

I have a customized widget: enter image description here

Now I have a "user info" form that has a QWidget promoted to "My Custom Widget":

enter image description here

How can I get the text from my custom widget? (e.g. QLineEdit->text())


Solution

  • Based on the answers, I created some getters and setters methods to get and set the values of the fields in the "My Custom Widget".

    In my MyCustomWidget class I created the getters and setters for each field:

    public:
        void setNameLineEdit(QString value);
        QString getNameLineEdit();
    
        void setAddressLineEdit(QString value);
        QString getAddressLineEdit();
    
        void setPhoneLineEdit(QString value);
        QString getPhoneLineEdit();
    

    And then:

    void MyCustomWidget::setNameLineEdit(QString value)
    {
        ui->nameLineEdit->setText(value);
    }
    
    QString MyCustomWidget::getNameLineEdit()
    {
        return ui->nameLineEdit->text();
    }
    
    ...
    

    Now I can access these methods from my UserInfo class:

    ui->myCustomWidget->setNameLineEdit( QString("Paul") );
    

    Thanks a lot for all help.