【问题标题】:calling a Qt widget main window function in my main在我的主窗口中调用 Qt 小部件主窗口函数
【发布时间】:2018-07-10 08:53:08
【问题描述】:

我正在尝试在我的主函数中调用 Qt Mainwindow 函数,但不幸的是它不起作用

一些信息:我编写了一个简单的登录应用程序。这是它的代码:

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

}

我还有一段代码用于将批处理文件的内容保存到向量中。 在该代码中,我有一个查找功能,用于查找某个单词。

我在这里想要实现的正是 1. 将批处理的内容保存到我的向量中,然后让 find 函数在该向量中查找该特定单词,然后当找到该单词时,提示用户输入用户名和密码(通过我的登录应用程序完成)

现在这就是我的问题所在,我有所有代码,分开但我不知道如何将它们放在一起,我不是 C++ 程序员,事实上我是一个完整的菜鸟。我的老板只是让我完成这项工作,而我正在苦苦挣扎:c

这是用批处理文件的内容填充我的向量的代码

 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。错误:未定义对 `qMain(int, char**)' 的引用 和 collect2.exe:-1: error: error: ld 返回 1 退出状态

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

  // Close filestream
  myfile.close();

  return 0;
} 

如果这个问题太具体或非常愚蠢,我真的很抱歉,但我只是需要帮助,我找不到关于这样一个具体问题的任何内容,而且所有可能提供帮助的东西都是针对那些真正了解自己的人的'正在用 c++ 做

我将不胜感激任何贡献!

胡须海狸:我的 MainWindow 类在我的 mainwindow.H 头文件中定义 我将其包含在我的 main.cpp 文件中

这是 Mainwindow.h 文件中的代码

#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

这是我在上面的 Find 函数中输入的内容

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;

这是我得到的错误

main.cpp:101: 错误: '.' 之前的预期 unqualified-id令牌 MainWindow.on_pushButton_Login_clicked(); ^

感谢您抽出宝贵时间!

【问题讨论】:

  • 您在main 中的哪里定义MainWindow 类?如我所见,您正在尝试调用MainWindow::on_pushButton_Login_clicked() 是静态函数,而它不是静态的。所以你需要定义一个MainWindow类型的对象并调用这个对象的函数
  • 我认为 MainWindow 类是在我的头文件中定义的,代码如下:#ifndef MAINWINDOW_H #define MAINWINDOW_H #include namespace Ui { class MainWindow; } 类 MainWindow : public QMainWindow { Q_OBJECT public: 显式 MainWindow(QWidget *parent = 0); 〜主窗口();无效 on_pushButton_Login_clicked(); ui::主窗口 *ui; }; #endif // MAINWINDOW_H 我是怎么做到的: if (i != my_vec.end()) // MainWindow.on_pushButton_Login_clicked();但我在 ' 之前得到了预期的不合格 id。 '令牌
  • 不要在 cmets 中发布您的代码,而是编辑问题
  • 开始删除代码,直到没有其他内容可以删除而不会丢失此错误。这叫做最小化。您可能会显示许多不相关的内容,甚至没有指出代码转储中的哪一行是第 101 行。

标签: c++ qt function main qtwidgets


【解决方案1】:

我基本上做了一些非常愚蠢的事情,改变了 main.cpp 文件(在 Qt Widgets 应用程序中)并删除了默认的 main 函数,看起来像这样

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

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

      return a.exec();
    }

在声明了对象名称后,我需要它来调用该 MainWindow 类中的函数。像这样:

w.on_pushButton_Login_clicked();

不幸的是,我的查找功能不起作用。 如果使用以下方法找到向量中的某个字符串元素,我希望有一个条件语句调用我的函数:

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;   

我没有收到任何错误,它只是提示我登录应用程序,无论是否找到元素。

我不知道为什么会发生这种情况,但如果我发现了,我会编辑这个答案^^谢谢你们抽出时间,如果我使用了错误的术语/措辞,请提前道歉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多