0
votes

I need the instance of p to by of type QObject it seems, I have extended QObject and defined the keyword Q_OBJECT in fileprocessor.h, I am not sure what else I can do.

-Fileprocessor.h

#ifndef FILEPROCESSOR_H
#define FILEPROCESSOR_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QStandardItemModel>
#include <QObject>

class fileProcessor: public QObject
{
    Q_OBJECT
public:
    fileProcessor();
public slots:
    void on_action_Open_triggered();
    void checkString(QString &temp, QChar ch = 0);

public:
QList<QStringList> csv;
QStandardItemModel *model;
QList<QStandardItem*> standardItemList;
};


#endif // FILEPROCESSOR_H

-Mainwindow.cpp

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

MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{
    fileProcessor p;
    ui->setupUi(this);
    QObject::connect(ui->Open, SIGNAL(clicked()),
                     p,SLOT(on_action_Open_triggered()));
}

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

-Mainwindow.cpp

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();


public:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

Error:

C:\Qt\Qt5.3.2\Tools\QtCreator\bin\Assignment3\mainwindow.cpp:10: error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const' : cannot convert argument 3 from 'fileProcessor' to 'const QObject *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

1

1 Answers

2
votes

What the compiler means by this line

cannot convert argument 3 from 'fileProcessor' to 'const QObject *'

is that you are passing in an object rather than a pointer to the object.

So all you need to do is an the ampersand to get a pointer to p, like this, right?

fileProcessor p;
ui->setupUi(this);
QObject::connect(ui->Open, SIGNAL(clicked()),
                 &p,SLOT(on_action_Open_triggered()));

It will compile, but it won't work.

Why? Your variable p will go out of scope and be destroyed at the end of the constructor, so there's no way it's slot will ever be called.

The most straightforward solution is to declare p as a member variable of your MainWindow class. That way your fileProcessor will exist as long as its MainWindow exists.