If the rows can't be moved I would suggest to use a property:
Example for plain structure, use complex index for tree structure:
QPushButton btn=new QPushButton();
btn->setProperty("row", ui->treeWidget->topLevelItemCount());
void OnPreview()
{
int row = sender()->property("row").toInt();
QTreeWidgetItem* item = ui->treeWidget->topLevelItem(row);
}
Otherwise, if rows amount if not too large you can loop by items to find out which of them contains the button:
Example for plain structure, use complex index for tree structure:
void OnPreview()
{
for(int i = 0 ; i < ui->treeWidget->topLevelItemCount() ; i++)
{
QTreeWidgetItem* item = ui->treeWidget->topLevelItem(i);
if (ui->treeWidget->itemWidget(item, 0) == sender())
{
...
}
}
}
If you have great amount of rows and they can be moved, create a hash table to match buttons and items:
QHash<QObject*, QTreeWidgetItem*> hash;
QPushButton* btn=new QPushButton();
QTreeWidgetItem *Items=new QTreeWidgetItem(ui->treeWidget);
hash[btn] = Items;
void OnPreview()
{
QTreeWidgetItem* item = hash[sender()];
...
}
OnPreview()
slot, or keep mapping between button and the index it assigned to. – vahancho