简体   繁体   中英

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.

I'm trying to create a GUI for an already created c program (an image scraper). 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. 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.

#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() .

  1. A space is missing between new and QProcess .

  2. There's no parent variable in scope. There is, of course, a parent() member of QObject . You can simply parent the process to the window itself.

  3. You can't call QLineEdit::text without an object, as the error says. Only you know what object you need. Let's pretend for now that the object is ui->myLineEdit .

  4. The tilde expansion is done by the shell. The kernel has no idea what a tilde is, neither does a QProcess . You need to provide full path to the executable.

  5. The home directory is not always available from the HOME environment variable either. It should be obtained from the portable QDir::homePath() .

  6. QProcess::start() does not take two strings. 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'

and then replace

 QString arguments = QLineEdit::text();

With

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM