Retrieve QList<QString> from a event handler
-
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_NAMESPACEclass Widget : public QWidget
{
Q_OBJECTpublic:
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 functionsignals:
void onClicked(); //Sends the list of ingredients when user presses buttonpublic 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_HWidget.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_NAMESPACEclass 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
-
When your
widget
inmyRecipe
is a pointer to your current widget, you could simply write a getter and make that call ingetUserIngredients()
.
Otherwise you could also send aQList<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 yourmyRecipe
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 yourWidget
is for entering your "ingredients only, I would consider making it aQDialog
, create + show when needed and get the dialog's result when you close (accept / reject) it.