Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to pass an attribut to an another class.



  • Hello,
    I 'am trying to pass an attribut to another class but it doesn't working.
    Anybody's knows the good method ?
    In my mainwindow.cpp i catch a QString path (filepath_var) and i whant to pass it in window dialog in order to make a traitement on QFiles and display a chart.
    please help me.


    Mainwondows*********************************************************************


    void MainWindow::on_pushButton_Load_File_clicked()
    {
    QString Filter = "All files (.) ;; Text Files (*.txt)";
    QString filename = QFileDialog::getOpenFileName(this, "Ouvrir un fichier",QDir::homePath(), Filter);

    QFile File_R(filename);
    if (!File_R.open(QIODevice::ReadOnly | QIODevice::Text))
    
    {QMessageBox::information(this, "Pas de fichier selectionner", "Pas de fichier chargée !");}
    
    //else
    //{QMessageBox::information(this,tr("ouvert"),filename);}
    
    
    QFileInfo fi(filename);
    QString filename_aff = fi.fileName();
    
    QString filepath_var =(fi.filePath());
    
    ui->lineEdit_2->setText(filename_aff);
    

    }



    Graph_az_cons::Graph_az_cons(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Graph_az_cons)
    {
    ui->setupUi(this);

    QString filename = ("C:/Users/Documents/outil/reg.txt");
    QFile File_R(filename);
    if (!File_R(QIODevice::ReadOnly | QIODevice::Text))
    

    {QMessageBox::information(this, "Pas de fichier selectionner", "Pas de fichier chargée !");}

    QTextStream flux(&File_R);
    while(!flux.atEnd())

                    {
    
            QString line = flux.readLine();

  • Qt Champions 2019

    @yoyo There are many ways. If you want to pass it to a dialog then simply add it to dialogs constructor:

    Graph_az_cons::Graph_az_cons(QString fileName, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Graph_az_cons)
    {
    


  • Sorry but i tried, it is not working :

    I put

    Graph_az_cons(QString fileName, QWidget *parent) :

    in my graph_az_cons.cpp and my graph_az_cons.h

    but Qt return to me a error's message : no matching function for call to Graph_az_cons(MainWindow*)'

    Code in my main window to call my Qdialog is

    if (ui->checkBox_az_cons->isChecked())

        {
    
        graph_az_cons = new Graph_az_cons(this);
        graph_az_cons->show();
    
    
        }
    

    I don't known ??



  • @yoyo
    You took @jsulm 's suggestion of changing the dialog's constructor to:

    Graph_az_cons(QString fileName, QWidget *parent) :
    

    So it now accepts a file name string as its first parameter, right?

    So do you not think you need to change your call

    graph_az_cons = new Graph_az_cons(this);
    

    to actually pass whatever the file name is to the constructor from this line? That is the point of the change you are making.



  • Yess ! it's working

    I put in my Mainwindow

    if (ui->checkBox_az_cons->isChecked())

        {
    
        graph_az_cons = new Graph_az_cons(filepath_var,this);
        graph_az_cons->show();
    
        }
    

    in my graph_az_cons.h

    public:
    explicit Graph_az_cons( QString filename, QWidget *parent = nullptr);
    ~Graph_az_cons();

    and in my my graph_az_cons.cpp

    Graph_az_cons::Graph_az_cons(QString filename,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Graph_az_cons)
    {
    ui->setupUi(this);



  • @yoyo please don't forget to mark your post as solved!


Log in to reply