2016-04-07 10 views
0

私は3つのクラスを持っています。ui-fileクラスから他のクラスのスロットにボタンを接続する方法は?

  • デザイナー(UI-ファイル)から来ているメインウィンドウ
  • クラスウィッヒを持つクラスは、挿入
  • コントローラクラスなどのデータベースのものを管理します。私は後で全部をネットワーク通信に広げたいと思っています。

私の問題:

私はdatabaseclassからスロットaddEntryでウィンドウクラスからの単純なQPushButtonui->addbuttonを接続したいが、私はこのエラーを取得:

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"; 
} 

私は6時間を探していたが、私は2クラス、のcontrolerおよびUIファイルで、このような例を見たことがありません。誰か助けてくれますか?

+0

接続が良く見えます。 generalcontroler.cppの '#include 'が役立つかどうか試してみてください。コンパイラがforward-declarationだけでQPushButtonを知っている場合、コンパイラはそれがQObjectであることを知らないため、(QObject *を含む)connect()シグネチャは一致しません。 –

+0

'QObject :: connect(....);を明示的に呼び出すのが助かりますか?コードはうまく見えます... – Elijan9

+0

フランクは私のヒーローですよ、皆さんありがとう、いい週末あります:) – Markus

答えて

1

接続がうまく見えます。 generalcontroler.cppの#include <QPushButton>が役立つ場合に試してください。コンパイラがforward-declarationだけでQPushButtonを知っている場合、それはQObjectであることを知らないため、(QObject *を含む)connect()シグネチャは一致しません。

関連する問題