0

i'm beging work with QT and have a problem. I have a project with 4 classes and a mainWindow (dialog). My first problem is reading a txt.file. If i put the code conteined in void fileTxt::setContaRigheFileTxt() into void Dialog::on_buttonBox_clicked(QAbstractButton *Open) it works perfectly. I can open my file, the labels are printed and the content of the file is written in the textEditBox. But as you can see in the following code, in void Dialog::on_buttonBox_clicked(QAbstractButton *Open) i called the setContaRigheFileTxt() funtion and i can't open my file (first error: tr and ui was not declared in this scope)

main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
        QApplication a(argc, argv);
        Dialog w;
        w.show();

        return a.exec();
}

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <fileTxt.h>
#include <string>

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

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

/* APRE FILE ROF .txt DA CONVERTIRE */
void Dialog::on_buttonBox_clicked(QAbstractButton *Open)
{
        fileTxt _fileTxt;
        /* FROM
        _fileTxt.setContaRigheFileTxt();                // apre il file .txt per contare le righe
        TO */
        _fileTxt.setContaRigheFileTxt(ui);                // apre il file .txt per contare le righe
}

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QMainWindow>
#include <QAbstractButton>

namespace Ui {
    class Dialog;
}

class Dialog : public QDialog
{
        Q_OBJECT

    public:
            explicit Dialog(QWidget *parent = 0);
            ~Dialog();

    private slots:
            void on_buttonBox_clicked(QAbstractButton *Open);

    private:
            Ui::Dialog *ui;
};

#endif // DIALOG_H

fileTxt.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include "fileTxt.h"    
#include <QTextStream>
#include <QLabel>
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QObject>

/* COSTRUTTORE - CREA FILE .TXT */
fileTxt::fileTxt()
{

}

/* DISTRUTTORE */
fileTxt::~fileTxt()
{

}

/* CONTARIGHE FILE .TXT */
/* FROM
void fileTxt::setContaRigheFileTxt()
TO */
void fileTxt::setContaRigheFileTxt(Ui::Dialog *ui)
{
        QString fileName = QFileDialog::getOpenFileName(this, tr("Apri File"), "C:\\Users\\Massimo Di Natale\\Documents\\Programmi C++ 11\\Programmi_QT\\Ericsson", tr("File ROF (R*.txt)"));

        if (!fileName.isEmpty())                        // se non è vuoto
        {
            QFile file(fileName);                       // se è vuoto
            if (!file.open(QIODevice::ReadOnly))
            {
                    QMessageBox::critical(this, tr("Errore"), tr("Non posso aprire questo file"));
                    return;
            } // end if

        /* ETICHETTE: CONTROLLO BYZ/CORREZIONE COORDINATE */
        QFont f( "Arial", 12);                          // imposta il font
        ui->label_3->setFont(f);
        ui->label_4->setFont(f);
        ui->label_5->setFont(f);
        ui->label_6->setFont(f);
        ui->label_3->setAlignment(Qt::AlignCenter);     // imposta l'allineamento
        ui->label_4->setAlignment(Qt::AlignCenter);
        ui->label_5->setAlignment(Qt::AlignCenter);
        ui->label_6->setAlignment(Qt::AlignCenter);
        ui->label_3->setText("Controllo che le BYZ richieste siano inserite nel DataBase...");
        ui->label_4->setText("e");
        ui->label_5->setText("correzione coordinate");
        ui->label_6->setText("in corso...");

        QTextStream in(&file);                          // legge il file .txt
        ui->textEdit->setText(in.readAll());            // lo stampa nella textEdit

        file.close();                                   // chiude il file aperto per la lettura
}

fileTxt.h

#ifndef FILETXT_H
#define FILETXT_H
#include <string>

/* ADD */
#include "dialog.h"
#include "ui_dialog.h"

class fileTxt
{
        public:
            fileTxt();
            virtual ~fileTxt();

            /* FROM
            void setContaRigheFileTxt();
            TO */
            void setContaRigheFileTxt(Ui::Dialog *ui);

        protected:

        private:
};

#endif // FILETXT_H
Massimo D. N.
  • 316
  • 1
  • 6
  • 17
  • Unfortunately i'm very new in C++ and QT and also my english is not so good, so the answer in the other post don't help me too (also the code is not complete so its very very difficult for me to understand) – Massimo D. N. Oct 09 '16 at 07:33

1 Answers1

0

If you want to access the Dialog's ui member in that way, pass it as an argument to setContaRigheFileTxt().

Alternatively pass the pointers of the labels and the textedit, or set all the static content (for the labels) inside the on_buttonBox_clicked() slot and let setContaRigheFileTxt() return the string that needs to be set on the textedit.

Change the signature of `setContaRigheFileTxt()? to

void fileTxt::setContaRigheFileTxt(Ui::Dialog *ui)

Copy the forward declaration of that type from dialog.h to fileTxt.h, and the include from dialog.cpp ti fileTxt.cpp

Kevin Krammer
  • 5,159
  • 2
  • 9
  • 22
  • Thanks Kevin but this don't help me so much. I googled how to pass ui member as an argument to another function but nothing. Can you modify the code for me? Thank you very much – Massimo D. N. Oct 08 '16 at 16:58
  • @ Kevin I'm trying to pass ui as member to setContaRigheFileTxt(): 1) in dialog.cpp _fileTxt.setContaRigheFileTxt(ui); 2) in dialog.h in private: Ui::Dialog *ui; is enough? 3) in fileTxt.cpp & fileTxt.h void fileTxt::setContaRigheFileTxt(_ui) what type is _ui? string Qwidget???? – Massimo D. N. Oct 09 '16 at 08:06
  • `ui` is of type `Ui::Dialog*`, see your `dialog.h` – Kevin Krammer Oct 09 '16 at 08:47
  • OK i modifyed the code, see above /* FROM TO */. Thank you. I would like you help me more if is possible. Now the error is no matching function for call QFileDialog::getOpenFileName I understand the problem is 'this' if i put '0' the program launch but nothing append when i press the button to open the file. Also if i comment the open file part, neither the labels are printed..... – Massimo D. N. Oct 09 '16 at 10:07
  • The first argument of `getOpenFileName()` is a QWidget parent. You can't pass `this` because fileTxt is not a widget. You could pass the dialog to the function addtionally to the ui pointer. Not sure what you mean with the last sentence – Kevin Krammer Oct 09 '16 at 11:02
  • with last sentence i mean that also commenting/removing the following part, the rest of the code, reguarding labels is not printed. – Massimo D. N. Oct 09 '16 at 15:29
  • QString fileName = QFileDialog::getOpenFileName(this, tr("Apri File"), "...", tr("File ROF (R*.txt)")); if (!fileName.isEmpty()) // se non è vuoto { QFile file(fileName); // se è vuoto if (!file.open(QIODevice::ReadOnly)) { QMessageBox::critical(this, tr("Errore"), tr("Non posso aprire questo file")); return; } // end if – Massimo D. N. Oct 09 '16 at 15:32