使用Qt 5
编译时,以下代码会产生错误styleComboBox = new QComboBox;
typedef QPair<QString, Qt::BrushStyle> BrushPair;
foreach (const BrushPair &pair, QList<BrushPair>()
<< qMakePair(tr("No Brush"), Qt::NoBrush)
<< qMakePair(tr("Solid"), Qt::SolidPattern)
<< qMakePair(tr("Dense #1"), Qt::Dense1Pattern)
<< qMakePair(tr("Dense #2"), Qt::Dense2Pattern)
<< qMakePair(tr("Dense #3"), Qt::Dense3Pattern)
<< qMakePair(tr("Dense #4"), Qt::Dense4Pattern)
<< qMakePair(tr("Dense #5"), Qt::Dense5Pattern)
<< qMakePair(tr("Dense #6"), Qt::Dense6Pattern)
<< qMakePair(tr("Horizontal"), Qt::HorPattern)
<< qMakePair(tr("Vertical"), Qt::VerPattern)
<< qMakePair(tr("Cross"), Qt::CrossPattern)
<< qMakePair(tr("Diagonal /"), Qt::BDiagPattern)
<< qMakePair(tr("Diagonal \\"), Qt::FDiagPattern)
<< qMakePair(tr("Diagonal Cross"), Qt::DiagCrossPattern))
styleComboBox->addItem(pair.first, pair.second);
错误信息如下:
C:\Qt\Qt5.2.1\5.2.1\mingw48_32\include\QtCore\qvariant.h:481: error: 'QVariant::QVariant(Qt::BrushStyle)' is private
QVariant(Qt::BrushStyle) Q_DECL_EQ_DELETE;
^
那我怎么克服它?
答案 0 :(得分:2)
BrushStyle是枚举类型,而不是QVariant类型。我认为你的意思是使用QBrush。
styleComboBox = new QComboBox;
typedef QPair<QString, QBrush> BrushPair;
foreach (const BrushPair &pair, QList<BrushPair>()
<< qMakePair(tr("No Brush"), QBrush(Qt::NoBrush))
<< qMakePair(tr("Solid"), QBrush(Qt::SolidPattern))
...
在ComboBox上调用addItem时,您隐式将 BrushPair 的“第二个”成员转换为QVariant