Search code examples
c++qtsignals-slotsqt-signals

How to connect a button from ui-file class to slots from other classes?


I have 3 classes.

  • class with a mainwindow which comes from the designer(ui-file)
  • class wich will manage database stuff like inserts
  • controller class. I want to extend the whole thing to networkcommunication later.

My problem:

I want to connect a simple QPushButton ui->addbutton from the window class with a slot addEntry from the databaseclass, but I get this error :

ERROR: no matching function for call to 'generalControler::connect(QPushButton*, const char*, dbmanager*&, const char*)' mydb, SLOT(addEntry())); //no difference with &mydb or *mydb MainWindow(0x13f57988, name = "MainWindow") QPushButton(0x13f5f3e0, name = "addButton") MainWindow(0x13f57988, name = "MainWindow") 0x13f5f3e0//<--????? //<=Here u see the adresses printed with Qdebug(). top: mainwindowclass. bot: generalcontrolerclass //why there is missing information after returning the adress of a 'ui->addButton' to another class? Is this maybe the problem?

main.cpp

#include <QApplication>
#include "generalcontroler.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

generalControler controler;

return a.exec();
}

generalcontroler.h

#ifndef GENERALCONTROLER_H
#define GENERALCONTROLER_H
#include <QApplication>
#include "mainwindow.h"
#include "dbmanager.h"

class generalControler : public QObject
{
Q_OBJECT
public:
generalControler();
};

#endif // GENERALCONTROLER_H

generalcontroler.cpp

#include "generalcontroler.h"
#include <QDebug>
generalControler::generalControler(){

MainWindow* window = new MainWindow;
window->show();
dbmanager* mydb= new dbmanager("path_to_my_database.db", window);

mydb->addEntry();
qDebug()<<window->getThis()<<window->getcloseButton();
connect(window->getaddButton(), SIGNAL(clicked()),
         mydb,                   SLOT(addEntry()));

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton* getaddButton();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new    Ui::MainWindow){

ui->setupUi(this);
}
QPushButton* MainWindow::getaddButton()
{
    return ui->addButton;
}

dbmanager.h

#ifndef DBMANAGER_H
#define DBMANAGER_H
#include <QSqlDatabase>
#include <QDebug>
#include "mainwindow.h"

class dbmanager: public QObject{

Q_OBJECT
public:
dbmanager(const QString& path);

public slots:
void addEntry();

private:
QSqlDatabase mydatabase;
};

#endif // DBMANAGER_H

dbmanager.cpp

#include "dbmanager.h"


dbmanager::dbmanager(const QString& path)
{
mydatabase = QSqlDatabase::addDatabase("QSQLITE");
mydatabase.setDatabaseName(path);
if (!mydatabase.open())
{
    qDebug() << "Error: connection fail";
}
else
{
    qDebug() << "Database: connection ok";
}
}
void dbmanager::addEntry()
{
    qDebug()<<"addEntry success";
}

I was searching for 6 hours but I never saw such an example with 2 classes, a controler and an ui-file. Could anyone help me?


Solution

  • The connect looks good to me. Try if #include <QPushButton> in generalcontroler.cpp helps. If the compiler knows about QPushButton only by forward-declaration, it doesn't know that it's a QObject and thus the connect() signatures (with QObject* in it) don't match.