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



  • 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:)



  • that no one will not help me?



  • 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.



  • 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 ...



  • 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 ?



  • 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.



  • 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);
    @



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.