QT property属性の応用
1201 ワード
QTのコントロールの使用では、QPShButtonなどのコントロールにカスタム属性を記録し、タグとしても識別子としても使用したいことがよくあります.QObjectのプロパティpropertyを使用できます
QObjectベースのコントロールでは、setPropertyでこのコントロールのプロパティを設定できます.
説明すると、パラメータnameはカスタム属性名であり、コントロールのデフォルト属性名と同じではないことに注意してください.valueこのプロパティの値.
使用するときはpropertyを使用してプロパティの値を取得します.
=========================================
QPShbuttonの「rowIndex」プロパティを設定して、このボタンを数行目に記録します.
このボタンをクリックすると、クリックしたのは数行目のボタンです.
QObject::property
QObjectベースのコントロールでは、setPropertyでこのコントロールのプロパティを設定できます.
bool QObject::setProperty(const char *name, const QVariant &value)
説明すると、パラメータnameはカスタム属性名であり、コントロールのデフォルト属性名と同じではないことに注意してください.valueこのプロパティの値.
使用するときはpropertyを使用してプロパティの値を取得します.
QVariant QObject::property(const char *name) const
=========================================
QPShbuttonの「rowIndex」プロパティを設定して、このボタンを数行目に記録します.
QPushButton *btn = new QPushButton(" ");
btn->setProperty("rowIndex", i);
connect(btn, &QPushButton::clicked, this, &tableFrom::slotBtnClicked);
このボタンをクリックすると、クリックしたのは数行目のボタンです.
void tableFrom::slotBtnClicked(bool checked)
{
Q_UNUSED(checked)
QPushButton *btn = dynamic_cast (sender());
if(btn != Q_NULLPTR)
{
int index = btn->property("rowIndex").toInt();
qDebug() << "clicked row index = " << index;
}
}