Unsolved How can I access the content of a widget outside the constructor?
-
I want to display the content I wrote in the QLineEdit widget before clicking on the QPushButton with the function ShowMessage(). So how can I access that content outside of the constructor?
This is my code:#include "manualwidget.h" #include <QLabel> #include <QLineEdit> #include <QPushButton> #include <QHBoxLayout> #include <QMessageBox> ManualWidget::ManualWidget(QWidget *parent) : QWidget(parent) { QLabel *label = new QLabel(this); QLineEdit *lineEdit = new QLineEdit(this); QPushButton *pushButton = new QPushButton(this); QHBoxLayout *layout = new QHBoxLayout(); label->setText("Enter text:"); pushButton->setText("Ok"); layout->addWidget(label); layout->addWidget(lineEdit); layout->addWidget(pushButton); setLayout(layout); connect(pushButton,SIGNAL(clicked()),this ,SLOT(showMessage())); connect(lineEdit, SIGNAL(returnPressed()),this, SLOT(showMessage())); } void ManualWidget::showMessage(){ QMessageBox::information(this, "Message", "The text entered in the " "manual widget window is:\n" + m_lineEdit->text()); }
-
@DarknessNacho said in How can I access the content of a widget outside the constructor?:
So how can I access that content outside of the constructor?
QString ManualWidget::text() { return m_lineEdit->text(); }
-
@DarknessNacho Simply define these widgets as members of your class:
class ManualWidget : public QWidget { private: QLabel *label; QLineEdit *lineEdit; QPushButton *pushButton; QHBoxLayout *layout; } ManualWidget::ManualWidget(QWidget *parent) : QWidget(parent) { label = new QLabel(this); lineEdit = new QLineEdit(this); pushButton = new QPushButton(this); layout = new QHBoxLayout(); ... }
Now, all these widgets are accessible everywhere in the class.