Find QListWidgetItem by custom widget object name
-
I am trying to find a widget item by its object name. I was able to find it by text using
findItems, but as I am using a custom widget the text superimposes on the widget:
I tried setting the pixel size to
1, but it looks like there is a dot on the list item.The cross button from the above image is connected to the parents method. I was able to send the widgets object name by doing -
emit sendMessage(this->d_ptr->q_ptr->objectName());, but for this I have to set the text of the item in the parent.Is there a better way to find the widget?
-
@akshaybabloo said in Find QListWidgetItem by custom widget object name:
The only way to findItem in QListWidget is to set the item text, but here I am using custom widget
Then you have to iterate over the items by yourself. Shouldn't be too hard to write a simple for loop.
@Christian-Ehrlicher True and this is what I did. It works:
void MainWindow::removeItem(const QString &text) { for (int i = 0; i < ui->listWidget->count(); ++i) { auto item = ui->listWidget->item(i); auto itemWidget = dynamic_cast<CustomWidget*>(ui->listWidget->itemWidget(item)); if (itemWidget->getText() == text){ delete item; break; } } } -
I am trying to find a widget item by its object name. I was able to find it by text using
findItems, but as I am using a custom widget the text superimposes on the widget:
I tried setting the pixel size to
1, but it looks like there is a dot on the list item.The cross button from the above image is connected to the parents method. I was able to send the widgets object name by doing -
emit sendMessage(this->d_ptr->q_ptr->objectName());, but for this I have to set the text of the item in the parent.Is there a better way to find the widget?
@akshaybabloo please provide a minimal workable example
-
I am trying to find a widget item by its object name. I was able to find it by text using
findItems, but as I am using a custom widget the text superimposes on the widget:
I tried setting the pixel size to
1, but it looks like there is a dot on the list item.The cross button from the above image is connected to the parents method. I was able to send the widgets object name by doing -
emit sendMessage(this->d_ptr->q_ptr->objectName());, but for this I have to set the text of the item in the parent.Is there a better way to find the widget?
Hi,
@akshaybabloo said in Find QListWidgetItem by custom widget object name:
emit sendMessage(this->d_ptr->q_ptr->objectName());
This looks a bit nasty and from the looks of it could be replaced by:
emit sendMessage(objectName());How do you get the text to draw like that ? The object name is usually not visible at all.
-
Hi,
@akshaybabloo said in Find QListWidgetItem by custom widget object name:
emit sendMessage(this->d_ptr->q_ptr->objectName());
This looks a bit nasty and from the looks of it could be replaced by:
emit sendMessage(objectName());How do you get the text to draw like that ? The object name is usually not visible at all.
@SGaist I use
setObjectNamewhen adding the widget to the list.@SGaist and @eyllanesc the example code can be found at https://github.com/akshaybabloo/qlistwidget-custom-widget.
The only way to
findItemin QListWidget is to set the item text, but here I am using custom widget. So, I had to set the item text make the font size to1, but still, you can see the tiny text to the left.
It would be nice to find the list item by its object name instead on the item text.
-
Well... You are putting a text in your item and set a widget on it which does not make much sense.
In any case, using setItemWidget like you do is going to turn to a bad idea. It's not meant to be used to fill QListWidget.
-
Well... You are putting a text in your item and set a widget on it which does not make much sense.
In any case, using setItemWidget like you do is going to turn to a bad idea. It's not meant to be used to fill QListWidget.
@SGaist okay, any suggestions?
-
@SGaist I use
setObjectNamewhen adding the widget to the list.@SGaist and @eyllanesc the example code can be found at https://github.com/akshaybabloo/qlistwidget-custom-widget.
The only way to
findItemin QListWidget is to set the item text, but here I am using custom widget. So, I had to set the item text make the font size to1, but still, you can see the tiny text to the left.
It would be nice to find the list item by its object name instead on the item text.
@akshaybabloo said in Find QListWidgetItem by custom widget object name:
The only way to findItem in QListWidget is to set the item text, but here I am using custom widget
Then you have to iterate over the items by yourself. Shouldn't be too hard to write a simple for loop.
-
@akshaybabloo said in Find QListWidgetItem by custom widget object name:
The only way to findItem in QListWidget is to set the item text, but here I am using custom widget
Then you have to iterate over the items by yourself. Shouldn't be too hard to write a simple for loop.
@Christian-Ehrlicher True and this is what I did. It works:
void MainWindow::removeItem(const QString &text) { for (int i = 0; i < ui->listWidget->count(); ++i) { auto item = ui->listWidget->item(i); auto itemWidget = dynamic_cast<CustomWidget*>(ui->listWidget->itemWidget(item)); if (itemWidget->getText() == text){ delete item; break; } } }