- Position widgets in layout
- Embed in logic (signals/slots, events)
- Expose data to the outside
QVBoxLayout * vLayout = new QVBoxLayout(this);
gLayout = new QGridLayout();
QSizePolicy policy(QSizePolicy::Minimum, QSizePolicy::Expanding);
gLayout->addWidget(button1, 0, 0);
void ColorPaletteWidget::button1Clicked()
{
QString css = QString("background-color : %1");.arg(colorList.at(0).name());
label->setStyleSheet(css);
emit colorChanged(colorList[0]);
}
// widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void colorChanged(QColor color);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
// widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "colorpicker.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ColorPicker * colorPicker = new ColorPicker(this);
connect(colorPicker,&ColorPicker::colorChanged,this,&Widget::colorChanged);
ui->verticalLayout->addWidget(colorPicker);
}
Widget::~Widget()
{
delete ui;
}
void Widget::colorChanged(QColor color)
{
qDebug() << "Color changed to : " << color.name();
}
// colorpiicker.h
#ifndef COLORPICKER_H
#define COLORPICKER_H
#include <QWidget>
#include <QColor>
#include <QGridLayout>
#include <QLabel>
class ColorPicker : public QWidget
{
Q_OBJECT
public:
explicit ColorPicker(QWidget *parent = nullptr);
QColor getColor() const;
void setColor(const QColor &value);
signals:
void colorChanged(QColor newColor);
public slots:
void button1Clicked();
void button2Clicked();
void button3Clicked();
void button4Clicked();
void button5Clicked();
void button6Clicked();
void button7Clicked();
void button8Clicked();
void button9Clicked();
private:
void populateColors();
void setupUi();
QColor color;
QList<QColor> colorList;
QGridLayout * gLayout;
QLabel * label;
};
#endif // COLORPICKER_H
// colorpiicker.cpp
#include "colorpicker.h"
#include <QVBoxLayout>
#include <QPushButton>
ColorPicker::ColorPicker(QWidget *parent) : QWidget(parent)
{
populateColors();
setupUi();
}
QColor ColorPicker::getColor() const
{
return color;
}
void ColorPicker::setColor(const QColor &value)
{
color = value;
}
void ColorPicker::button1Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(0).name());
label->setStyleSheet(css);
emit colorChanged(colorList[0]);
}
void ColorPicker::button2Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(1).name());
label->setStyleSheet(css);
emit colorChanged(colorList[1]);
}
void ColorPicker::button3Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(2).name());
label->setStyleSheet(css);
emit colorChanged(colorList[2]);
}
void ColorPicker::button4Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(3).name());
label->setStyleSheet(css);
emit colorChanged(colorList[3]);
}
void ColorPicker::button5Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(4).name());
label->setStyleSheet(css);
emit colorChanged(colorList[4]);
}
void ColorPicker::button6Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(5).name());
label->setStyleSheet(css);
emit colorChanged(colorList[5]);
}
void ColorPicker::button7Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(6).name());
label->setStyleSheet(css);
emit colorChanged(colorList[6]);
}
void ColorPicker::button8Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(7).name());
label->setStyleSheet(css);
emit colorChanged(colorList[7]);
}
void ColorPicker::button9Clicked()
{
QString css = QString("background-color : %1")
.arg(colorList.at(8).name());
label->setStyleSheet(css);
emit colorChanged(colorList[8]);
}
void ColorPicker::populateColors()
{
colorList << Qt::red <<
Qt::green <<
Qt::blue <<
Qt::cyan <<
Qt::darkRed <<
Qt::darkGray <<
Qt::gray <<
Qt::yellow <<
Qt::darkYellow;
}
void ColorPicker::setupUi()
{
QVBoxLayout * vLayout = new QVBoxLayout(this);
gLayout = new QGridLayout();
QSizePolicy policy(QSizePolicy::Minimum,QSizePolicy::Expanding);
QPushButton * button1 = new QPushButton("one",this);
button1->setSizePolicy(policy);
QString css = QString("background-color : %1").arg(colorList.at(0).name());
button1->setStyleSheet(css);
connect(button1,&QPushButton::clicked,this,&ColorPicker::button1Clicked);
QPushButton * button2 = new QPushButton("two",this);
button2->setSizePolicy(policy);
css = QString("background-color : %1").arg(colorList.at(1).name());
button2->setStyleSheet(css);
connect(button2,&QPushButton::clicked,this,&ColorPicker::button2Clicked);
QPushButton * button3 = new QPushButton("three",this);
button3->setSizePolicy(policy);
css = QString("background-color : %1").arg(colorList.at(2).name());
button3->setStyleSheet(css);
connect(button3,&QPushButton::clicked,this,&ColorPicker::button3Clicked);
QPushButton * button4 = new QPushButton("four",this);
button4->setSizePolicy(policy);
css = QString("background-color : %1").arg(colorList.at(3).name());
button4->setStyleSheet(css);
connect(button4,&QPushButton::clicked,this,&ColorPicker::button4Clicked);
QPushButton * button5 = new QPushButton("five",this);
button5->setSizePolicy(policy);
css = QString("background-color : %1").arg(colorList.at(4).name());
button5->setStyleSheet(css);
connect(button5,&QPushButton::clicked,this,&ColorPicker::button5Clicked);
QPushButton * button6 = new QPushButton("six",this);
button6->setSizePolicy(policy);
css = QString("background-color : %1").arg(colorList.at(5).name());
button6->setStyleSheet(css);
connect(button6,&QPushButton::clicked,this,&ColorPicker::button6Clicked);
QPushButton * button7 = new QPushButton("seven",this);
button7->setSizePolicy(policy);
css = QString("background-color : %1").arg(colorList.at(6).name());
button7->setStyleSheet(css);
connect(button7,&QPushButton::clicked,this,&ColorPicker::button7Clicked);
QPushButton * button8 = new QPushButton("eight",this);
button8->setSizePolicy(policy);
css = QString("background-color : %1").arg(colorList.at(7).name());
button8->setStyleSheet(css);
connect(button8,&QPushButton::clicked,this,&ColorPicker::button8Clicked);
QPushButton * button9 = new QPushButton("nine",this);
button9->setSizePolicy(policy);
css = QString("background-color : %1").arg(colorList.at(8).name());
button9->setStyleSheet(css);
connect(button9,&QPushButton::clicked,this,&ColorPicker::button9Clicked);
gLayout->addWidget(button1,0,0);
gLayout->addWidget(button2,0,1);
gLayout->addWidget(button3,0,2);
gLayout->addWidget(button4,1,0);
gLayout->addWidget(button5,1,1);
gLayout->addWidget(button6,1,2);
gLayout->addWidget(button7,2,0);
gLayout->addWidget(button8,2,1);
gLayout->addWidget(button9,2,2);
label = new QLabel("Color");
css = QString("background-color : #eeeab6");
label->setFixedHeight(50);
label->setStyleSheet(css);
vLayout->addWidget(label);
vLayout->addLayout(gLayout);
}