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. This if statment has me confused
Forum Updated to NodeBB v4.3 + New Features

This if statment has me confused

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 296 Views 2 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.
  • C Offline
    C Offline
    Circuits
    wrote on last edited by
    #1

    Can someone break this down into an if else statement so I know what's going on here?

    color: control.enabled ? control.down ? themeManager.currentTheme.palette.highlightColor : control.flat? "transparent" : control.isPrimary ? control.style.primaryColor : control.style.fillColor: "grey"
    

    I just recently started using these alternate forms of the if/else statement and I am not that familiar with how they work when they become more complex. Here is my attempt but I am just guessing:

    if(control.enabled || control.down)
    {
      themeManager.currentTheme.palette.highlightColor
    }
    else
    {
      if(control.flat)
      {
        "transparent"
       }
       else
       {
         if(control.isPrimary)
         {
           control.style.primaryColor
         }
         else
         {
           control.style.fillColor
         }
    
    	... here is where I am lost
       }
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      What a monster line! This code is in bad need of refactoring :-)

      (something)? always means if (something) and : always means else. So:

      color: {
      if (control.enabled) {
        if (control.down) {
          return themeManager.currentTheme.palette.highlightColor;
        } else {
          if (control.flat) {
            return "transparent";
          } else {
            if (control.isPrimary) {
              return control.style.primaryColor;
            } else {
              return control.style.fillColor;
            }
          }
        }
      } else {
        return "grey";
      }
      }
      

      It can be also expressed in other ways, of course (nested if is more or less the same as && etc.).

      (Z(:^

      C 1 Reply Last reply
      6
      • sierdzioS sierdzio

        What a monster line! This code is in bad need of refactoring :-)

        (something)? always means if (something) and : always means else. So:

        color: {
        if (control.enabled) {
          if (control.down) {
            return themeManager.currentTheme.palette.highlightColor;
          } else {
            if (control.flat) {
              return "transparent";
            } else {
              if (control.isPrimary) {
                return control.style.primaryColor;
              } else {
                return control.style.fillColor;
              }
            }
          }
        } else {
          return "grey";
        }
        }
        

        It can be also expressed in other ways, of course (nested if is more or less the same as && etc.).

        C Offline
        C Offline
        Circuits
        wrote on last edited by
        #3

        @sierdzio Thanks, I appreciate that!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          One other to analyse this line is to break it in more "logical unit":

          control.enabled ? 
              control.down ? 
                  themeManager.currentTheme.palette.highlightColor 
                  : control.flat ? 
                      "transparent" 
                      : control.isPrimary ? 
                          control.style.primaryColor 
                          : control.style.fillColor
              : "grey"
          

          It is still not pretty but more manageable to get around.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          • Login

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