我是一只小小小小鸟

导航

 

我希望能在这一系列文章中一步步探寻QT的二维功能。
这系列文章假定读者已经知道了基本的信号、槽,和窗体、布局等最基础的知识(其实就是我目前学到的那些东西。。。)。
先讲讲最基本的背景,那就要说到调色板了,小题大做,我们将通过一个RGB调色器的实现的例子来说说调色板。 

先贴出程序代码吧

main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QSpinBox>
class MainWindow: public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
private:
QSpinBox *redBox;
QSpinBox *blueBox;
QSpinBox *greenBox;
public slots:
void callRepaint(int value);
signals:
void colors(int red, int green, int blue);
};

#endif // MAINWINDOW_H
mainwindow.cpp
#include <QPainter>
#include <QPalette>
#include <QColor>
#include <QVBoxLayout>
#include <QPushButton>
#include <QSize>
#include <QSpinBox>
#include <QLabel>

#include "mainwindow.h"
#include "colorboard.h"
MainWindow::MainWindow(QWidget *parent)
:QWidget(parent)
{
QGridLayout *gLayout = new QGridLayout;

QPushButton *quit = new QPushButton(tr("&Quit"));
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
gLayout->addWidget(quit, 1, 1);

QHBoxLayout *hLayout = new QHBoxLayout;
QLabel *redLabel = new QLabel(tr("red"));
hLayout->addWidget(redLabel);
redBox = new QSpinBox;
redBox->setRange(0, 255);
redBox->setValue(255);
hLayout->addWidget(redBox);

QLabel *greenLabel = new QLabel(tr("green"));
hLayout->addWidget(greenLabel);
greenBox = new QSpinBox;
greenBox->setRange(0, 255);
greenBox->setValue(255);
hLayout->addWidget(greenBox);

QLabel *blueLabel = new QLabel(tr("blue"));
hLayout->addWidget(blueLabel);
blueBox = new QSpinBox;
blueBox->setRange(0, 255);
blueBox->setValue(255);
hLayout->addWidget(blueBox);

gLayout->addLayout(hLayout, 2, 1);

ColorBoard *c = new ColorBoard;
gLayout->addWidget(c, 3, 1);

connect(redBox, SIGNAL(valueChanged(int)), this, SLOT(callRepaint(int)));
connect(greenBox, SIGNAL(valueChanged(int)), this, SLOT(callRepaint(int)));
connect(blueBox, SIGNAL(valueChanged(int)), this, SLOT(callRepaint(int)));
connect(this, SIGNAL(colors(int,int,int)), c, SLOT(change(int,int,int)));

setLayout(gLayout);
}

void MainWindow::callRepaint(int value)
{
emit colors(redBox->value(), greenBox->value(), blueBox->value());
}
colorboard.h
#ifndef COLORBOARD_H
#define COLORBOARD_H
#include <QWidget>
#include <QSize>
class ColorBoard: public QWidget
{
Q_OBJECT
public:
ColorBoard(QWidget *parent = 0);
QSize minimumSizeHint() const;
public slots:
void change(int red, int green, int blue);
};

#endif // COLORBOARD_H
colorboard.cpp
#include "colorboard.h"

ColorBoard::ColorBoard (QWidget *parent)
:QWidget(parent)
{
setPalette(QPalette(QColor(255,255,255)));
setAutoFillBackground(true);
//setFixedSize(100, 100);
}

QSize ColorBoard::minimumSizeHint() const
{
return QSize(50, 50);
}

void ColorBoard::change(int red, int green, int blue)
{
setPalette(QPalette(QColor(red,green,blue)));
update();
}

涉及到二维作图的东西就是那块颜色了。
关于调色板的东西都在colorboard.cpp里面,那么什么是调色板呢?调色板其实就是默认的颜色,通过设置调色板我们能设置这个窗体内背景,按钮,等等的默认颜色。

ColorBoard::ColorBoard (QWidget *parent)
    :QWidget(parent)
{
    setPalette(QPalette(QColor(255,255,255)));
    setAutoFillBackground(true);

在ColorBoard的构造函数里,我们可以用setPalette设置调色板,QPalette有很多种构造函数(分别涉及具体的颜色分配),大家到帮助里仔细看看就很清楚了。我这里的构造方法是把按钮和背景颜色都设成了白色。
不过这样还不够,还要执行以下setAutoFillBackground方法,这个窗体才会自动从Palette里去找设定的背景颜色。
好了,设定好了默认的背景颜色,那么我们怎么去修改它呢?
槽change(int,int,int)是专门用来修改背景色的。

void ColorBoard::change(int red, int green, int blue)
{
    setPalette(QPalette(QColor(red,green,blue)));
    update();
}

可以看到,我们只需要再次设定一下调色板,把背景色设到我们的三个spinBox设定的颜色就行了,然后在update()一下,update的意思是重绘整个窗体,不过实验发现不加这句也行,应该是setPalette里面包含了update了。

 






 

posted on 2012-01-28 22:49  cloudygoose  阅读(1432)  评论(0)    收藏  举报