Passing Data to a Slot



  • Bonjour,

    I want to pass the qlineedit content of the "New Actor" object to the qtreewidget of the "Actors" object
    I want when I click on the button "new actor" a new actor add under the element ACTORS of qtreewidget and has the name I've already entered in qlineedit.

    Can anyone help me?

    Thank you in advance,

    catches:
    0_1522082380372_list actor.PNG
    This is my code :

    //list_actors.h
    #ifndef LIST_ACTORS_H
    #define LIST_ACTORS_H
    
    #include <QWidget>
    #include <QtWidgets>
    
    namespace Ui {
    class List_Actors;
    }
    
    class List_Actors : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit List_Actors(QWidget *parent = 0);
        ~List_Actors();
        QString data1;
        QTreeWidget * tree;
         QTreeWidgetItem * topLevel;
    
        Ui::List_Actors *ui;
    private slots:
          void on_pushButton_clicked();
    public slots:
    void outputData(QString data);
    };
    
    #endif // LIST_ACTORS_H
    

    //list_actors.cpp
    #include "list_actors.h"
    #include "ui_list_actors.h"
    #include "new_actor.h"

    List_Actors::List_Actors(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::List_Actors)
    {
    ui->setupUi(this);
    tree = ui->treeWidget;

    topLevel = new QTreeWidgetItem();
    topLevel->setText(0, "ACTORS");

    QTreeWidgetItem * item = new QTreeWidgetItem();
    item->setText(0,data1);
    

    topLevel->addChild(item);

    tree->addTopLevelItem(topLevel);
    

    }

    List_Actors::~List_Actors()
    {
    delete ui;
    }
    void List_Actors::on_pushButton_clicked()
    {
    New_Actor *na=new New_Actor();
    na->show();
    }
    void List_Actors::outputData(QString data){

    data1=data;
    }

    //new_actor.h
    #ifndef NEW_ACTOR_H
    #define NEW_ACTOR_H
    #include "list_actors.h"
    #include <QWidget>
    
    namespace Ui {
    class New_Actor;
    }
    
    class New_Actor : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit New_Actor(QWidget *parent = 0);
        ~New_Actor();
        Ui::New_Actor *ui;
    
    signals:
    void redirectData(QString data);
    
    public slots:
    void sendData();
    
    
    
    private:
    
        List_Actors *list_actors; // the object to receive and output the data
    
    };
    
    #endif // NEW_ACTOR_H
    

    //new_actor.cpp
    #include "new_actor.h"
    #include "ui_new_actor.h"

    New_Actor::New_Actor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::New_Actor)
    {
    ui->setupUi(this);
    List_Actors *list_actors= new List_Actors();
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(sendData()));
    connect(this, SIGNAL(redirectData(QString)), list_actors, SLOT(outputData(QString)));
    }

    New_Actor::~New_Actor()
    {
    delete ui;
    }
    void New_Actor::sendData()
    {
    emit redirectData(ui->lineEdit->text());
    this->close();
    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should keep List_Actors and New_Actor cleanly separated.

    Instantiate a new New_Actor object in List_Actors and connect it to the correct slot.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.