c++ - QObject::connection(const QObject*, const char*, const char*, Qt::ConnectionType) "Cannot convert argument 3 from 'fileProcessor' to const QObject *" -



c++ - QObject::connection(const QObject*, const char*, const char*, Qt::ConnectionType) "Cannot convert argument 3 from 'fileProcessor' to const QObject *" -

i need instance of p of type qobject seems, have extended qobject , defined keyword q_object in fileprocessor.h, not sure else 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 'fileprocessor' 'const qobject *' no user-defined-conversion operator available can perform conversion, or operator cannot called

what compiler means line

cannot convert argument 3 'fileprocessor' 'const qobject *'

is passing in object rather pointer object.

so need ampersand pointer p, this, right?

fileprocessor p; ui->setupui(this); qobject::connect(ui->open, signal(clicked()), &p,slot(on_action_open_triggered()));

it compile, won't work.

why? variable p go out of scope , destroyed @ end of constructor, there's no way it's slot ever called.

the straightforward solution declare p fellow member variable of mainwindow class. way fileprocessor exist long mainwindow exists.

c++ qt signals slots

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -