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.
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.