Solved Retrieve a QWidget inside QTableWidgetItem
-
My app has a QTableWidget in wich in some cell was added another widget like this:
QWidget *cWidget = new QWidget(); WidgetColorPicker *pColor= new WidgetColorPicker(); QHBoxLayout *cLayout = new QHBoxLayout(cWidget); cLayout->addWidget(pColor); cLayout->setAlignment(Qt::AlignLeft); cLayout->setContentsMargins(0,0,0,0); cWidget->setLayout(cLayout); ui->channelsTable->setCellWidget(i,1,cWidget); connect(pColor, SIGNAL(colorSelectionChanged(QColor)), this, SLOT(plotColorChanged(QColor)));
An event is triggered and one of the color picker, for get what widget was, it needs to get the color picker that was the sender in a for loop, but I dont know of retrieve this from the QTableWidgetItem
WidgetColorPicker *widget = (WidgetColorPicker*)sender(); for (int i = 0; i < ui->channelsTable->rowCount(); i++) { QTableWidgetItem *it= ui->channelsTable->item(i,1); if (it ->????? == widget) { return i; } }
-
You can either use QTableWidget::cellWidget() or use the new signal/slot syntax and pass the widget directly via a lambda.
-
@Christian-Ehrlicher
I test with cellWidget, and use findChildWidgetColorPicker *widget = (WidgetColorPicker*)sender(); for (int i = 0; i < ui->channelsTable->rowCount(); i++) { QWidget *it= ui->channelsTable->cellWidget(i,1); if(widget == it->findChild<WidgetColorPicker*>("pColor")) { qDebug()<<"ROWW="<<i; } }
but cant find the sender qwidget, never enter the if sentence. What is wrong??
-
Hi
The variable name is pColor
you dont seem to called
pColor->setObjectName("pColor")so that is why it cant find it
However, why not just do
it->findChild<WidgetColorPicker*>()
since there should only be one WidgetColorPicker in that widget, correct ?
But is the logic not wrong ?
The cellWidget returns the holding QWidget.
and you compare that to the inner WidgetColorPicker ?
i would think you want
if( widget == it )
like is the widget i get from sender() the same as i get from CellWidget
so you can know the row index ? -
@mrjj
Hi.
Ok, Im sorry, you are all right, the concept of variable name, and object name are new for me, I miss it completly.
The commandit->findChild<WidgetColorPicker*>()
Works fine, the name object was the problem.
In other hand, the comparation in if sentence is correct,
if(widget == it->findChild<WidgetColorPicker*>())
because "it" is not the WidgetColorPicker, is the father that is directly inserted in the cell.
Then, the child of "it" is the WidgetColorPicker that send the signal. Remembering that in the code of TableWidget the color picker is put in a layout, the layout inside another widget, and this widget in the table cell.Retaking the main issue, yes, the command it->findChild<WidgetColorPicker*>() works fine, arealdy get the row number correctly. The color picker will be used for change the color of the plot, of the logic signal analizer. Yes, there is the same project that mentioned in other questions.
Thank you very much, also thanks of @Christian-Ehrlicher for the first tip. Problem resolved.
-
Np, I also forget it sometimes as the Designer gives them names
but when you insert from code they don't have:)Oh yes you are right.
The sender is the ofc the WidgetColorPicker. My bad :)just as a note:
If your list is stable, meaning you adds the rows and don't delete any or insert new
we could just have stuff the index via
a dynamic propertypColor->setProperty("myindex", i);
and in the slot
int index = widget->property("myindex").toInt();