Set palette for toolbar in MainWindow



  • Hello,

    I want to set a palette for the toolbar of my QMainWindow and I don't see why it is not working, I hope someone here can help me :) !

    Here is my code (QT5)-:

    @#include "Mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    Fenetrep *zoneCentrale = new Fenetrep;

    // Bouton de retour a la page precedente
    QPixmap imagePrec;
    QPushButton *vignettePrec = new QPushButton ();
    imagePrec.load("precedent.png");
    QIcon ButtonIconPrec(imagePrec);
    vignettePrec->setIcon(ButtonIconPrec);
    vignettePrec->setIconSize(QSize(105,105));
    
    // Bouton de retour a l'accueil
     QPixmap imageAccueil;
     QPushButton *vignetteAccueil = new QPushButton ();
     imageAccueil.load("home.png");
     QIcon ButtonIconAccueil(imageAccueil);
     vignetteAccueil->setIcon(ButtonIconAccueil);
     vignetteAccueil->setIconSize(QSize(105,105));
    
     //Titre de la fenetre
     QFont f( "Arial", 15, QFont::Bold);
     QLabel *titre = new QLabel("<font color=\"#FFFFFF\">Bienvenue sur EDAI !</font>");
     titre->setFont(f);
    
    // Création de la barre d'outils avec les deux boutons et le titre (à passer en parametre)
    QToolBar *toolBarFichier = addToolBar("Fichier");
    toolBarFichier->addWidget(vignettePrec);
    QWidget *spacerWidget = new QWidget(this);
    spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    spacerWidget->setVisible(true);
    QWidget *spacerWidget2 = new QWidget(this);
    spacerWidget2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    spacerWidget2->setVisible(true);
    toolBarFichier->addWidget(spacerWidget);
    toolBarFichier->addWidget(titre);
    toolBarFichier->addWidget(spacerWidget2);
    toolBarFichier->addWidget(vignetteAccueil);
    
    //Style de la barre d'outils <--------------------------------------------------------------------------------Not Working ?
    QPalette Pal;
    QLinearGradient linearGradientOutils(QPointF(0, 0), QPointF(0, 110));
    linearGradientOutils.setColorAt(0, Qt::blue);
    linearGradientOutils.setColorAt(0.5, Qt::darkBlue);
    linearGradientOutils.setColorAt(1, Qt::blue);
    Pal.setBrush(QPalette::Window,*(new QBrush(linearGradientOutils)));
    toolBarFichier->setAutoFillBackground(true);
    toolBarFichier->setPalette(Pal);
    
    
    connect(vignettePrec, SIGNAL(clicked()), qApp, SLOT(quit()));
    
    setCentralWidget(zoneCentrale);
    setWindowState(this->windowState() ^ Qt::WindowMaximized);--
    

    }@


  • Moderators

    Have you tried using a lineargradient element in a qss stylesheet applied to the toolbar? I haven't tried it myself, but it may work for you.



  • Thanks it seems to be working with stylesheet !
    While you are here, my other problem is that the palette of the central widget does not show when it is in the QMainWIndow but is working separately in the widget Fenetrep (defined in the beginning) ?

    I will try to use a stylesheet also but if it is another problem...



  • Just to understand your problem:
    You made zoneCentral the central Widget and cannot access the palette of zoneCentral even if you select it in the object tree?



  • The problem is that when I display the main window, only a part of the stylesheet of the central widget (defined the constructor of Fenetrep-) is displayed.
    The main Window :
    !http://img15.hostingpics.net/pics/936078mainwindow.png(MainWindow)!
    The widget display on its own : --
    !http://img15.hostingpics.net/pics/806282centralwidget.png(QWidget)!



  • I see - probably this is related to parent/child connection.
    Your stylesheet will be inherited to all children unless changed on child's layer



  • Ok, I will look into that then, thanks !


Log in to reply
 

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