Skip to content

Instantly share code, notes, and snippets.

@EXL
Last active May 11, 2022 01:59
Show Gist options
  • Save EXL/c93cfabb24aa03d859c9 to your computer and use it in GitHub Desktop.
Save EXL/c93cfabb24aa03d859c9 to your computer and use it in GitHub Desktop.
QComboBox Gradient Sample
#-------------------------------------------------
#
# Project created by QtCreator 2014-08-29T17:55:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GradientComboBox
TEMPLATE = app
SOURCES += main.cpp\
MainWindow.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.ui
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initializeComboBox();
colorizeComboBox(ui->comboBox, Qt::green, Qt::red, Qt::yellow);
}
void MainWindow::initializeComboBox()
{
const int n = 50;
for (size_t i = 0; i <= n; ++i) {
ui->comboBox->insertItem(i, QString::number(getValue(i), 'f', 2));
// ui->comboBox->setItemData(i, colorGenerator(Qt::green, Qt::red, i, n), Qt::BackgroundRole);
}
}
qreal MainWindow::getValue(int aStep)
{
return (qreal) aStep / 10;
}
QColor MainWindow::colorGenerator(QColor aBegin, QColor aEnd, int aStep, int aSize) const
{
qreal rB, gB, bB;
qreal rE, gE, bE;
aBegin.getRgbF(&rB, &gB, &bB);
aEnd.getRgbF(&rE, &gE, &bE);
QColor color;
color.setRgbF(colorInterpolate(rB, rE, aStep, aSize),
colorInterpolate(gB, gE, aStep, aSize),
colorInterpolate(bB, bE, aStep, aSize));
return color;
}
void MainWindow::colorizeComboBox(QComboBox *aComboBox, QColor aBegin, QColor aEnd, QColor aMiddle)
{
int _size = aComboBox->count();
int _middle = _size / 2;
qreal rB, gB, bB;
qreal rE, gE, bE;
qreal rM, gM, bM;
aBegin.getRgbF(&rB, &gB, &bB);
aEnd.getRgbF(&rE, &gE, &bE);
aMiddle.getRgbF(&rM, &gM, &bM);
for (int i = 0; i < _size; ++i) {
QColor color;
if (aMiddle != Qt::transparent) {
if (i < _middle) {
color.setRgbF(colorInterpolate(rB, rM, i, _middle),
colorInterpolate(gB, gM, i, _middle),
colorInterpolate(bB, bM, i, _middle));
} else {
color.setRgbF(colorInterpolate(rM, rE, i - _middle, _middle),
colorInterpolate(gM, gE, i - _middle, _middle),
colorInterpolate(bM, bE, i - _middle, _middle));
}
} else {
color.setRgbF(colorInterpolate(rB, rE, i, _size),
colorInterpolate(gB, gE, i, _size),
colorInterpolate(bB, bE, i, _size));
}
aComboBox->setItemData(i, color, Qt::BackgroundRole);
}
}
qreal MainWindow::colorInterpolate(qreal aBegin, qreal aEnd, int aStep, int aSize) const
{
return (aBegin < aEnd) ? qAbs(((aEnd - aBegin) * ((qreal) aStep / aSize)) + aBegin) :
qAbs(((aBegin - aEnd) * (1 - ((qreal) aStep / aSize))) + aEnd);
}
MainWindow::~MainWindow()
{
delete ui;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class QComboBox;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void initializeComboBox();
qreal getValue(int aStep);
QColor colorGenerator(QColor aBegin, QColor aEnd, int aStep, int aSize) const;
void colorizeComboBox(QComboBox *aComboBox, QColor aBegin, QColor aEnd, QColor aMiddle = Qt::transparent);
qreal colorInterpolate(qreal aBegin, qreal aEnd, int aStep, int aSize) const;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBox">
<property name="minimumSize">
<size>
<width>120</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment