0

I've got a little qt interface designed to ask the user to input their Username and Password. Basically 2 input fields or lineEdits in Qt after which I verify them using a simple if statement.

My goal is that when these are verified, I would like my program to enter those in my batch file.

Is this an optimal approach for having user input interact with my batch? and more importantly, how do I go about it ?

Should I save the text from these "line edits" to strings and then input these strings in my .bat file?

Should I use ofstream for that?

If that approach is good, how do I even go about it?

How can I save my lineEdit text in strings?

I read a lot but I could barely find anything about saving lineEdits or lineEdits interacting with .bat files.

Here is my mainwindow.cpp code:

#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"); }
}

I'm really sorry if my question is dumb, I just cant find anything and desperate, I'd really appreciate any contribution.

AAEM
  • 1,837
  • 2
  • 18
  • 26
skdadle
  • 155
  • 1
  • 2
  • 17
  • 1
    Might be an idea to show the batch file as well, and the type of changes you expect to make to it. – john Feb 12 '18 at 09:31
  • the batch is used to execute a new release of an app we developed and the batch requests IP (in my app its' called "username") and password to long into the Database server. my boss basically wants to have a GUI for entering these details.. – skdadle Feb 12 '18 at 09:49
  • i'm a bit perplexed, it sounds like all you need is to be able to read and write text to a file, judging by the code above you should have the skills to do that (or at the very least the skills to find out how to do it). So what gives, in what way is this task more difficult than it seems? – john Feb 12 '18 at 13:23
  • the code above gets automatically generated when u create a button and "go to slot" in ui, only thing i did was If statements haha unfortunately i'm not that good, I managed to figure it out tho, needed to convert my Qstrings to std::strings. i found the answer here [in the comments of this question](https://stackoverflow.com/questions/25953927/saving-the-content-of-a-qlineedit-object-into-a-string-variable-c) – skdadle Feb 14 '18 at 09:03

2 Answers2

0

if you use qt, you can use QFile class from Qt to exchange with a file.

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

Just a quick sample:

void writeInBatch(const QString &_str)
{
    QFile file("your batch");
    file.open(_str, QIODevice::WriteOnly);
    file.write(_str);        
    file.close();
}
Gerhard
  • 22,678
  • 7
  • 27
  • 43
doudouremi
  • 186
  • 1
  • 6
0

I think this is irrelevant to a lot of people but here's how my function finally looks like, & it does exactly what I want it to ^^

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

had to include Fstream which helped me append to my .txt file and (I don't know if this is valid or not, or if it's redundant but I also had this
const char * filePath = "C:/Users/name goes here/Downloads/fish.txt";

Hope this helps ^^

skdadle
  • 155
  • 1
  • 2
  • 17