[英]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()
。
A space is missing between new
and QProcess
. new
和QProcess
之间缺少空间。
There's no parent
variable in scope. 范围内没有
parent
变量。 There is, of course, a parent()
member of QObject
. 当然,还有
QObject
的parent()
成员。 You can simply parent the process to the window itself. 您可以简单地将进程父级指向窗口本身。
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
。
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. 您需要提供可执行文件的完整路径。
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()
。
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.