2012-09-18 74 views
2

我試圖用QSignalMapper在單擊時發送信號發送器(QPushButton)到插槽。但它不起作用,我真的不知道爲什麼。Qt QSignalMapper不起作用

如果你知道不同的方式來做到這一點,這將是偉大通知我,但我真的想知道如何使用QSignalMapper,因爲它可以在將來有用。

moviebase.h

#ifndef MOVIEBASE_H 
    #define MOVIEBASE_H 

    #include <QtGui/QMainWindow> 
    #include <Qt\qsignalmapper.h> 

    #include "ui_moviebase.h" 
    #include "include\DBAdapter.h" 
    #include "include\objView.h" 

    class MovieBase : public QMainWindow 
    { 

     Q_OBJECT 

    public: 
     MovieBase(QWidget *parent = 0, Qt::WFlags flags = 0); 
     ~MovieBase(); 

    private: 

     Ui::MovieBaseClass ui; 

     DBAdapter *db; 
     DBAdapter::Type type; 
     QPushButton *buttonChecked; 
     QSignalMapper *pushButtonMapper; 

     void setMainButtonsFunct(); 


    private slots: 
     void button_pushed(const QPushButton &); 

    }; 

    #endif // MOVIEBASE_H 

moviebase.cpp

#include "moviebase.h" 

MovieBase::MovieBase(QWidget *parent, Qt::WFlags flags) 
    : QMainWindow(parent, flags) 
{ 
    ui.setupUi(this); 
    db = new DBAdapter(); 

    this->type = DBAdapter::Movie; 

    this->setMainButtonsFunct(); 

    ObjView *obj = new ObjView(this->ui.objView); 

    obj->view(db->get_All_Elements(this->type)); 
} 

void MovieBase::setMainButtonsFunct() 
{ 
    this->buttonChecked = ui.watchedButton; 

    this->pushButtonMapper = new QSignalMapper(this); 

    connect(this->ui.watchedButton, SIGNAL(clicked()), this->pushButtonMapper, SLOT(map())); 
    connect(this->ui.towatchButton, SIGNAL(clicked()), this->pushButtonMapper, SLOT(map())); 
    connect(this->ui.availableButton, SIGNAL(clicked()), this->pushButtonMapper, SLOT(map())); 
    connect(this->ui.allButton, SIGNAL(clicked()), this->pushButtonMapper, SLOT(map())); 

    this->pushButtonMapper->setMapping(this->ui.watchedButton, this->ui.watchedButton); 
    this->pushButtonMapper->setMapping(this->ui.towatchButton, this->ui.towatchButton); 
    this->pushButtonMapper->setMapping(this->ui.availableButton, this->ui.availableButton); 
    this->pushButtonMapper->setMapping(this->ui.allButton, this->ui.allButton); 

    connect(this->pushButtonMapper, SIGNAL(mapped(const QPushButton &)), this, SLOT(button_pushed(const QPushButton &))); 
} 

void MovieBase::button_pushed(const QPushButton &sender) 
{ 
    qDebug() << "button pushed"; 
    this->ui.watchedButton->setChecked(false); 
} 

MovieBase::~MovieBase() 
{ 

} 
+0

http://stackoverflow.com/questions/12173153/qt-dynamic-widgets-signal-and-slot-connection/12173296#12173296這可能會有幫助..... 。 – shan

回答

4

您只能使用中存在的QSignalMapper的信號。沒有這樣的信號mapped(const QPushButton&)。使用mapped(QWidget*)並更改您的插槽以具有相同的簽名:button_pushed(QWidget*)

+0

thx很多,現在工作:D –

0

看起來像所有的好。看你編譯器輸出。也許你定義錯了,你的信號/槽簽名