erbal
erbal

Reputation: 727

How button onClick works

I am a bit confused with Qt's onClick handling. I have a class which looks like this:

class DatabaseManager : public QObject
{
Q_OBJECT;
private:
    QSqlDatabase db;

public slots: 
    bool openDB(const QString& path);

};

And I have a class which handles the click on the button:

Click::Click(QWidget *parent) : QWidget(parent){
    QPushButton *create = new QPushButton("Create database", this);
    create->setGeometry(50,100,100,100);
    connect(create, SIGNAL(clicked()), this, SLOT(openDB("/home/peter/database.db")));
}

main.cpp

int main(int argc,char **argv){
    QApplication *app = new QApplication(argc, argv);
    QPushButton btn;

    DatabaseManager db;
    btn.move(300,300);
    btn.resize(250,250);
    btn.setWindowTitle("Dibli");
    btn.show();
    return app->exec();
}

How could I tell to the click handler, that I want to use a specific DatabaseManager object's openDB function? Because it doesn't create the file, if I click on it.

I've updated the code.

Upvotes: 0

Views: 2586

Answers (2)

nkint
nkint

Reputation: 11733

You cannot call the specific argument instance in the connect function call.

connect is processed by the MOC - meta object compiler - and add some magick to all object that has the macro Q_OBJECT. You have to call a function inside the connect in which you specify only the argument it will receive. (And if they are non qt-object you have to register them with qRegisterMetaType<MyDataType>("MyDataType"); but this is a different story).

So, remember, every time call:

connect(sender, SIGNAL( event() ),
        receiver, SLOT( onEvent() ))

and then:

void onEvent() {
    mycomplexoperation( ... )
}

EDIT:

thank to Riateche comment, I have to specify that you need qRegisterMetaType<MyDataType>("MyDataType"); with all object not listed in this list. QString not inherits from QObject but could be used in signal/slot system without registration. and thanks to Frank Osterfeld comment I have to add that only for queued signal/slot connections the registration is needed (I didn't know that)

thankd to

Upvotes: 0

CapelliC
CapelliC

Reputation: 60004

assuming your Click class derives from QObject, you should add a slot

public slots:
 void onClick() { openDB("/home/peter/database.db"); }

and connect that:

connect(create, SIGNAL(clicked()), this, SLOT(onClick()))

edit Since you show more code now, here is a different hint. Change main like

int main(int argc,char **argv){
    QApplication *app = new QApplication(argc, argv);
    QPushButton btn;

    DatabaseManager db;
    db.path = "/home/peter/database.db";
    QObject::connect(&btn, SIGNAL(clicked()), &db, SLOT(openDB()));

    btn.move(300,300);
    btn.resize(250,250);
    btn.setWindowTitle("Dibli");
    btn.show();

    return app->exec();
}

and

class DatabaseManager : public QObject
{
Q_OBJECT;
private:
    QSqlDatabase db;

public:
    QString path;

public slots: 
    bool openDB();

};

Note I added a member variable (db.path) to DatabaseManager, and changed the slot openDB removing the argument. That's because the button' signal cannot provide the string. The easier way then is to make it available in the class.

Upvotes: 1

Related Questions