Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to connect button to function from other class?



  • Hi, what am I missing?

    I don't know how to connect function from class Baza to QPushButton in .cpp ui file.
    EncryptFile.h

    class EncryptFile : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    	EncryptFile(QWidget *parent = Q_NULLPTR);
    
    private:
    	Ui::EncryptFileClass ui;
    
    private slots:
    	void run();
    };
    

    Part EncryptFile.cpp

    EncryptFile::EncryptFile(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    	Baza *plik = new Baza();
    	connect(ui.createBtn, &QPushButton::clicked, this, &EncryptFile::run);
    	connect(ui.bazaBtn, &QPushButton::clicked, this, &Baza::createDatabase);
    }
    

    Baza.h

    class Baza : public QMainWindow 
    {
    	Q_OBJECT
    private:
    	sqlite3 *database;
    	char *errMsg = 0;
    	int rc;
    public:
    	Baza(QWidget *parent = Q_NULLPTR) {};
    	~Baza() {};
    	
    public slots:
    	void createDatabase();
    };
    

    First connect works. But another connect spit out: E0304, C2664, C3867.

    Any ideas? Thanks :)


  • Lifetime Qt Champion

    Hi,

    Because you are using "this" as target which does not contain the method since it's not the same classe.

    On a side note, Qt has a SQL module so you don't have to use SQLite directly.



  • omg. I'm tired.

    Really, big thanks :)


  • Lifetime Qt Champion

    You're welcome !

    Since that answers your question, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)


Log in to reply