QIcon setting and icon then getting name of existing set icon
-
I am setting icons in a QComboBox, the icons are created with:
mpicnConnected = new QIcon(":/ICN_CONNTD"); mpicnNotConnected = new QIcon(":/ICN_DISCONTD");I set the icons in the combo using the setItemIcon function, this all works, what I want to do is determine what the existing icon is from the selected item in the combo, I get the icon with:
int intIndex(mpcboTraineeToView->findText(crstrHostname)); if ( intIndex == -1 ) { return; } QIcon currentIcon(mpcboTraineeToView->itemIcon(intIdex));currentIcon does return an object, but the name function returns an emtpy string, how can I determine what icon has been set which can only be either: mpicnConnected or ** mpicnNotConnected**
-
I am setting icons in a QComboBox, the icons are created with:
mpicnConnected = new QIcon(":/ICN_CONNTD"); mpicnNotConnected = new QIcon(":/ICN_DISCONTD");I set the icons in the combo using the setItemIcon function, this all works, what I want to do is determine what the existing icon is from the selected item in the combo, I get the icon with:
int intIndex(mpcboTraineeToView->findText(crstrHostname)); if ( intIndex == -1 ) { return; } QIcon currentIcon(mpcboTraineeToView->itemIcon(intIdex));currentIcon does return an object, but the name function returns an emtpy string, how can I determine what icon has been set which can only be either: mpicnConnected or ** mpicnNotConnected**
- You should not abuse pointers, it is enough to use:
QIcon mpicnConnected(":/ICN_CONNTD"); mpcboTraineeToView->setItemIcon(index, mpicnConnected);Read abour memory-leak
- There is no way to do that but an alternative is to use userData to store the name:
QIcon mpicnConnected(":/ICN_CONNTD"); mpcboTraineeToView->setItemIcon(index, mpicnConnected); mpcboTraineeToView->setItemData(index, ":/ICN_CONNTD");Then
int intIndex(mpcboTraineeToView->findText(crstrHostname)); if ( intIndex == -1 ) { return; } QString data = mpcboTraineeToView->itemData(index).toString(); qDebug() << data; -
Hi
I dont think you can get the name from the icon constructed as you have shown.
https://doc.qt.io/qt-5/qicon.html#name -
I am setting icons in a QComboBox, the icons are created with:
mpicnConnected = new QIcon(":/ICN_CONNTD"); mpicnNotConnected = new QIcon(":/ICN_DISCONTD");I set the icons in the combo using the setItemIcon function, this all works, what I want to do is determine what the existing icon is from the selected item in the combo, I get the icon with:
int intIndex(mpcboTraineeToView->findText(crstrHostname)); if ( intIndex == -1 ) { return; } QIcon currentIcon(mpcboTraineeToView->itemIcon(intIdex));currentIcon does return an object, but the name function returns an emtpy string, how can I determine what icon has been set which can only be either: mpicnConnected or ** mpicnNotConnected**
- You should not abuse pointers, it is enough to use:
QIcon mpicnConnected(":/ICN_CONNTD"); mpcboTraineeToView->setItemIcon(index, mpicnConnected);Read abour memory-leak
- There is no way to do that but an alternative is to use userData to store the name:
QIcon mpicnConnected(":/ICN_CONNTD"); mpcboTraineeToView->setItemIcon(index, mpicnConnected); mpcboTraineeToView->setItemData(index, ":/ICN_CONNTD");Then
int intIndex(mpcboTraineeToView->findText(crstrHostname)); if ( intIndex == -1 ) { return; } QString data = mpcboTraineeToView->itemData(index).toString(); qDebug() << data; -
I am setting icons in a QComboBox, the icons are created with:
mpicnConnected = new QIcon(":/ICN_CONNTD"); mpicnNotConnected = new QIcon(":/ICN_DISCONTD");I set the icons in the combo using the setItemIcon function, this all works, what I want to do is determine what the existing icon is from the selected item in the combo, I get the icon with:
int intIndex(mpcboTraineeToView->findText(crstrHostname)); if ( intIndex == -1 ) { return; } QIcon currentIcon(mpcboTraineeToView->itemIcon(intIdex));currentIcon does return an object, but the name function returns an emtpy string, how can I determine what icon has been set which can only be either: mpicnConnected or ** mpicnNotConnected**
If others didn't work then try using QMap
QMap<QString,QString> icons; // first one for text or item no and second for path icons.insert("first selection", ":/ICN_CONNTD"); icons.insert("second selection", ":/SECOND"); // and on selection change you can simply get the icon using selectionChanged(QString text){ QString iconName = icons.value(text); // Here you can access the icon name }I don't think as good answer but it can get work done. LOL
Thanks