티스토리 뷰

C | C++

QVariant 클래스(QT C++)

야라바 2024. 3. 19. 10:32
728x90

QVariant 클래스는 다양한 QT 데이터 타입을 하나의 타입으로 대신할 수 있도록 도와주는 클래스이다.

 

참조 링크 : https://doc.qt.io/qt-5/qvariant.html

 

hit->setData(Qt::UserRole + 1, (bool)s2i(tC->attr("edit")));
hit->setData(Qt::UserRole + 2, ((wVl = tC->attr("color")).size()) ? QString::fromStdString(wVl) : QVariant());
hit->setData(Qt::UserRole + 3, ((wVl = tC->attr("colorText")).size()) ? QString::fromStdString(wVl) : QVariant());
hit->setData(Qt::UserRole + 4, ((wVl = tC->attr("font")).size()) ? QString::fromStdString(wVl) : QVariant());

tit->setData(Qt::DisplayRole, QVariant());
tit->setData(Qt::DisplayRole, v);
if ((tC && (wVl = tC->attr("color")).size()) || (wVl = hit->data(Qt::UserRole + 2).toString().toStdString()).size() ||
	(wVl = rClr).size())
	tit->setData(Qt::BackgroundRole, QColor(wVl.c_str()));
else
	tit->setData(Qt::BackgroundRole, QVariant());
if ((tC && (wVl = tC->attr("colorText")).size()) || (wVl = hit->data(Qt::UserRole + 3).toString().toStdString()).size() ||
	(wVl = rClrTxt).size())
	tit->setData(Qt::ForegroundRole, QColor(wVl.c_str()));
else
	tit->setData(Qt::ForegroundRole, QVariant());
	
QVariant val = wIt->data(Qt::DisplayRole);
if (val.type() == QVariant::Bool)
	val = val.toInt();
AttrValS attrs;
attrs.push_back(std::make_pair("set", val.toString().toStdString()));

 

QVariant 클래스를 빈번하게 사용하는 대표적인 사례는 QSettings 클래스(QSettings 클래스(QT C++) 참조)로 설정을 관리하거나 위의 예제처럼 트리 노드, 콤보박스 항목, 리스트 항목, 테이블 셀 항목등에 사용자 데이터를 저장하여 활용하는 경우이다. 트리 노드에 특정한 값을 넣고 싶은데 어떤 곳은 논리값(bool), 어떤 곳은 정수, 또 다른 곳은 스트링일 수 있으니 값을 설정하고 조회하는 함수에서는 "void setData(int key, const QVariant &value)"처럼 인수 타입을 QVariant로 지정하여 다양한 타입을 간편하게 사용할 수 있도록 해주는 것이다. 단, 값을 읽을 때는 위의 예제처럼 toString(), toInt()와 같은 함수처럼 원하는 타입으로 전환해서 사용해야 한다.

 

QPersistentModelIndex, QModelIndex, QJsonDocument, QJsonArray, QJsonObject, QJsonValue, QUrl, QUuid, QEasingCurve, QRegularExpression, QRegExp, QLocale, QRectF, QRect, QLineF, QLine, QPointF, QPoint, QSizeF, QSize, QHash<QString, QVariant>, QMap<QString, QVariant>, QList<QVariant>, QDateTime, QTime, QDate,  QStringList, QString, QBitArray, QByteArray, char *, QChar, QLatin1String, float, double, bool, qulonglong, qlonglong, uint, int, QDataStream. QVariant

 

QVariant 클래스의 생성자에 적용할 수 있는 타입들은 QVariant 자체 오브젝트와 사용자 클래스를 비롯하여 위의 타입들을 사용할 수 있다.

 

toBitArray(), toBool(), toByteArray(), toChar(), toDate(), toDateTime(), toDouble(), toEasingCurve(),  toFloat(), toHash(), toInt(), toJsonArray(), toJsonDocument(), toJsonObject(), toJsonValue(), toLine(), toLineF(), toList(), toLocale(), toLongLong(), toMap(), toModelIndex(), toPersistentModelIndex(), toPoint(), toPointF(), toReal(), toRect(), toRectF(), toRegExp(), toRegularExpression(), toSize(), toSizeF(), toString(), toStringList(), toTime(), toUInt(), toULongLong(), toUrl(), toUuid()

 

마찬가지로 QVariant 오브젝트를 원래의 타입으로 전환하는 것도 위와 같이 존재한다. 원래의 타입이 아니라 다른 타입으로 변환해서 받을 수도 있는데, 예를 들면 원래는 Int 타입이었지만 Bool, QChar, Double, LongLong, QString, UInt, ULongLong 등으로 받을 수도 있다. 다만, 변환되지 않는 것이 있으므로 확인이 필요하다. 기타 함수들은 다음과 같다.

 

bool canConvert(int targetTypeId) 

bool canConvert<타입>() : 지정한 타입으로 변환 가능한지 여부 리턴

void setValue(const T &value)

bool convert(int targetTypeId) : 새 값을 설정하거나, 지정한 타입으로 변환

void clear() : 값으로 지우고 QMetaType::UnknownType로 전환

bool isValid() : QMetaType::UnknownType이 아니면 true 리턴

QVariant QVariant::fromValue(const T &value) : QVariant 생성하여 리턴

value <타입>(): 지정한 타입으로 값을 리턴. to...() 함수와 동일.

 

728x90

'C | C++' 카테고리의 다른 글

QFileDialog 클래스(QT C++)  (0) 2024.03.24
QPainter 클래스(QT C++)  (0) 2024.03.24
QSettings 클래스(QT C++)  (0) 2024.03.18
QGraphicsItem 클래스(QT C++)  (0) 2024.03.14
QGraphicsScene 클래스(QT C++)  (0) 2024.03.13