@SGaist
The project consists of three cpp and two header files.
The code of cpp files is listed below:
myapp.cpp
#include "myapp.h"
myApp::myApp(QWidget *parent) : QMainWindow(parent)
{
QIcon icns(":/03_Images/simple_icon.png");
QPushButton* btnNormal = new QPushButton(this);
btnNormal->setText("Normal");
btnNormal->setIcon(icns);
QPushButton* btnActive = new QPushButton(this);
btnActive->setText("Active");
btnActive->setIcon(icns);
QPushButton* btnDisabled = new QPushButton(this);
btnDisabled->setText("Disabled");
btnDisabled->setIcon(icns);
btnDisabled->setEnabled(false);
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(btnNormal);
layout->addWidget(btnActive);
layout->addWidget(btnDisabled);
QWidget* centralWidget = new QWidget(this);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
QMenu* menu = new QMenu(this);
menu->setTitle("Menu");
QAction* actItem4_1 = menu -> addAction("Normal icon", this, SLOT(repaint()));
actItem4_1 ->setIcon(icns);
QAction* actItem4_2 = menu -> addAction("Active icon", this, SLOT(repaint()));
actItem4_2 ->setIcon(icns);
QAction* actItem4_3 = menu -> addAction("Disabled icon", this, SLOT(repaint()));
actItem4_3 ->setIcon(icns);
actItem4_3 ->setEnabled(false);
QMenuBar* menuBar = new QMenuBar(this);
menuBar->addMenu(menu);
setMenuBar(menuBar);
}
mystyle.cpp
#include "mystyle.h"
MyStyle::MyStyle() : QProxyStyle(QStyleFactory::create("fusion")){
setObjectName("MyStyle");
}
MyStyle::~MyStyle(){
//
}
void MyStyle::polish(QPalette& palette){
palette.setColor(QPalette::Window, QColor(230, 230, 230));
palette.setColor(QPalette::WindowText, QColor(64, 64, 64));
palette.setColor(QPalette::Base, QColor(248, 248, 248));
palette.setColor(QPalette::AlternateBase, QColor(243, 243, 243));
palette.setColor(QPalette::Text, QColor(64, 64, 64));
palette.setColor(QPalette::ToolTipBase, QColor(0, 255, 0));
palette.setColor(QPalette::ToolTipText, QColor(64, 64, 64));
palette.setColor(QPalette::Button, QColor(194, 194, 194));
palette.setColor(QPalette::ButtonText, QColor(64, 64, 64));
palette.setColor(QPalette::BrightText, QColor(0, 255, 0));
palette.setColor(QPalette::Highlight, QColor(86, 86, 86));
palette.setColor(QPalette::HighlightedText, QColor(191, 191, 191));
palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(128, 128, 128));
palette.setColor(QPalette::Disabled, QPalette::Button, QColor(214, 214, 214));
palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(127, 127, 127));
palette.setColor(QPalette::Disabled, QPalette::Text, QColor(128, 128, 128));
palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(64, 128, 0));
}
QPixmap MyStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const
{
QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32);
QPainter painter(&img);
painter.save();
switch (iconMode) {
case QIcon::Normal:
qDebug() << "Normal mode";
painter.fillRect(img.rect(), QColor(Qt::red));
break;
case QIcon::Active:
qDebug() << "Active mode";
painter.fillRect(img.rect(), QColor(Qt::green));
break;
case QIcon::Disabled:
qDebug() << "Disabled mode";
painter.fillRect(img.rect(), QColor(Qt::blue));
break;
case QIcon::Selected:
qDebug() << "Selected mode";
painter.fillRect(img.rect(), QColor(Qt::magenta));
break;
default:
qDebug() << "Other mode (Unknown)";
painter.fillRect(img.rect(), QColor(Qt::gray));
break;
}
painter.restore();
return QPixmap::fromImage(img);
}
main.cpp
#include "myapp.h"
#include <QApplication>
#include "mystyle.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle(new MyStyle());
myApp w;
w.show();
return a.exec();
}