×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Contributor
    Join Date
    Feb 2009
    Location
    India
    Posts
    123

    QMenu Item Color

    Hi,

    I have created popup menu to appear on button click event i.e when i click on xyz button a pop up menu appears.
    Now the problem is, I want to set Background color of Individual Menu Item..
    I have searched for code and I think this can be done using stylesheet
    so I implemented code

    QMenu::item {
    padding: 2px 25px 2px 20px;
    border: 1px solid transparent; /* reserve space for selection border */
    }

    QMenu::item:selected {
    border-color: darkblue;
    background: rgba(100, 100, 100, 150);
    }


    But Nothing happens I think I am doing it in Wrong way can any one help me with this issue

    This is my stylesheet code:
    menu->setStyleSheet("*{background-color:rgb(225,225,225);color:rgb(0,0,0);selection-color:rgb(0, 170, 255);font-size:5pt}");

    Regards,
    Vishal

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QMenu Item Color

    Some important relevant information is missing in your question.

  3. #3
    Regular Contributor
    Join Date
    Feb 2009
    Location
    India
    Posts
    123

    Re: QMenu Item Color

    Quote Originally Posted by divanov View Post
    Some important relevant information is missing in your question.

    Sry i could not understand but then also ..Once again this is my Problem


    I want to set Background color of Individual Menu Item..

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QMenu Item Color

    You are so concentrated on your problem that forget to tell us how did you run into that problem.
    For example, you say particularly nothing about applying "QMenu::item" rules.

  5. #5
    Nokia Developer Expert
    Join Date
    Feb 2008
    Posts
    183

    Re: QMenu Item Color

    QS60Style support for stylesheets was fixed for Qt 4.6.0. So make sure that you are using 4.6.0 or newer (prereleases won't work).

    I have the following code that seems to style sheet menu successfully:

    Code:
        QString menuStyle(
            "QMenu { background-color:rgb(225,225,0); color:rgb(255,0,0); selection-color:rgb(0, 170, 255); font-size:2pt} \n"
            "QMenu::item:selected { background-color: #654321; }"
            );
    
        QMenu *menu1 = new QMenu("Menu", this);
        QAction *actStyled1 = new QAction("MenuItem1", this);
        QAction *actStyled2 = new QAction("MenuItem2", this);
        actStyled1->setCheckable(true);
        actStyled2->setCheckable(true);
        menu1->addAction(actStyled1);
        menu1->addAction(actStyled2);
        menu1->setStyleSheet(menuStyle);
    
        QToolButton *toolButton = new QToolButton(this);
        toolButton ->setText("ToolButton");
        toolButton ->setMenu(menu1);
        toolButton ->setPopupMode(QToolButton::MenuButtonPopup);
        toolButton ->setStyleSheet(toolButtonStyle);
        m_layout->addWidget(toolButton );
    So stylesheet for QMenus in general seem to work. If you could provide more details of your failing case that would help.
    For example a mini-app where the case can easily be reproduced.

    Thanks,
    FB

  6. #6
    Regular Contributor
    Join Date
    Feb 2009
    Location
    India
    Posts
    123

    Re: QMenu Item Color

    Quote Originally Posted by divanov View Post
    You are so concentrated on your problem that forget to tell us how did you run into that problem.
    For example, you say particularly nothing about applying "QMenu::item" rules.
    Sry I have no idea how to apply "QMenu::item" rules.

    Plz can u let me know

    Regards

    Vishal

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QMenu Item Color

    Quote Originally Posted by vishalrocks101 View Post
    Sry I have no idea how to apply "QMenu::item" rules.

    Plz can u let me know

    Regards

    Vishal
    Style rules are applied with help of setStyleSheet() method.

  8. #8
    Regular Contributor
    Join Date
    Feb 2009
    Location
    India
    Posts
    123

    Re: QMenu Item Color

    Quote Originally Posted by Fuzzbender View Post
    QS60Style support for stylesheets was fixed for Qt 4.6.0. So make sure that you are using 4.6.0 or newer (prereleases won't work).

    I have the following code that seems to style sheet menu successfully:

    Code:
        QString menuStyle(
            "QMenu { background-color:rgb(225,225,0); color:rgb(255,0,0); selection-color:rgb(0, 170, 255); font-size:2pt} \n"
            "QMenu::item:selected { background-color: #654321; }"
            );
    
        QMenu *menu1 = new QMenu("Menu", this);
        QAction *actStyled1 = new QAction("MenuItem1", this);
        QAction *actStyled2 = new QAction("MenuItem2", this);
        actStyled1->setCheckable(true);
        actStyled2->setCheckable(true);
        menu1->addAction(actStyled1);
        menu1->addAction(actStyled2);
        menu1->setStyleSheet(menuStyle);
    
        QToolButton *toolButton = new QToolButton(this);
        toolButton ->setText("ToolButton");
        toolButton ->setMenu(menu1);
        toolButton ->setPopupMode(QToolButton::MenuButtonPopup);
        toolButton ->setStyleSheet(toolButtonStyle);
        m_layout->addWidget(toolButton );
    So stylesheet for QMenus in general seem to work. If you could provide more details of your failing case that would help.
    For example a mini-app where the case can easily be reproduced.

    Thanks,
    FB


    Hi,

    FB thanks for reply,this style is working for me,, can u tell me how I can set background for INDIVIDUAL action added On MENU. Is this possible ??

    For Eg:

    menu1->addAction(actStyled1); have different background color && menu1->addAction(actStyled2); have different background color

    Regards,

    Vishal

  9. #9
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QMenu Item Color

    Quote Originally Posted by vishalrocks101 View Post
    menu1->addAction(actStyled1); have different background color && menu1->addAction(actStyled2); have different background color

    Regards,

    Vishal
    I think it's not doable because QAction isn't QWidget.

  10. #10
    Regular Contributor
    Join Date
    Feb 2009
    Location
    India
    Posts
    123

    Re: QMenu Item Color

    Quote Originally Posted by divanov View Post
    I think it's not doable because QAction isn't QWidget.
    hi,

    Divanov thnks for reply , but can we set a widget for eg: QLabel as a menu Item

    and set its background color.

    Regards,

    Vishal

  11. #11
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QMenu Item Color

    Yes, you can:
    Code:
    #include <QApplication>
    #include <QMainWindow>
    #include <QWidgetAction>
    #include <QMenuBar>
    #include <QMenu>
    #include <QLabel>
    #include <QStyle>
    #include <QDebug>
    
    int
    main (int argc,
          char **argv)
    {
        QApplication app(argc, argv);
        app.setStyleSheet("QLabel#label1 { background-color: blue }" \
                          "QLabel#label2 { background-color: yellow }");
    
        QMainWindow win;
    
        QLabel *label1 = new QLabel("Action1");
        label1->setObjectName("label1");
        QWidgetAction action1(&win);
        action1.setDefaultWidget(label1);
    
        QLabel *label2 = new QLabel("Action2");
        label2->setObjectName("label2");
        QWidgetAction action2(&win);
        action2.setDefaultWidget(label2);
    
        QMenu menu("Edit");
        menu.addAction(&action1);
        menu.addAction(&action2);
    
        win.menuBar()->addMenu(&menu);
        win.show();
    
        return app.exec();
    }

  12. #12
    Regular Contributor
    Join Date
    Feb 2009
    Location
    India
    Posts
    123

    Re: QMenu Item Color

    Quote Originally Posted by divanov View Post
    Yes, you can:
    Code:
    #include <QApplication>
    #include <QMainWindow>
    #include <QWidgetAction>
    #include <QMenuBar>
    #include <QMenu>
    #include <QLabel>
    #include <QStyle>
    #include <QDebug>
    
    int
    main (int argc,
          char **argv)
    {
        QApplication app(argc, argv);
        app.setStyleSheet("QLabel#label1 { background-color: blue }" \
                          "QLabel#label2 { background-color: yellow }");
    
        QMainWindow win;
    
        QLabel *label1 = new QLabel("Action1");
        label1->setObjectName("label1");
        QWidgetAction action1(&win);
        action1.setDefaultWidget(label1);
    
        QLabel *label2 = new QLabel("Action2");
        label2->setObjectName("label2");
        QWidgetAction action2(&win);
        action2.setDefaultWidget(label2);
    
        QMenu menu("Edit");
        menu.addAction(&action1);
        menu.addAction(&action2);
    
        win.menuBar()->addMenu(&menu);
        win.show();
    
        return app.exec();
    }
    Hi,
    Divanov thanks for help,,this worked,now i can have different background color for individual menu items...

    But there is one problem how I can perform actions on label..??
    For Eg:when I click on menu.addAction(&action1); Item application should Close.. Because I think we cannot perform connect Action on Labels

    Regards,
    Vishal

  13. #13
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QMenu Item Color

    Quote Originally Posted by vishalrocks101 View Post
    Hi,
    Divanov thanks for help,,this worked,now i can have different background color for individual menu items...

    But there is one problem how I can perform actions on label..??
    For Eg:when I click on menu.addAction(&action1); Item application should Close.. Because I think we cannot perform connect Action on Labels

    Regards,
    Vishal
    You should connect your slot to QAction::triggered signal emitted on QWidgetAction object as you would do with usual QAction object.

  14. #14
    Regular Contributor
    Join Date
    Feb 2009
    Location
    India
    Posts
    123

    Re: QMenu Item Color

    Quote Originally Posted by divanov View Post
    You should connect your slot to QAction::triggered signal emitted on QWidgetAction object as you would do with usual QAction object.
    Hi,
    Divanov thanks for ur help ,,now i can perform action on label Item sucessfully,,

    But when I roll over Particular Label Item using up down navigation key of emulator..I cannot get ,, which Item is selected because Label Items donot get Highlighted. Insted I have Provided StyleSheet Information
    For Eg:QLabel *menulabel =new QLabel("\t ITEM 1");

    style sheet for label item menulabel->setStyleSheet("*{background-color:rgb(208, 208, 208);font-size:4pt;selection-color:rgb(0, 85, 127)}");

    style sheet for menu
    menu->setStyleSheet("*{background-color:rgb(225,225,225);color:rgb(0,0,0);selection-color:rgb(0, 170, 255);font-size:4pt;selection-background-color:rgb(0, 85, 127)}");

    Regards,

    Vishal

  15. #15
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QMenu Item Color

    Selected QAction can be styled with a following rule:
    Code:
    QMenu::item:selected { background-color: red }
    However, the same approach doesn't work for QWidgetAction.

Similar Threads

  1. Setting Item List text color
    By em75dl07 in forum Symbian User Interface
    Replies: 2
    Last Post: 2010-04-16, 00:24
  2. Color in Table Item?
    By quoctrang in forum Mobile Java Tools & SDKs
    Replies: 2
    Last Post: 2008-12-24, 16:27
  3. how to change the color of the Highlight item in the listbox
    By RaveendraB in forum Symbian User Interface
    Replies: 8
    Last Post: 2008-02-28, 17:49
  4. Change color of listbox item
    By anoopd in forum Symbian User Interface
    Replies: 2
    Last Post: 2005-03-09, 08:33
  5. getting menu item highlight color
    By gaurav_wirkle in forum Mobile Java General
    Replies: 1
    Last Post: 2005-01-15, 10:07

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×