Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QIcon setting and icon then getting name of existing set icon
Qt 6.11 is out! See what's new in the release blog

QIcon setting and icon then getting name of existing set icon

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 714 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    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**

    Kind Regards,
    Sy

    eyllanescE Thank YouT 2 Replies Last reply
    0
    • SPlattenS SPlatten

      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**

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #3

      @SPlatten

      1. You should not abuse pointers, it is enough to use:
      QIcon mpicnConnected(":/ICN_CONNTD");
      mpcboTraineeToView->setItemIcon(index, mpicnConnected);
      

      Read abour memory-leak

      1. 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;
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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

        1 Reply Last reply
        0
        • SPlattenS SPlatten

          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**

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by eyllanesc
          #3

          @SPlatten

          1. You should not abuse pointers, it is enough to use:
          QIcon mpicnConnected(":/ICN_CONNTD");
          mpcboTraineeToView->setItemIcon(index, mpicnConnected);
          

          Read abour memory-leak

          1. 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;
          

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply
          2
          • SPlattenS SPlatten

            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**

            Thank YouT Offline
            Thank YouT Offline
            Thank You
            wrote on last edited by
            #4

            @SPlatten

            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

            Let's make QT free or It will go forever

            TRUE AND FALSE <3

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved