我有一个c ++课程,我想要翻译'在Python中。 在c ++中,我的类的实例由QDataWidgetMapper映射,工作正常。 但我无法在Python中使用它。 这是我的课程,用C ++和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
这是源文件:
#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:
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版本I中创建了一个自定义QMap类,因为它不存在于Python 3.4中,我正在使用)
我做错了什么,我的Python类不像C ++那样工作? (没有通过QDataWidgetMapper正确映射,因此它不会显示我数据库中的数据)
答案 0 :(得分:0)
回答我自己,解决方案非常简单:
我改变了
currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection)
...为:
currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection, user = True)