【问题标题】:taking text from qt line edit and inputting it to .bat从 qt 行编辑中获取文本并将其输入到 .bat
【发布时间】:2018-02-12 09:22:59
【问题描述】:

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

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

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

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

我应该使用ofstream 吗?

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

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

我读了很多书,但我几乎找不到任何关于保存 lineEdits 或 lineEdits 与 .bat 文件交互的信息。

这是我的 ma​​inwindow.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"); }
}

如果我的问题很愚蠢,我真的很抱歉,我找不到任何东西并且绝望,我真的很感谢任何贡献。

【问题讨论】:

  • 显示批处理文件以及您希望对其进行的更改类型可能是一个想法。
  • 批处理用于执行我们开发的应用程序的新版本,并且批处理请求 IP(在我的应用程序中称为“用户名”)和密码长到数据库服务器。我的老板基本上想要一个用于输入这些详细信息的 GUI..
  • 我有点困惑,听起来您所需要的只是能够读取文本并将其写入文件,根据上面的代码判断,您应该具备这样做的技能(或至少是找出如何去做的技能)。那么,是什么让这个任务比看起来更困难?
  • 当你在 ui 中创建一个按钮并“转到插槽”时,上面的代码会自动生成,我唯一做的就是如果语句哈哈不幸的是我不是那么好,我设法弄明白了tho,需要将我的 Qstrings 转换为 std::strings。我在这里找到了答案in the comments of this question

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


【解决方案1】:

我认为这与很多人无关,但这就是我的功能最终的样子,它完全按照我的意愿行事^^

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";

希望对你有帮助^^

【讨论】:

    【解决方案2】:

    如果你使用 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多