2017-07-22 4 views
-1

私はPythonで '翻訳'したいC++クラスを持っています。 C++では、私のクラスのインスタンスはQDataWidgetMapperによってマップされ、正常に動作します。 しかし、私はそれをPythonで動作させることはできません。 私のクラスはC++とPythonです。QDataWidgetMapperでマッピングできるPythonクラス

#include <QRadioButton> 
#include "optiongroup.h" 
#include <qdebug.h> 

OptionGroup::OptionGroup(QWidget *parent) :QWidget(parent), currentSelection_(-1) 
{ 
    qDebug()<<"1.OptionGroup: currentSelection_=" << currentSelection_; 
} 

int OptionGroup::currentSelection() const 
{ return currentSelection_; } 

void OptionGroup::setCurrentSelection(int selection) 
{  
    // If the specified selection id is not in our button map, 
    // then it is invalid, set selection to -1. Otherwise, 
    // update the selection to user specified value 
    qDebug()<<"1.setCurrentSelection: selection=" <<selection; 
    auto iter = buttonMap_.find(selection); 
    qDebug() << "2.setCurrentSelection: iter=" << iter.value() ; 
    if (iter == buttonMap_.end() || selection < 0) { 
     currentSelection_ = -1; 
     for (iter = buttonMap_.begin(); iter != buttonMap_.end(); ++iter) 
      iter.value()->setChecked(false); 
    } else { 
     iter.value()->setChecked(true); 
     currentSelection_ = selection; 
    } 
} 

void OptionGroup::setSelectionId(QRadioButton* button, int id) 
{ 
    // Make sure we got a valid Id (non-negative) 
    // Also then listen for signals from this button 
    qDebug()<<"1.setSelectionId: button=" <<button->objectName() << " id=" << id; 
    if (id >= 0) { 
     buttonMap_[id] = button; 
     revButtonMap_[button] = id; 
     connect(button, SIGNAL(toggled(bool)), this, SLOT(buttonToggled(bool))); 
    } 
} 

void OptionGroup::buttonToggled(bool checked) 
{ 
    qDebug()<<"1.buttonToggled: checked=" <<checked; 
    if (checked == true) { 
     QRadioButton* btn = qobject_cast<QRadioButton*>(sender()); 
     Q_ASSERT(btn); 
     currentSelection_ = revButtonMap_[btn]; 
     emit selectionChanged(currentSelection_); 
    } 
} 

void OptionGroup::clear() 
{ 
    qDebug() << "1.clear"; 
    foreach (QRadioButton *RadioButton, buttonMap_){ 
     RadioButton->setAutoExclusive(false); 
     RadioButton->setChecked(false); 
     RadioButton->setAutoExclusive(true); 
    } 
    currentSelection_ = false; 
    qDebug() << "2.clear emit selectionChanged"; 
    emit selectionChanged(currentSelection_); 
} 

PYTHON::これは、ソースファイルである

#ifndef OPTIONGROUP_H 
#define OPTIONGROUP_H 

#include <QWidget> 
#include <QMap> 

class QRadioButton; 

class OptionGroup : public QWidget 
{ 
Q_OBJECT 
Q_PROPERTY(int currentSelection READ currentSelection WRITE setCurrentSelection USER true) 

public: 
explicit OptionGroup(QWidget *parent = 0); 
int currentSelection() const; 
void setCurrentSelection(int selection); 
void setSelectionId(QRadioButton *button, int id); 
void OptionGroup::clear(); 

signals: 
void selectionChanged(int selection); 

public slots: 
void buttonToggled(bool checked); 

private: 
int currentSelection_; 
QMap<int, QRadioButton*> buttonMap_; 
QMap<QRadioButton*, int> revButtonMap_; 
}; 

#endif // OPTIONGROUP_H 

これは、ヘッダファイルである

from PyQt4 import QtGui, QtCore 
from PyQt4.QtCore import QObject, SIGNAL, SLOT, pyqtProperty, pyqtSignal, QPoint, qDebug 
from PyQt4.QtGui import QRadioButton 


class QMap(dict): 
    def __init__(self): 
     self = [] 
     pass 
    def find(self, item): 
     return self[item]   
    def begin(self): 
     return 0 
    def end(self): 
     return len(self) 




class OptionGroup(QtGui.QWidget) : 
    def __init__(self, parent): 
     super(OptionGroup, self).__init__(parent) 
     self.currentSelection_ = -1 
     print("1.OptionGroup: currentSelection_=", self.currentSelection_) 
     self.buttonMap_ = QMap() 
     self.revButtonMap_ = QMap() 
    def currentSelection(self): 
     return self.currentSelection_ 
    def setCurrentSelection(self, selection): 
     # If the specified selection id is not in our button map, 
     # then it is invalid, set selection to -1. Otherwise, 
     # update the selection to user specified value 
     print("1.setCurrentSelection: selection=" ,selection) 
     iter = self.buttonMap_.find(selection) 
     print("2.setCurrentSelection: iter=", iter.value()) 
     if (iter == self.buttonMap_.end() or selection < 0): 
      self.currentSelection_ = -1 
      for iter in range(self.buttonMap_.begin(), self.buttonMap_.end()): 
       iter.setChecked(False); 
     else: 
      iter.setChecked(True) 
      self.currentSelection_ = selection 

    currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection) 



    def setSelectionId(self, button, id): 
     # Make sure we got a valid Id (non-negative) 
     # Also then listen for signals from this button 
     print("1.setSelectionId: button=", button.objectName(), " id=", id) 

     if id >= 0: 
      self.buttonMap_[id] = button 
      self.revButtonMap_[button] = id 
      QObject.connect(button, SIGNAL('toggled(bool)'), self.buttonToggled) 
    def buttonToggled(self, checked): 
     btn = self.sender()   
     print("1.buttonToggled: checked=", btn.checked) 
     if btn.isChecked() == True:    
      self.currentSelection_ = self.revButtonMap_[btn]    
      self.emit(QtCore.SIGNAL("selectionChanged"), self.currentSelection_) 
    def clear(self): 
     print("1.clear") 
     for key, RadioButton in self.buttonMap_.items():    
      RadioButton.setAutoExclusive(False) 
      RadioButton.setChecked(False) 
      RadioButton.setAutoExclusive(True) 

     self.currentSelection_ = False 
     print("2.clear emit selectionChanged") 
     self.emit(SIGNAL('selectionChanged'), self.currentSelection_) 

(Pythonのバージョンでは、私は「カスタムを作成しましQMap Python 3.4には存在しないので、私が使用しているクラス)

私は何が間違っていて、私のPythonクラスはC++のように動作しませんか? (それは私のデータベースからデータを表示しないように、適切に、QDataWidgetMapperによってマッピングされていない)

答えて

0

は私の自己に答え、解決策は非常に簡単だった:

私は

currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection) 

を変更。.. .to:

currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection, user = True) 
関連する問題