10

I have use setProperty function to set dynamic property to object.
But I want in other place to check if the property that was created is exists or not.

What I done:
When set the property:

QString fileDlg = QFileDialog::getOpenFileName(this, "Open File", "F://","Text Files(*.txt)");
QWidget *widget = new QWidget(this);
QMdiSubWindow *mdiWindows = ui->mdiArea->addSubWindow(widget);
mdiWindows->setProperty("filePath", fileDlg);

When check if property exists:

QMdiSubWindow *activeWindow = ui->mdiArea->activeSubWindow();
if(activeWindow->property("filePath") == true){
    // code here
}
rubenvb
  • 74,642
  • 33
  • 187
  • 332
Lion King
  • 32,851
  • 25
  • 81
  • 143

2 Answers2

9

If the property doesn't exist, the QObject::property method returns an invalid variant. This is documented.

Thus:

QVariant filePath = activeWindow->property("filePath");
if (filePath.isValid()) {
  ...
}

Side note: comparing anything to true is either completely superfluous, or a sign of broken design somewhere. You should not have ... == true nor ... == false anywhere in your code.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • I think your way is more simpler, and nearest to my code. – Lion King Jun 20 '14 at 15:36
  • Oh well, and how to distunguish between unset property and property set to `QVariant()`? – Tomáš Zato Sep 05 '16 at 18:54
  • There's no need to distinguish between them. If you think you need to, you need to use a custom data type and only wrap that in the variant. – Kuba hasn't forgotten Monica Sep 06 '16 at 20:45
  • @TomášZato An unset dynamic property is a null `QVariant`. A property that has been set will have some value, although the value itself may be null. E.g. say that we set the variant to `QString{}`. The `QVariant::isNull()` is false, but `QVariant::toString().isNull()` is true, because the variant was set with a string value that just happens to be null - the variant doesn't care about that though. From variant's viewpoint, it's either set and thus non-null, or unset and thus null. The enclosed data types may have their own "null/non-null" distinction, but it's optional. – Kuba hasn't forgotten Monica Aug 20 '18 at 14:21
2

The problem is that you are trying to check the QVariant property directly, whereas it is not even sure if it exists in your case.

I would personally use either of the solutions below depending on your exact context in the real program.

Replace the variable placeholder with your preferred property name if you wish.

QVariant myPropertyValue =
    ui->mdiArea->activeSubWindow()->property(myPropertyName);
if(myPropertyValue.isValid())
    qDebug() << myPropertyName << "exists.";

or:

QList<QByteArray> dynamicPropertyNames =
    ui->mdiArea->activeSubWindow()->dynamicPropertyNames();
if (dynamicPropertyNames.contains(myPropertyName))
    qDebug() << myPropertyName << "exists.";
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Thank you for your answer. I'm also benefited from your code. – Lion King Jun 20 '14 at 15:37
  • @LionKing: nice, by the way, I was thinking about that while cycling home that you could probably also query the property directly and see if it exists that way. I updated the answer. – László Papp Jun 20 '14 at 16:01