Reputation: 3061
How can I access widgets from a customized widget?
For example:
Now I have a "user info" form that has a QWidget promoted to "My Custom Widget":
How can I get the text from my custom widget? (e.g. QLineEdit->text()
)
Upvotes: 1
Views: 949
Reputation: 3061
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.
Upvotes: 0
Reputation: 8238
QLineEdit->text()
is syntactically incorrect and won't compile. QLineEdit::text()
will not compile either, because text()
is non-static member and this call does not make sense without a QLineEdit
object.
Back to your question, first you need to access a custom widget itself. It's easy, once you named the widget somehow in the editor:
In this example its name is customWidget
. Thus, in the ui
private member of QDialog
class you'll find a public member named customWidget
, which has type MyCustomWidget
and corresponds to a widget on the picture. Having this, you may access the public members of customWidget
. QDialog
corresponds to your User Info widget class.
For example, you may declare signals (let's call them value1Changed(QString)
, value2Changed(QString)
and so on) in MyCustomWidget
and forward signals from QLineEdit
s to these signals using signal-to-signal connect()
. Then you may connect value1Changed
to any slot of QDialog
or any other object from the scope where customWidget
pointer is visible.
Another way to go is to declare public methods like QString getLine1Content() const
in MyCustomWidget
and access them from QDialog
whenever you want.
These are not the only methods to access members of a custom widget, but the most frequently used ones.
Upvotes: 1
Reputation: 30717
The right way is to create a suitable accessor method in your MyCustomWidget
implementation:
namespace Ui {
class MyCustomWidget;
}
class MyCustomWidget : public QWidget
{
// You may also wish to add WRITE and NOTIFY methods;
// that's left as an exercise for the reader.
Q_PROPERTY(QString name READ name)
const std::unique_ptr<Ui::MyCustomWidget> ui;
public:
explicit MyCustomWidget(QWidget *parent = 0);
~MyCustomWidget();
QString name() const;
};
#include "ui_mycustomwidget.h"
// Constructor and destructor
MyCustomWidget::MyCustomWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyCustomWidget)
{}
MyCustomWidget::~MyCustomWidget() = default;
// Accessor
QString MyCustomWidget::name() const
{
return ui->nameEdit->text();
}
You can then call the MyCustomWidget::name()
method from within methods of UserInfo
in the normal way:
ui->customWidget->name();
You'll want to create similar accessors for address()
and phone()
too, of course.
The hacky way would be to obtain the line-edit by name, using QObject::findChild<QLineEdit>()
. That really breaks encapsulation, and I won't describe that further.
Upvotes: 2