Qt und Aufzählungen als Properties [solved]

Alles rund um die Programmierung mit Qt
Antworten
kitov
Beiträge: 36
Registriert: 29. November 2006 12:24

Qt und Aufzählungen als Properties [solved]

Beitrag von kitov »

hier mein code

ports.h

Code: Alles auswählen

#ifndef _PORTS_H_
#define _PORTS_H_

#include <QObject>
#include <QMetaType>

/// ports on controller
class Ports:public QObject {
	Q_OBJECT
	// register Enum to the meta object system
	Q_ENUMS(Ports::Type)
public:
	enum Type {
		AIN1=0,AIN2,AIN3,AIN4,
		COUNTER1,COUNTER2,PWM1,PWM2,PWM3,PWM4
	};
	static QStringList list();
	static QString toString(Ports::Type);
	static Ports::Type fromString(const QString &);
private:
	Ports();
};

#endif //	_PORTS_H_
ports.cpp

Code: Alles auswählen

#include "ports.h"
#include <QStringList>

QStringList Ports::list() {
	return QStringList()<<"AIN1"
	<<"AIN2"
	<<"AIN3"
	<<"AIN4"
	<<"COUNTER1"
	<<"COUNTER2"
	<<"PWM1"
	<<"PWM2"
	<<"PWM3"
	<<"PWM4";
}

QString Ports::toString(Ports::Type port){
	return Ports::list().at(static_cast<int>(port));
}

Ports::Type Ports::fromString(const QString &port){
	int index=Ports::list().indexOf(port);
	if(index<0) {
		qCritical(qPrintable(QString("Ports::fromString ")+
							 QString("unknown port")));
		index=0;
	}
	return static_cast<Ports::Type>(index);
}
ports_combo_box.h

Code: Alles auswählen

#ifndef _PORTS_COMBO_BOX_H_
#define _PORTS_COMBO_BOX_H_

//#include "item_combo_box.h"
#include "ports.h"
#include <QComboBox>

class PortsComboBox:public QComboBox {
	Q_OBJECT
	Q_PROPERTY(Ports::Type port READ currentItem WRITE setCurrentItem)
	Q_ENUMS(Ports::Type)
public:
	explicit PortsComboBox(QWidget *parent=0);
	void setCurrentItem(Ports::Type);
	Ports::Type currentItem() const;
};

#endif //	_PORTS_COMBO_BOX_H_
ports_combo_box.cpp

Code: Alles auswählen

#include "ports_combo_box.h"

PortsComboBox::PortsComboBox(QWidget *parent)
:QComboBox(parent)
{
	addItems(Ports::list());
}

Ports::Type PortsComboBox::currentItem() const{
	return Ports::fromString(currentText());
}

void PortsComboBox::setCurrentItem(Ports::Type port){
	setCurrentIndex(static_cast<int>(port));
}
Die Frage ist :
Warum wenn ich auf Property "port" zugreife bekomme ich die Fehlermeldung

Code: Alles auswählen

QMetaProperty::read: Unable to handle unregistered datatype 'Ports::Type' for property 'PortAssignmentSettingsBox::port'
Mach ich was falsch oder ist das ein Qt bug ?

P.S. Qt v. 4.4.0
kitov
Beiträge: 36
Registriert: 29. November 2006 12:24

Re: Qt und Aufzählungen als Properties [solved]

Beitrag von kitov »

Alles klar , anworte ich eben selbst :-)
in der Klasse wo Enum definiert ist , darf nicht Q_ENUMS(Class::Enum) stehen , einfach Q_ENUMS(Enum) .
Man darf nicht diese Aufzählung als metatype deklarieren z.B. mit
Q_DECLARE_METATYPE() , weshalb weiss ich nicht .
Antworten