从qt行编辑文本并将其输入到.bat

时间:2018-02-12 09:23:00

标签: c++ windows qt batch-file qtwidgets

我有一个小qt界面,旨在要求用户输入他们的用户名和密码。基本上是Qt中的2个输入字段或lineEdit,之后我使用简单的if语句验证它们。

我的目标是,当验证这些时,我希望我的程序在我的批处理文件中输入。

这是让用户输入与我的批次交互的最佳方法吗?更重要的是,我该怎么做呢?

我应该将这些“行编辑”中的文本保存到字符串中,然后在我的.bat文件中输入这些字符串吗?

我应该使用ofstream吗?

如果这种方法很好,我怎么去呢?

如何将lineEdit文字保存在字符串中?

我阅读了很多,但我几乎找不到任何关于保存lineEditlineEdit.bat文件互动的内容。

这是我的 mainwindow.cpp 代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QCoreApplication>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

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

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

void MainWindow::on_pushButton_clicked()
{
  QString username = ui->lineEdit_username->text();
  QString password = ui->lineEdit_password->text();

  if (username == "test" && password == "test") {
      QMessageBox::information(this,"Login", "Username Name and Password is correct"); }
  else {
      QMessageBox::warning(this,"Login","Username and password is incorrect"); }
}

如果我的问题愚蠢,我真的很抱歉,我无法找到任何事情并且绝望,我真的很感激任何贡献。

2 个答案:

答案 0 :(得分:0)

如果使用qt,可以使用Qt中的QFile类与文件交换。

http://doc.qt.io/qt-5/qfile.html

快速示例:

void writeInBatch(const QString &_str)
{
    QFile file("your batch");
    file.open(_str, QIODevice::WriteOnly);
    file.write(_str);        
    file.close();
}

答案 1 :(得分:0)

我认为这与很多人无关,但这是我的功能最终的样子,&amp;它完全符合我的要求^^

void MainWindow::on_pushButton_clicked()
    {
  std::string username = ui->lineEdit_username->text().toStdString();
  std::string password = ui->lineEdit_password->text().toStdString();

  if (username == "test" && password == "test") {

      //QMessageBox::information(this,"Login", "Username Name and Password is correct");

      fstream myoutfile(filePath, ios::in | ios::out | ios::app);

      myoutfile <<""<< username << endl;
      myoutfile <<""<< password << endl;

      }

  else {
      QMessageBox::warning(this,"Login","Username and password is incorrect");
             }
}

必须包含帮助我附加到我的.txt文件的Fstream(我不知道这是否有效,或者它是否多余但是我也有这个  const char * filePath = "C:/Users/name goes here/Downloads/fish.txt";

希望这有助于^^