标签: qtextstream

QTextStream行为搜索不符合预期的字符串

我有这几行代码:

QFile file("h:/test.txt");
file.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&file);

bool found = false;
uint pos = 0;

do {
    QString temp = in.readLine();
    int p = temp.indexOf("something");
    if (p < 0) {
        pos += temp.length() + 1;
    } else {
        pos += p;
        found = true;
    }
} while (!found && !in.atEnd());

in.seek(0);
QString text = in.read(pos);
cout << text.toStdString() << endl;
Run Code Online (Sandbox Code Playgroud)

我们的想法是在文本文件中搜索特定的char序列,并在找到时将文件从开头加载到搜索文本的出现位置.我用于测试的输入是:

this is line one, the first line
this is line two, it is second
this is the third …
Run Code Online (Sandbox Code Playgroud)

c++ qt qtextstream

7
推荐指数
1
解决办法
1974
查看次数

查看QTextStream

我想查看读取QFile的QTextStream的下一个字符,以便创建一个有效的标记器.

但是,我没有找到任何令人满意的解决方案.

QFile f("test.txt");
f.open(QIODevice::WriteOnly);
f.write("Hello world\nHello universe\n");
f.close();

f.open(QIODevice::ReadOnly);
QTextStream s(&f);
int i = 0;
while (!s.atEnd()) {
  ++i;
  qDebug() << "Peek" << i << s.device()->peek(3);
  QString v;
  s >> v;
  qDebug() << "Word" << i << v;
}
Run Code Online (Sandbox Code Playgroud)

给出以下输出:

Peek 1 "Hel" # it works only the first time
Word 1 "Hello" 
Peek 2 "" 
Word 2 "world" 
Peek 3 "" 
Word 3 "Hello" 
Peek 4 "" 
Word 4 "universe" 
Peek 5 "" 
Word 5 ""
Run Code Online (Sandbox Code Playgroud)

我尝试了几个实现,也使用了QTextStream :: …

qt seek peek qtextstream

5
推荐指数
1
解决办法
1332
查看次数

在 Qt 中:我可以输出到 `stdout`,就像我可以使用 qDebug() 输出到 `stderr` 一样简单吗?

到目前为止,我使用qDebug().noquote(). 这很容易,因为它只需要一个简单的#import <QDebug>

现在我需要将所有内容输出到stdout,但我不知道如何轻松完成。我是这样被教导的:

QTextStream cout(stdout, QIODevice::WriteOnly);
Run Code Online (Sandbox Code Playgroud)

然而,创建一个新对象比一个简单的#import <QDebug>. stdout在qt中处理的好/最不麻烦的方法是什么?

c++ qt stdout qdebug qtextstream

4
推荐指数
1
解决办法
4432
查看次数

QTextStream stdin readline 不暂停输入

这是一个非常简单的应用程序来说明我遇到的问题。

#include <QTextStream>

int main()
{
    QTextStream cin(stdin);
    QTextStream cout(stdout);

    QString test;

    cout << "Enter a value: ";
    cout.flush();
    cin >> test;

    cout << "Enter another value: ";
    cout.flush();

    test = cin.readLine();
    cout << test;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望执行暂停并等待输入test = cin.readline();,但事实并非如此。如果我删除cin >> test;然后它暂停。

为什么这段代码的行为是这样的,我如何获得我想要的行为?

c++ qt4 qtextstream

1
推荐指数
1
解决办法
2029
查看次数

解析QTextStream

我的问题很简单,但我无法解决这个问题......

在我的标题中:

QTextStream *in = NULL;
Run Code Online (Sandbox Code Playgroud)

在一个方法中,QTextStream被初始化:

in = new QTextStream(&file);
Run Code Online (Sandbox Code Playgroud)

然后我试图用另一种方法解析它:

QString next;

if(in != NULL){
    while(!in->atEnd()){
        next = in->readLine();
    }
}
else{
  QMessageBox::critical(this, "Error", "No file to test!");
}
Run Code Online (Sandbox Code Playgroud)

当初始化精品工程,应用程序崩溃的测试,如果atEnd() .我究竟做错了什么?我需要是可访问的几种方法.我要在这里(?)使用指针,因为后来被初始化(据我所知这是不可能的引用)

这可能是显而易见的,但我对c ++相当新...

谢谢!

c++ qt qtextstream

0
推荐指数
1
解决办法
1024
查看次数

我无法使用QFileDialog,C++ QT保存.txt文件

这是我的代码:

void MainWindow::save(){
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save Text File"), path, tr("Text Files (*.txt)"));
    if (fileName != "")
    {
       QFileInfo info(fileName);
       path = info.absoluteDir().absolutePath();
       QFile file(path);
       if(!file.open(QIODevice::WriteOnly)){
           QString text = ui->plainTextEdit->toPlainText();
           QTextStream out(&file);
           out << text ;
           file.close();
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在单击pushButton后它不会创建任何.txt文件:

connect(ui->saveButton, SIGNAL(clicked(bool)), this, SLOT(save()));
Run Code Online (Sandbox Code Playgroud)

日志:

QIODevice::write (QFile, "C:\Users\kfg\Desktop"): device not open
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助 :)

c++ qt qfiledialog qfile qtextstream

0
推荐指数
1
解决办法
1074
查看次数

标签 统计

qtextstream ×6

c++ ×5

qt ×5

peek ×1

qdebug ×1

qfile ×1

qfiledialog ×1

qt4 ×1

seek ×1

stdout ×1