Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I access the content of a widget outside the constructor?

How can I access the content of a widget outside the constructor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 420 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DarknessNacho
    wrote on last edited by
    #1

    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());
    
    }
    
    Pablo J. RoginaP jsulmJ 2 Replies Last reply
    0
    • D DarknessNacho

      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());
      
      }
      
      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by Pablo J. Rogina
      #2

      @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();
      }
      

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      5
      • D DarknessNacho

        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());
        
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved