Python 类能够由 QDataWidgetMapper 映射

Python class able to be mapped by QDataWidgetMapper

本文关键字:QDataWidgetMapper 映射 Python      更新时间:2023-10-16

我有一个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_);
}

蟒:

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未正确映射,因此它不显示数据库中的数据(

回答我自己,解决方案非常简单:

我变了

currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection)

。自:

currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection, user = True)
相关文章:
  • 没有找到相关文章