该类工作正常,但现在我需要将它存储在QVariant中。以为我只需像往常一样添加Q_DECLARE_METATYPE(Docs),但不会在添加宏的情况下编译。
ValueRange.h
#ifndef VALUERANGE_H
#define VALUERANGE_H
template <typename T>
class ValueRange{
public:
ValueRange() = default;
ValueRange(const ValueRange &other)
: isInverted_(other.isInverted_), min_(other.min_), max_(other.max_){}
~ValueRange() = default;
ValueRange(const T &min, const T &max) : min_(min), max_(max){}
T min() const{ return min_; }
T max() const{ return max_; }
void invert(){ isInverted_ = true; }
bool isInverted() const{ return isInverted_; }
bool operator==(const ValueRange &other) const{
return other.min()==min_ && other.max()==max_;
}
bool operator!=(const ValueRange &other) const{
return other.min()!=min_ || other.max()!=max_;
}
private:
bool isInverted_ = false;
T min_;
T max_;
};
using IntRange = ValueRange<int>;
using DoubleRange = ValueRange<double>;
Q_DECLARE_METATYPE(IntRange);
Q_DECLARE_METATYPE(DoubleRange);
#endif // VALUERANGE_H
答案 0 :(得分:3)