skdadle
skdadle

Reputation: 165

calling a Qt widget main window function in my main

I'm trying call a Qt Mainwindow function in my main function but unfortunately it does not work

some info: I've written a simple log in application. here's the code for it:

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

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

else
 {
   QMessageBox::warning(this,"Login", "Username and Password is not 
correct");
  }

}

I also have a piece of code for saving the content of a batch file into a vector. and in that code I have a find function, to look for a certain word.

what I'm trying to achieve here exactly is 1.save the contents of the batch to my vector, then have the find function look for that certain word in that vector and then when this word is found, prompt the user to enter username and password (which is done through my log in application)

Now here's where my problem is, I have all the code, separately but I don't know how to put it together, I'm not a c++ programmer, in fact I'm a complete noob. my boss just asked me to get this done, and I'm struggling :c

Here's the code for populating my vector with the contents of the batch file

 int main()
    {
   // VARIABLES
   // file path, pointer
  const char * filePath = "C:/Users/Name/Downloads/test.bat";       

   // single line
  std::string line;
   // vector
  std::vector<std::string> my_vec;           
  // filestream, reading
  //std::ifstream myfile("batch.bat");
  std::ifstream myfile(filePath);

  // Test to open file
  if (!myfile)
  {
   std::cout << "Error opening file" << std::endl;
   exit(1);
  }
  else
  {
   std::cout << "File opened successfully (for reading)" << std::endl;
  }


  // Read from file and put into vector
  while (myfile)  // while we are reading the file
  {
   getline(myfile, line);
   my_vec.push_back(line);
  }
//my find function
std::vector<std::string>::iterator i;
i = std::find(my_vec.begin(),my_vec.end(),"batch");
   if (i != my_vec.end())

  /**** here is where id like to call my clicked function ****/
  /* tried putting in "void MainWindow::on_pushButton_Login_clicked()" but it 
  here's what I got */

1. error: undefined reference to `qMain(int, char**)' and collect2.exe:-1: error: error: ld returned 1 exit status

  else
  std::cout<<"No result found" << std::endl;

  // Close filestream
  myfile.close();

  return 0;
} 

I truly apologize if this is too specific or an extremely dumb question, but I just needed help and I couldn't find anything on such a specific question, also everything that could help, is for people who actually know what they're doing with c++

I'd appreciate any contribution!

Bearded beaver: My MainWindow class is defined in my mainwindow.H header file which I included in my main.cpp file

heres the code in that Mainwindow.h file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
  class MainWindow;
}

 class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();
  void on_pushButton_Login_clicked();


  Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

and here is what I entered in my Find function from above

std::vector<std::string>::iterator i;
i = std::find(my_vec.begin(),my_vec.end(),"much");
    if (i != my_vec.end())

  MainWindow.on_pushButton_Login_clicked();

    else
  std::cout<<"No result found" << std::endl;

and this is the error that I get

main.cpp:101: error: expected unqualified-id before '.' token MainWindow.on_pushButton_Login_clicked(); ^

thanks for taking the time!

Upvotes: 1

Views: 2117

Answers (2)

crillion
crillion

Reputation: 380

I've just been helped solving this same problem (see here). What makes the error disappear is having a main(int argc, char* argv[]) with parameters; if it misses them the rather obscure error about qMain is emitted.

Upvotes: 0

skdadle
skdadle

Reputation: 165

I basically did something really stupid by altering the main.cpp file (in Qt Widgets application) and removing the default main function which looked like this

#include "mainwindow.h"
#include <QApplication>

    int main(int argc, char *argv[])
    {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();

      return a.exec();
    }

In that the object name was declared, I needed it to to call my function which was in that MainWindow class. like so:

w.on_pushButton_Login_clicked();

unfortunately though, my find function does not work. I wanted to have a conditional statement that calls upon my function if a certain string element in my vector is found using:

if (std::find(my_vec.begin(),my_vec.end(),much) != my_vec.end() ) //or "much"
 w.on_pushButton_Login_clicked();
else
 std::cout<<"No result found" << std::endl;   

I get no errors, it just prompts me to the log in application regardless of if element was found.

I don't know why this happens, but if I found out, I will edit this answer ^^ thank you guys for taking the time, apologize in advance if i used wrong terminology/wording.

Upvotes: 1

Related Questions