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. Trying to control individual QMenu titles color

Trying to control individual QMenu titles color

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 3.5k Views
  • 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.
  • H Offline
    H Offline
    hashtagqt
    wrote on last edited by hashtagqt
    #1

    I wonder if I can do this programmatically (change the color of a menu title):
    This should explain what I want:
    picture

    Cobra91151C 1 Reply Last reply
    0
    • H hashtagqt

      I wonder if I can do this programmatically (change the color of a menu title):
      This should explain what I want:
      picture

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #2

      @hashtagqt

      Yes, you can do it using Qt style sheets.

      Example code:

      QMenu {
          background-color: #ABABAB; /* sets background of the menu */
          border: 1px solid black;
      }
      
      QMenu::item {
          /* sets background of menu item. set this to something non-transparent
              if you want menu color and menu item color to be different */
          background-color: transparent;
      }
      
      QMenu::item:selected { /* when user selects item using mouse or keyboard */
          color: #FF0000; // for red color
          background-color: #654321;
      }
      

      More details here - Qt Style Sheets Examples

      Also you can set your style sheet for specific menu item using setObjectName("myMenuItem") function.

      Usage:

      QMenu::item#myMenuItem {
           color: #FF0000; // for red color
      }
      
      1 Reply Last reply
      0
      • H Offline
        H Offline
        hashtagqt
        wrote on last edited by hashtagqt
        #3

        That does change the color of all the children of the "View" menu. I just want to change the color of the "View" label itself, within the top level menu bar.

        Cobra91151C 1 Reply Last reply
        0
        • H hashtagqt

          That does change the color of all the children of the "View" menu. I just want to change the color of the "View" label itself, within the top level menu bar.

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #4

          @hashtagqt

          Try to setObjectName("myMenuItem") function to appropriate view and style:

          QMenu::item#myMenuItem {
               color: #FF0000; // for red color
          }
          

          or try this for top level menu:

          QMenu#myMenuItem {
               color: #FF0000; // for red color
          }
          

          If you can't solve your problem then post some code so I can help you.

          H 1 Reply Last reply
          0
          • Cobra91151C Cobra91151

            @hashtagqt

            Try to setObjectName("myMenuItem") function to appropriate view and style:

            QMenu::item#myMenuItem {
                 color: #FF0000; // for red color
            }
            

            or try this for top level menu:

            QMenu#myMenuItem {
                 color: #FF0000; // for red color
            }
            

            If you can't solve your problem then post some code so I can help you.

            H Offline
            H Offline
            hashtagqt
            wrote on last edited by
            #5

            @Cobra91151 , thanks for your help!
            I have a top level menu bar called m_menuBar.
            That menu has (among others) a QMenu called menuView, with the title "View".
            menuView has 3 actions.

            when I do this in code:
            menuView->setObjectName("ColorThis");
            m_menuBar->setStyleSheet("QMenu::item#ColorThis {color: #FF0000;}");

            The three actions under menuView get painted in red, but the title "View" stays regular (black).

            Cobra91151C 1 Reply Last reply
            0
            • H hashtagqt

              @Cobra91151 , thanks for your help!
              I have a top level menu bar called m_menuBar.
              That menu has (among others) a QMenu called menuView, with the title "View".
              menuView has 3 actions.

              when I do this in code:
              menuView->setObjectName("ColorThis");
              m_menuBar->setStyleSheet("QMenu::item#ColorThis {color: #FF0000;}");

              The three actions under menuView get painted in red, but the title "View" stays regular (black).

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #6

              @hashtagqt

              Ok. Try to set style sheet like this:

              menuView->setStyleSheet("color: #FF0000;");

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hashtagqt
                wrote on last edited by
                #7

                Same exact outcome.

                1 Reply Last reply
                0

                • Login

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