在QT的控件使用中,我们常常想在某个控件上(比如QPushButton)记录上我们一些自定义的一些属性,用作标记也好,识别符也好。 我们可以使用QObject的属性property
1、属性设置
=举个栗子=====
设置一个QPushbutton 的“rowIndex”属性,来记录这个按钮在第几行。
QPushButton *btn = new QPushButton("查看");
// i 表示是第i行。
btn->setProperty("rowIndex", i);
connect(btn, &QPushButton::clicked, this, &tableFrom::slotBtnClicked);
当点击这个按钮时候,来获取点击的是第几行的按钮。
void tableFrom::slotBtnClicked(bool checked)
{
Q_UNUSED(checked)
QPushButton *btn = dynamic_cast<QPushButton*> (sender());
if(btn != Q_NULLPTR)
{
int index = btn->property("rowIndex").toInt();
qDebug() << "clicked row index = " << index;
}
}