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. Retrieve QList<QString> from a event handler
Qt 6.11 is out! See what's new in the release blog

Retrieve QList<QString> from a event handler

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 263 Views 2 Watching
  • 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.
  • M Offline
    M Offline
    mrflores
    wrote on last edited by
    #1

    Hey guys,

    Im working on a project where I want a user to enter ingredients into a lineEdit widget and when they press the enter key, it creates a list of ingredients in a textEdit widget (I have this working). Right now those ingredients are being stored in a QList and I am having trouble sending those strings to another class in my code. Im not too versed in the signals and slots functionalities. So how can I send these strings in my Widget class once the user presses the button widget I have in my UI, to the getUserIngredients method in the myRecipe class?

    Widget.h

    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QWidget>
    #include <QLabel>
    #include <QPushButton>
    #include <QDebug>
    #include <QLabel>
    #include <QLineEdit>
    #include <QTextEdit>

    QT_BEGIN_NAMESPACE
    namespace Ui { class Widget; }
    QT_END_NAMESPACE

    class Widget : public QWidget
    {
    Q_OBJECT

    public:
    explicit Widget(QWidget *parent = nullptr); //Constructor to make API Calls
    ~Widget();
    QSet<QString> mySet; //Used in returnPressed function
    QList<QString> myList; //Used in returnPressed function
    QObject myWidget;
    int n; //Counter for updating returnPressed function

    signals:
    void onClicked(); //Sends the list of ingredients when user presses button

    public slots:

    private slots:
    void on_lineEdit_returnPressed(); //Part of GUI that allows for user to enter ingredients and create a list
    void returnPressed();

    private:
    Ui::Widget *ui;

    };
    #endif // WIDGET_H

    Widget.cpp:

    #include "widget.h"
    #include "ui_widget.h"
    #include "myrecipe.h"

    Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    {
    ui->setupUi(this);
    n=0; //Counter for on_lineEdit_returnPressed()

    QObject::connect(ui->pushButton, &QPushButton::clicked, this, &Widget::onClicked);
    

    }

    Widget::~Widget()
    {
    delete ui;
    }

    void Widget::on_lineEdit_returnPressed()
    {
    mySet.insert(ui->lineEdit->text());
    n = n + 1;
    myList = mySet.values();
    ui->textEdit->clear();
    for(int i = 0; i<n; i++)
    {
    ui->textEdit->append(myList[i]);
    }
    ui->lineEdit->clear();
    }

    void Widget::onClicked()
    {

    }

    myRecipe.h

    #ifndef MYRECIPE_H
    #define MYRECIPE_H

    #include <QObject>
    #include <QWidget>
    #include "ui_widget.h"
    #include "widget.h"

    QT_BEGIN_NAMESPACE
    namespace Ui { class myRecipe; }
    QT_END_NAMESPACE

    class myRecipe : public QWidget
    {
    Q_OBJECT
    public:
    explicit myRecipe(QWidget *parent=nullptr);
    ~myRecipe();
    Widget widgets;

    void getUserIngredients();  //Gets the users ingredients they entered in the GUI
    

    signals:

    private slots:
    //void lineEdit_returnPressed();

    private:
    Ui::myRecipe *ui;

    };

    #endif // MYRECIPE_H

    Pl45m4P 1 Reply Last reply
    0
    • M mrflores

      Hey guys,

      Im working on a project where I want a user to enter ingredients into a lineEdit widget and when they press the enter key, it creates a list of ingredients in a textEdit widget (I have this working). Right now those ingredients are being stored in a QList and I am having trouble sending those strings to another class in my code. Im not too versed in the signals and slots functionalities. So how can I send these strings in my Widget class once the user presses the button widget I have in my UI, to the getUserIngredients method in the myRecipe class?

      Widget.h

      #ifndef WIDGET_H
      #define WIDGET_H

      #include <QWidget>
      #include <QLabel>
      #include <QPushButton>
      #include <QDebug>
      #include <QLabel>
      #include <QLineEdit>
      #include <QTextEdit>

      QT_BEGIN_NAMESPACE
      namespace Ui { class Widget; }
      QT_END_NAMESPACE

      class Widget : public QWidget
      {
      Q_OBJECT

      public:
      explicit Widget(QWidget *parent = nullptr); //Constructor to make API Calls
      ~Widget();
      QSet<QString> mySet; //Used in returnPressed function
      QList<QString> myList; //Used in returnPressed function
      QObject myWidget;
      int n; //Counter for updating returnPressed function

      signals:
      void onClicked(); //Sends the list of ingredients when user presses button

      public slots:

      private slots:
      void on_lineEdit_returnPressed(); //Part of GUI that allows for user to enter ingredients and create a list
      void returnPressed();

      private:
      Ui::Widget *ui;

      };
      #endif // WIDGET_H

      Widget.cpp:

      #include "widget.h"
      #include "ui_widget.h"
      #include "myrecipe.h"

      Widget::Widget(QWidget *parent)
      : QWidget(parent)
      , ui(new Ui::Widget)
      {
      ui->setupUi(this);
      n=0; //Counter for on_lineEdit_returnPressed()

      QObject::connect(ui->pushButton, &QPushButton::clicked, this, &Widget::onClicked);
      

      }

      Widget::~Widget()
      {
      delete ui;
      }

      void Widget::on_lineEdit_returnPressed()
      {
      mySet.insert(ui->lineEdit->text());
      n = n + 1;
      myList = mySet.values();
      ui->textEdit->clear();
      for(int i = 0; i<n; i++)
      {
      ui->textEdit->append(myList[i]);
      }
      ui->lineEdit->clear();
      }

      void Widget::onClicked()
      {

      }

      myRecipe.h

      #ifndef MYRECIPE_H
      #define MYRECIPE_H

      #include <QObject>
      #include <QWidget>
      #include "ui_widget.h"
      #include "widget.h"

      QT_BEGIN_NAMESPACE
      namespace Ui { class myRecipe; }
      QT_END_NAMESPACE

      class myRecipe : public QWidget
      {
      Q_OBJECT
      public:
      explicit myRecipe(QWidget *parent=nullptr);
      ~myRecipe();
      Widget widgets;

      void getUserIngredients();  //Gets the users ingredients they entered in the GUI
      

      signals:

      private slots:
      //void lineEdit_returnPressed();

      private:
      Ui::myRecipe *ui;

      };

      #endif // MYRECIPE_H

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @mrflores

      When your widget in myRecipe is a pointer to your current widget, you could simply write a getter and make that call in getUserIngredients().
      Otherwise you could also send a QList<QString> using Signals & Slots.
      For example like this:

      connect(widget, &Widget::sendList, this, &MyRecipe::handleUserIngredients);
      

      To make this work, you need a

      signals:
      void sendList(QList<QString>);
      

      in your widget class and a slot or function which accepts a list in your myRecipe class.

      void handleUserIngredients(QList<QString>);
      

      then you only need to emit the signal at the right place (whenever you want to send the list).
      The getter approach should also work well.

      Edit:
      If your Widget is for entering your "ingredients only, I would consider making it a QDialog, create + show when needed and get the dialog's result when you close (accept / reject) it.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mrflores
        wrote on last edited by
        #3

        Thanks for the feedback. Ill try your suggestions!

        1 Reply Last reply
        0

        • Login

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