简体   繁体   English

如何在Qt5中声明Qt :: PenStyle?

[英]How to declare Qt::PenStyle in Qt5?

I am migrating a project from Qt 4.x to 5, and there is a class that contains an attribute that goes: 我正在将项目从Qt 4.x迁移到5,并且有一个包含以下属性的类:

Qt::PenStyle penStyle;

and its respective get function: 及其各自的get函数:

Qt::PenStyle getPenStyle() {return penStyle;}

I get a compilation error saying that 我收到一个编译错误,说

error: 'QVariant::QVariant(Qt::PenStyle)' is private
     QVariant(Qt::PenStyle) Q_DECL_EQ_DELETE;
     ^
..\marssies\painterbar.cpp:217:88: error: within this context
         lineStyle -> setCurrentIndex(lineStyle -> findData(graphicObject->getPenStyle()));
                                                                                        ^

Doing some research I found this: 做一些研究,我发现:

QVariant: 的QVariant:
*Inconsistent constructor taking Qt::GlobalColor and producing QVariant(QColor) instance was removed. *删除了采用Qt :: GlobalColor和产生QVariant(QColor)实例的不一致构造函数。 Code constructing such variants can be migrated by explicitly calling QColor constructor. 可以通过显式调用QColor构造函数来迁移构造此类变量的代码。 For example from “QVariant(Qt::red)” to “QVariant(QColor(Qt::red)) 例如,从“ QVariant(Qt :: red)”到“ QVariant(QColor(Qt :: red))”

*Similarly, implicit creation of QVariants from enum values Qt::BrushStyle, Qt::PenStyle, and Qt::CursorShape have been removed. *类似地,已从枚举值Qt :: BrushStyle,Qt :: PenStyle和Qt :: CursorShape隐式创建QVariant。 Create objects explicitly or use static_cast(Qt::SolidLine) to create a QVariant of type int with the same value as the enum. 显式创建对象或使用static_cast(Qt :: SolidLine)创建类型为int且与枚举值相同的QVariant。

The question is, how should I declare the attribute/function if I cant do Qt::PenStyle just like that? 问题是,如果不能像这样那样做Qt :: PenStyle, 我应该如何声明属性/函数 What does it mean by with creating objects explicitly? 显式创建对象意味着什么? I've tried doing something like the quote says but I didnt manage to solve it. 我曾尝试做如报价所述的事情,但我没有设法解决它。 I did manage to solve other similar errors but none of them were declarations of attributes or functions. 我确实设法解决了其他类似的错误,但是它们都不是属性或函数的声明。

Full error: 完整错误:

In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtCore/qsettings.h:46:0,
                 from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtCore/QSettings:1,
                 from ..\marssies\appcommon.h:10,
                 from ..\marssies\painterbar.h:5,
                 from ..\marssies\painterbar.cpp:1:
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qvariant.h: In member function 'void PainterBar::setGraphicObject(GraphicsPrimitive*, PainterBar::FinishMode)':
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qvariant.h:482:5: error: 'QVariant::QVariant(Qt::PenStyle)' is private
     QVariant(Qt::PenStyle) Q_DECL_EQ_DELETE;
     ^
..\marssies\painterbar.cpp:217:88: error: within this context
         lineStyle -> setCurrentIndex(lineStyle -> findData(graphicObject->getPenStyle()));
                                                                                        ^

The problem is that enums are a bit of a tricky type and Qt 5 seems to have removed some specific magic for these enums in question. 问题在于,枚举类型有些棘手,而Qt 5似乎已删除了这些枚举的某些特定魔术。 The base template constructor for QVariant is defined as private as to enforce that QVariants can only be constructed with types they really can handle. QVariant的基本模板构造函数被定义为私有的,以强制QVariant只能使用其真正可以处理的类型来构造。

The first solution proposed in the docs is to instantiate an object because it is easier to hold a "normal" type in a QVariant than an enum. 文档中提出的第一个解决方案是实例化一个对象,因为在QVariant中持有“普通”类型比枚举更容易。 QColor, QPen etc. are all QObjects so the general logic to hold them in a QVariant works for them; QColor,QPen等都是QObject,因此将它们保存在QVariant中的一般逻辑适用于它们。 it doesn't work for Qt::PenStyle. 它不适用于Qt :: PenStyle。

The second solution then is the typical C-style solution people use when dealing with enums. 第二种解决方案是人们在处理枚举时使用的典型C风格解决方案。 As an enum is stored as an int, you can always explicitely cast to int (and C sometimes also does this implicitely, where C++ doesn't, afaik). 由于枚举以int形式存储,因此您始终可以显式地强制转换为int(C有时也隐式地将其转换为int,而C ++则没有,afaik)。 So what is effectively stored in the QVariant is an int and you, the programmer are the only one that knows in reality it is a Qt::PenStyle. 因此,有效存储在QVariant中的是一个int,而您,程序员是唯一知道实际上是Qt :: PenStyle的人。

While the first solution is slightly less efficient (thinking of storing thousands of QPen objects instead of QPenStyles/ints), it might be the cleaner one, as the semantics (the QVariant holds a pen you can draw with) are preserved. 尽管第一种解决方案的效率稍低(想存储数千个QPen对象而不是QPenStyles / ints),但它可能更干净一些,因为它保留了语义(QVariant握有一支可以用来绘制的笔)。

If this doesn't work for you: 如果这对您不起作用:

int getPenStyle() {return (int)penStyle;}

or 要么

QPen getPenStyle() {return QPen(penStyle);}

then please go ahead and post a self-contained example so we can further discuss. 然后请继续发布一个独立的示例,以便我们进一步讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM