Qasim Qasim - 24 days ago 5
C++ Question

how can work with QFile in QT?

i give one object from the QFile class ...
but wen debuging i see this is not have valuse ...
how i must to do ???

mainscrin.cpp

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

QMainWindow::statusBar()->showMessage("Welcome...",10000);
//ui->tableWidget->setStyle(Macintosh);
double lab22 = ui->tableWidget->StyledPanel;
ui->label_22->setNum(lab22);


QTableWidgetItem *item_1=new QTableWidgetItem;

QString Filename1("bin/bin_1.txt");
QStringList str_item1 = StRead(Filename1);

QString Filename2("bin/bin_2.txt");
QStringList str_item2 = StRead(Filename2);

QString Filename3("bin/bin_3.txt");
QStringList str_item3 = StRead(Filename3);

QString Filename4("bin/bin_4.txt");
QStringList str_item4 = StRead(Filename4);

QString Filename5("bin/bin_5.txt");
QStringList str_item5 = StRead(Filename5);


for(int i=0;i<str_item1.size();i++)
{
item_1->setText(str_item1[i]);
ui->tableWidget->setItem(i+1,1,item_1);
}

for(int i=0;i<str_item2.size();i++)
{
item_1->setText(str_item1[i]);
ui->tableWidget->setItem(i+1,2,item_1);
}

for(int i=0;i<str_item3.size();i++)
{
item_1->setText(str_item1[i]);
ui->tableWidget->setItem(i+1,3,item_1);
}

for(int i=0;i<str_item4.size();i++)
{
item_1->setText(str_item1[i]);
ui->tableWidget->setItem(i+1,4,item_1);
}

for(int i=0;i<str_item5.size();i++)
{
item_1->setText(str_item1[i]);
ui->tableWidget->setItem(i+1,5,item_1);
}
}


mainscrin.h

private:
Ui::MainScrin *ui;
QList<QString> StRead(QString Filename)
{
QFile m_File(Filename);

if(!m_File.open(QFile::ReadWrite | QFile::Text ))
{
QMessageBox message_file_Read;
message_file_Read.warning(0,"Open Error"
,"coude not to open file for Reading");
QList<QString> Erorr;
return Erorr;
}

QList<QString> strings;

QTextStream in(&m_File);
while (!in.atEnd())
{
QString line = in.readLine();
strings.push_back(line);
}

m_File.close();
return strings;
}
};


enter image description here

Answer

I suppose, you working directory has no folder 'bin'. You may use applicationFilePath to translate relative paths to absolute paths.

QString QCoreApplication::applicationFilePath()

And also, please, look at the simple example of QFile usage (QFile):

QFile file("/proc/modules");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    return;

QTextStream in(&file);
QString line = in.readLine();
while (!line.isNull()) {
    process_line(line);
    line = in.readLine();
}