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

How to print data on textbrowser in other class?



  • When i click "Registra!" in mainwindow i have to print data ,saved on file, on textBrowser in "persone"
    i tryed like everything but cant do it.
    If i click "Registra!" i print and see myfile and "Non va" on Application Output but ui->textBrowwser_->setText(str) doesnt work
    I hope I was clear

    Those are my files :

    main.cpp
    #include "mainwindow.h"
    #include "persone.h"

    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    Persone p;
    p.show();
    w.show();
    return a.exec();
    }

    mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <vector>
    #include "persone.h"

    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE

    struct persona {
    std::string nickname;
    std::string nome;
    std::string cognome;
    std::string email;
    std::string conferma_email;
    std::string sesso;
    int gg;
    int mm;
    int aaaa;

    persona() {};
    persona(const std::string &ni, const std::string &n, const std::string &cn, const std::string &em,
             const std::string &cem, const std::string &s, const int &g, const int &m, const int &a) :
        nickname(ni), nome(n), cognome(cn), email(em), conferma_email(cem), sesso(s), gg(g), mm(m), aaaa(a) {};
    

    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    private slots:
    void on_pushButton_registra_clicked();
    void on_lineEdit_nickname_textChanged(const QString &arg1);
    void on_lineEdit_email_textChanged(const QString &arg1);
    void on_lineEdit_conferma_email_textChanged(const QString &arg1);

    private:
    Ui::MainWindow *ui;
    friend class persone;
    std::vector<persona> rubrica;
    void updateUI();
    void updateDATE();
    void cambio30();
    };
    #endif // MAINWINDOW_H

    persone.h
    #ifndef PERSONE_H
    #define PERSONE_H

    #include <QWidget>

    QT_BEGIN_NAMESPACE
    namespace Ui { class Persone; }
    QT_END_NAMESPACE

    class Persone : public QWidget {
    Q_OBJECT
    public:
    public:
    Persone(QWidget *parent = nullptr);
    ~Persone();
    public:
    Ui::Persone *ui;
    friend class mainwindow;
    void updatetextpersona();
    private slots:
    void on_pushButton_nome_clicked();
    };

    #endif // PERSONE_H

    mainwwindo.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <fstream>
    #include "persone.h"

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    this->setFixedSize(650,500);

    ui->comboBox_sesso->addItem("Sesso");
    ui->comboBox_sesso->addItem("Maschio");
    ui->comboBox_sesso->addItem("Femmina");
    ui->comboBox_dd->addItem("Giorno");
    ui->comboBox_mm->addItem("Mese");
    ui->comboBox_aaaa->addItem("Anno");
    
    for (int i = 2021; i > 1900; i--) {
        QString ii = QString::number(i);
        ui->comboBox_aaaa->addItem(ii);
    }
    
    for (int i = 12; i > 0; i--) {
        QString ii = QString::number(i);
        ui->comboBox_mm->addItem(ii);
    }
    
    for (int i = 31; i > 0; i--) {
        QString ii = QString::number(i);
        ui->comboBox_dd->addItem(ii);
    }
    

    }

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

    void MainWindow::updateDATE(){
    }

    void MainWindow::cambio30(){
    ui->comboBox_dd->clear();
    for (int i = 30; i > 0; i--) {
    QString ii = QString::number(i);
    ui->comboBox_dd->addItem(ii);
    }
    }

    void MainWindow::updateUI(){
    if(ui->lineEdit_nickname->text().isEmpty() || ui->lineEdit_email->text().isEmpty() ||
    ui->lineEdit_conferma_email->text().isEmpty() ||
    !(ui->lineEdit_email->text() == ui->lineEdit_conferma_email->text()))
    ui->pushButton_registra->setEnabled(false);
    else
    ui->pushButton_registra->setEnabled(true);
    }

    void MainWindow::on_pushButton_registra_clicked()
    {
    persona v;
    v.nickname = ui->lineEdit_nickname->text().toStdString();
    v.nome = ui->lineEdit_nome->text().toStdString();
    v.cognome = ui->lineEdit_cognome->text().toStdString();
    v.email = ui->lineEdit_email->text().toStdString();

    v.sesso = ui->comboBox_sesso->currentText().toUtf8().constData();
    v.gg = ui->comboBox_dd->currentData().toInt();
    v.mm = ui->comboBox_mm->currentData().toInt();
    v.aaaa = ui->comboBox_aaaa->currentData().toInt();
    
    this->rubrica.push_back(v);
    
    std::ofstream of;
    of.open("rubrica.txt");
    
    for(int k=0; k<static_cast<int>(this->rubrica.size()); k++) {
        persona b = this->rubrica[k];
        of << b.nickname << ";" << b.nome << ";" << b.cognome << ";" << b.email << ";" << b.sesso << ";" << b.gg + ";" << b.mm + ";" << b.aaaa << std::endl;
    }
    Persone bv;
    bv.updatetextpersona();
    

    }

    void MainWindow::on_lineEdit_nickname_textChanged(const QString &arg1)
    {
    this->updateUI();
    }

    void MainWindow::on_lineEdit_email_textChanged(const QString &arg1)
    {
    this->updateUI();
    }

    void MainWindow::on_lineEdit_conferma_email_textChanged(const QString &arg1)
    {
    this->updateUI();
    }

    persone.cpp
    #include "persone.h"
    #include "ui_persone.h"
    #include <fstream>
    #include "mainwindow.h"
    #include <QFile>
    #include <QMessageBox>

    Persone::Persone(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Persone)
    {
    ui->setupUi(this);
    this->setFixedSize(650,500);
    }

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

    void Persone::on_pushButton_nome_clicked()
    {
    qDebug("OOO");
    // std::sort();
    }

    void Persone::updatetextpersona(){

    QFile file("D:\\Programming\\Projects\\EsameC++\\QT\\QtEsame\\rubrica.txt");
    if(!file.open(QIODevice::ReadOnly))
        QMessageBox::information(0,"info",file.errorString());
    
    QTextStream in(&file);
    QString str;
    str = in.readAll();
    qDebug() << str;
    ui->textBrowser_->setText(str);
    qDebug() << "Non va";
    

    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please use coding tags (the </> button) when posting code, that makes it readable.

    As for your issue, you are working with local objects that will be destroyed at the en of the function they are declared in.

    You are also working on likely two different files since once you use a full path and the other a relative path.



  • Solved

    Add:
    mainwindow.h

    signals:
        void updatevistapersone();
    
    

    persone.h

    public slots:
        void onupdatevistapersone();
    

    mainwindow.cpp
    Inside costructor

    connect(ui->pushButton_registra, SIGNAL(returPressed),
                    this, SIGNAL(on_pushButton_registra_clicked()));
    

    Inside button

    emit this->updatevistapersone();
    

    persone.cpp

    void Persone::onupdatevistapersone()
    {
        //to do
    }
    

    main.cpp

    QObject::connect(&w, SIGNAL(updatevistapersone()),
                             &p, SLOT(onupdatevistapersone()));
    

Log in to reply