what should be the contents in the area of signal and slots of the push button in the Qt, so that after clicking the push button only the text file will open.
void MainWindow::on_pushButton_clicked()
{
....
}
You can use whatever you want in order to open file, like FILE, fstream, QFile ecc. You simply call a class method, but inside that function you can put everything.
You're using Qt, so you can check QFile class of QT.
It can be done like:
void MainWindow::on_pushButton_clicked()
{
QFile file( filename );
if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
QMessageBox::warning(this, tr("Error opening!"), tr("Could not open the file"));
QTextStream stream( &file );
while( !stream.atEnd() )
{
QString lineText;
lineText = stream.readLine(); //Read a line of text
QStringList tokens= lineText.split(" ",QString::SkipEmptyParts); //Take tokens from the line
}
file.close();
}
Related
I'm trying to create a text file by clicking on a button as following code, but I'm not getting.
QString local = "/local/flash/root";
QString name = " ProductionOrder.txt";
void page1000::on_pushButton_3_clicked()
{
QFile file(local+name);
if(!file.open(QFile::WriteOnly|QFile::Text)){
QMessageBox::warning(this,"ERROR","Error open file");
}
QTextStream output(&file);
QString text=ui->plainTextEdit->toPlainText();
output << text;
file.flush();
file.close();
}
what could be wrong with the code?
I am working with Qt 4.8
I don't know what to do
just put "/" at the end of the QString local
Like this: QString local = "/local/flash/root/";
I have a code:
int actualSize = 8;
QFile tableFile("C:\\Users\\Ms\\Documents\\L3\\table"+QString::number(actualSize)+".txt");
QTextStream in(&tableFile);
QString oneLine;
oneLine.append(in.readAll());
if(tableFile.exists())
{
messageLabel->setText(oneLine);
}else
{
messageLabel->setText("Not open");
}
In the C:\Users\Ms\Documents\L3\ folder, I have a "table8.txt" file. But the messageLabel (which is a QLabel) will have a "Not open" text, oneLine is empty, tableFile.exists() is false, and I got a device not open warning/error.
I tried relative path, like
QFile tableFile("table"+QString::number(actualSize)+".txt");
But none of the methods I come up with was good.
You should be able to use / separators for all QFile-related paths. Open the file before you read it and close it when done.
int actualSize = 8;
QFile tableFile("C:/Users/Ms/Documents/L3/table"+QString::number(actualSize)+".txt");
if(tableFile.exists() && tableFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&tableFile);
QString oneLine;
oneLine.append(in.readAll());
messageLabel->setText(oneLine);
tableFile.close();
} else
{
messageLabel->setText("Not open");
}
I am trying to read data from Multiple files in Qt.
This how I am doing it:
void MainWindow::on_pushButton_3_clicked()
{
QString path = "C:/MyDevelopment/readfiles";
QDir dir(path);
QStringList filters;
filters << "*.txt";
foreach ( QString fileName, dir.entryList(filters, QDir::Files) )
{
QFile readFile(fileName);
if(!readFile.open(QIODevice::ReadOnly | QIODevice::Text ) )
{
qDebug("Failed to read file.....");
//return ;
}
QTextStream in(&fileName);
while (!in.atEnd())
{
QString line = in.readLine();
qDebug() << line;
}
}
it is always going in failed to open. what i am doing wrong here??
in mentioned directory all files are .txt files.
I'm trying to search for a string in a text file; my aim is to write it only if it isn't already written inside my text file.
Here's my function (I don't know how to put inside the while loop):
QFile MyFile("text.txt");
MyFile.open(QIODevice::ReadWrite);
QTextStream in (&MyFile);
while(!MyFile.atEnd())
{ //do something to search string inside }
MyFile.close();
How can I do that? From Qt's Help, method "contains" works with const variable only; can I use it to look for my string?
You can do the following:
[..]
QString searchString("the string I am looking for");
[..]
QTextStream in (&MyFile);
QString line;
do {
line = in.readLine();
if (!line.contains(searchString, Qt::CaseSensitive)) {
// do something
}
} while (!line.isNull());
In case of not large file
QFile MyFile("text.txt");
MyFile.open(QIODevice::ReadWrite);
QTextStream in (&MyFile);
const QString content = in.readAll();
if( !content.contains( "String" ) {
//do something
}
MyFile.close();
To not repeat other answers in case of larger files do as vahancho suggested
This question is absolutely a newbie question so I apologize for that. I have a SLOT which pretty much looks like this.
void MainWindow::on_actionSelect_for_hashing_triggered()
{
QFile file(QFileDialog::getOpenFileName (this, tr("Open File"),
"",tr("")));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QByteArray line = file.readAll();
}
Now I want to pass line to my another SLOT which is look like this..
void MainWindow::on_pushButton_clicked()
{
line2 = line; // QByteArray line2 has been assigned globally
qDebug()<<line2;
}
So here I simply want to print line2 which will receive value from line from first SLOT.
How might I do that ?
void MainWindow::on_actionSelect_for_hashing_triggered()
{
QFile file(QFileDialog::getOpenFileName (this, tr("Open File"), "",tr("")));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QByteArray line = file.readAll();
on_pushButton_clicked( line );
}
void MainWindow::on_pushButton_clicked( const QByteArray& line )
{
line2 = line; // QByteArray line2 has been assigned globally
qDebug()<<line2;
}
Just call the method and pass the byte array. If you need an on_pushButton_clicked(), then just overload or provide a default argument.
If you want to be able to connect/disconnect them at runtime, you will have to get on_actionSelect_for_hashing_triggered() to emit something that on_pushButton_clicked(..) can receive.
And I'm going to give the usual speech on not using global variables...