简体   繁体   English

我的Qt代码无法编译“无法调用没有对象的成员函数”

[英]My Qt code doesn't compile with “cannot call member function without object”

So i'm struggling to find a solution to this and don't know where i'm going wrong. 所以我很难找到解决方案,不知道我哪里出错了。 I'm new to QT (today) and i'm not sure if i'm doing the right thing. 我是QT(今天)的新手,我不确定我是否做得对。

I'm trying to create a GUI for an already created c program (an image scraper). 我正在尝试为已经创建的c程序(图像处理程序)创建GUI。 The image scraper works but I am trying to implement the GUI which allows a user to input a website to scrape images from into a line edit box (lineEdit), and then on click of a push button (pushButton) it takes the input text from the line edit box and uses it as the argument to run the C program in the background. 图像刮刀工作,但我正在尝试实现GUI,允许用户输入网站将图像从线条编辑框(lineEdit)中删除,然后点击按钮(pushButton),它从中获取输入文本行编辑框并将其用作在后台运行C程序的参数。 Except I can't get that far because of the issue mentioned above. 因为上面提到的问题,我无法做到这一点。

Any assistance will be appreciated. 任何帮助将不胜感激。 Below is my code, the header and main files haven't been changed, and any changes that have been made have been done through the GUI designer over manual changes. 下面是我的代码,标题和主文件没有被更改,并且已经通过GUI设计者通过手动更改完成了所做的任何更改。

#include "mainwindow.h"
#include "ui_mainwindow.h"

char *arguments;

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

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

  void MainWindow::on_pushButton_clicked()
  {

    QString program = "~/Desktop/IS";
    QString arguments = QLineEdit::text(); //error on this line

    QProcess *myProcess = newQProcess(parent);
    myProcess->start(program, arguments);

  }

All errors are in on_pushButton_clicked() . 所有错误都在on_pushButton_clicked()

  1. A space is missing between new and QProcess . newQProcess之间缺少空间。

  2. There's no parent variable in scope. 范围内没有parent变量。 There is, of course, a parent() member of QObject . 当然,还有QObjectparent()成员。 You can simply parent the process to the window itself. 您可以简单地将进程父级指向窗口本身。

  3. You can't call QLineEdit::text without an object, as the error says. 如错误所示,您无法在没有对象的情况下调用QLineEdit::text Only you know what object you need. 只有知道你需要什么对象。 Let's pretend for now that the object is ui->myLineEdit . 我们现在假装对象是ui->myLineEdit

  4. The tilde expansion is done by the shell. 波浪扩展由外壳完成。 The kernel has no idea what a tilde is, neither does a QProcess . 内核不知道代字号是什么, QProcess也不知道。 You need to provide full path to the executable. 您需要提供可执行文件的完整路径。

  5. The home directory is not always available from the HOME environment variable either. HOME环境变量中的主目录也不总是可用。 It should be obtained from the portable QDir::homePath() . 它应该从便携式QDir::homePath()

  6. QProcess::start() does not take two strings. QProcess::start()不带两个字符串。 It needs a list of strings as the second parameter. 它需要一个字符串列表作为第二个参数。 Since you only intend to provide one argument, it's a simple matter to wrap it up in a string list. 由于您只打算提供一个参数,因此将其包装在字符串列表中是一件简单的事情。

void MainWindow::on_pushButton_clicked()
{
   QString program = QDir::homePath() + "/Desktop/IS";
   QProcess *myProcess = new QProcess(this);
   myProcess->start(program, QStringList(ui->myLineEdit->text()));
   // The variant above is slightly shorter then the equivalent line below:
   myProcess->start(program, QStringList() << ui->myLineEdit->text());
}

On your MainWindow Form just create a lineEdit somewhere called 'InputWebSite' 在你的MainWindow表单上只需创建一个名为'InputWebSite'的lineEdit

and then replace 然后更换

 QString arguments = QLineEdit::text();

With

QString arguments = ui->InputWebSite->text();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 QT,C ++无法在没有对象的情况下调用成员函数 - QT, C++, cannot call member function without object Qt C ++无法在没有对象的情况下调用成员函数“” - Qt C++ cannot call member function ' ' without object 成员函数调用在 Qt 函数“run”中不起作用。 我如何在“运行”中正确使用我的模板? - member function call doesn´t work in Qt function "run". How I use my template in "run" correctly? QT:当我有一个虚函数时,不能在没有对象的情况下调用成员 - QT : cannot call member without object when I have a virtual function 没有对象错误就无法调用成员函数 - cannot call member function without object error 命名空间:不能在没有对象的情况下调用成员函数 - namespace: cannot call member function without object 不能调用没有 object 的成员 function - cannot call member function without object 没有对象就无法调用成员函数,但是我有一个对象才能调用该函数 - Cannot call member function without object but I call the function with an object 错误:没有 object 无法呼叫成员 function - 但我有一个 object? - error: cannot call member function without object - but I have an object? 没有对象就无法调用成员函数std :: string class :: function() - Cannot call member function std::string class::function() without object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM