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. [solved] Set the style to a file on QPushButton->QMenu

[solved] Set the style to a file on QPushButton->QMenu

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 4.8k Views 1 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.
  • B Offline
    B Offline
    BlackJack
    wrote on last edited by
    #1

    Hi all.
    I have many QPushButton to ContexMenu
    @
    ui->buttonFile->setContextMenuPolicy(Qt::PreventContextMenu);
    QMenu* menuFile = new QMenu;
    menuFile->addAction("&Open");
    menuFile->addAction("&Save");
    menuFile->addAction("&Exit");
    ui->buttonFile->setMenu(menuFile);
    @

    All styles are connected to a file "style.qss" and why it can not no how to set styles on the menu
    @QMenu{
    border:none;
    background:qlineargradient(x1:0, y1:0, x2:0, y2:1,stop:0 #282a2b, stop: 0.5 #1c1c1d,stop: 0.6 #1f2020, stop:1 #1c1d1e);
    color:#c6caf3;
    }
    QMenu::item{
    border:none;
    padding:0 7px 0 7px;
    background:url(:/image/black/menu.png);
    background-repeat:repeat-x;
    background-position:center center;
    color:#c6caf3;
    }
    QMenu::item:selected{
    border:none;
    background:url(:/image/black/menu_active.png);
    background-repeat:repeat-x;
    background-position:center center;
    color:#c6caf3;
    }@

    obtained only if the style is set to do so
    @
    menuFile->setStyleSheet(
    "QMenu{"
    "border:none;"
    "background:qlineargradient(x1:0, y1:0, x2:0, y2:1,stop:0 #282a2b, stop: 0.5 #1c1c1d,stop: 0.6 #1f2020, stop:1 #1c1d1e);"
    "color:#c6caf3;"
    "}"
    "QMenu::item{"
    "height:20px;"
    "padding:0 7px 0 7px;"
    "border:none;"
    "background:url(:/image/black/menu.png);"
    "background-repeat:repeat-x;"
    "background-position:center center;"
    "color:#c6caf3;"
    "}"
    "QMenu::item:selected{"
    "border:none;"
    "background:url(:/image/black/menu_active.png);"
    "background-repeat:repeat-x;"
    "background-position:center center;"
    "color:#c6caf3;"
    "}");
    @

    I would like to have all the styles that were in the file...

    thanks in advance:)

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BlackJack
      wrote on last edited by
      #2

      that no one will not help me?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on last edited by
        #3

        I don't really understand your problem. Do you want to read the style settings from the .qss file, or are you asking if there is a way to apply the settings specified in the .qss file to a widget or application or whatever ?

        Read from file:
        @
        QFile stylesheetFile (a_stylesheetFilePath);
        if(stylesheetFile.open(QIODevice::ReadOnly))
        {
        const QByteArray content = stylesheetFile.readAll();
        QString contentString (content);
        menuFile->setStyleSheet(contentString);
        stylesheetFile.close();
        }
        @

        Since I have not used .qss files up until now I don't know if there is a more elegant way to apply the settings from a .qss file to a widget. I suggest you take a look at "the docs":http://developer.qt.nokia.com/doc/qt-4.8/qwidget.html#styleSheet-prop yourself.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BlackJack
          wrote on last edited by
          #4

          I have all the styles are in the css file I am using them to install "setStyleSheet" the whole widget, like this

          @int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          Widget w;

          QFile qss(":/blackStyle.qss");
          qss.open(QFile::ReadOnly);
          w.setStyleSheet(qss.readAll());
          qss.close();
          
          w.show();
          
          return a.exec();
          

          }
          @
          I need to apply the settings from the file. for the whole widget settings are, and why we do not want on the menu ...

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            So if I understand correctly, the settings from the .qss file are only applied to your widget but not to the menu inside that widget when using the code you posted above ?

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BlackJack
              wrote on last edited by
              #6

              Superimposed on the widget styles from a file, in Widget has a button on which I create a contex menu.
              Yes, code in posted above.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on last edited by
                #7

                I don't know if the style of a widget is passed down to its child objects. I would asume this would make sense, but in your case this doesn't seem to work. Have you tried to retrieve the style from your widget and then pass it to your menu when you create it?

                Something like this:
                @
                ui->buttonFile->setContextMenuPolicy(Qt::PreventContextMenu);
                QMenu* menuFile = new QMenu;
                menuFile->addAction("&Open");
                menuFile->addAction("&Save");
                menuFile->addAction("&Exit");
                QString style = styleSheet();
                menuFile->setStyleSheet(style);
                ui->buttonFile->setMenu(menuFile);
                @

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BlackJack
                  wrote on last edited by
                  #8

                  Thank you. tried this option, the shift is there, but not as what I would like to ... Why do not you apply that style that I would like ... I will continue to have to deal with the file QSS.

                  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