Render Area : QWidget « Qt « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Qt » QWidgetScreenshots 
Render Area
  



/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef RENDERAREA_H
#define RENDERAREA_H

#include <QPainterPath>
#include <QWidget>


class RenderArea : public QWidget
{
    Q_OBJECT

public:
    RenderArea(const QPainterPath &path, QWidget *parent = 0);

    QSize minimumSizeHint() const;
    QSize sizeHint() const;

public slots:
    void setFillRule(Qt::FillRule rule);
    void setFillGradient(const QColor &color1, const QColor &color2);
    void setPenWidth(int width);
    void setPenColor(const QColor &color);
    void setRotationAngle(int degrees);

protected:
    void paintEvent(QPaintEvent *event);



private:
    QPainterPath path;
    QColor fillColor1;
    QColor fillColor2;
    int penWidth;
    QColor penColor;
    int rotationAngle;
};


#endif






#include <QtGui>

#include "renderarea.h"


RenderArea::RenderArea(const QPainterPath &path, QWidget *parent)
    : QWidget(parent), path(path)
{
    penWidth = 1;
    rotationAngle = 0;
    setBackgroundRole(QPalette::Base);
}



QSize RenderArea::minimumSizeHint() const
{
    return QSize(5050);
}



QSize RenderArea::sizeHint() const
{
    return QSize(100100);
}



void RenderArea::setFillRule(Qt::FillRule rule)
{
    path.setFillRule(rule);
    update();
}



void RenderArea::setFillGradient(const QColor &color1, const QColor &color2)
{
    fillColor1 = color1;
    fillColor2 = color2;
    update();
}



void RenderArea::setPenWidth(int width)
{
    penWidth = width;
    update();
}



void RenderArea::setPenColor(const QColor &color)
{
    penColor = color;
    update();
}



void RenderArea::setRotationAngle(int degrees)
{
    rotationAngle = degrees;
    update();
}



void RenderArea::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
 
    painter.scale(width() 100.0, height() 100.0);
    painter.translate(50.050.0);
    painter.rotate(-rotationAngle);
    painter.translate(-50.0, -50.0);

 
    painter.setPen(QPen(penColor, penWidth, Qt::SolidLine, Qt::RoundCap,
                        Qt::RoundJoin));
    QLinearGradient gradient(000100);
    gradient.setColorAt(0.0, fillColor1);
    gradient.setColorAt(1.0, fillColor2);
    painter.setBrush(gradient);
    painter.drawPath(path);
}






#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QComboBox;
class QLabel;
class QSpinBox;
QT_END_NAMESPACE
class RenderArea;


class Window : public QWidget
{
    Q_OBJECT

public:
    Window();

private slots:
    void fillRuleChanged();
    void fillGradientChanged();
    void penColorChanged();



private:
    void populateWithColors(QComboBox *comboBox);
    QVariant currentItemData(QComboBox *comboBox);



    enum NumRenderAreas = };

    RenderArea *renderAreas[NumRenderAreas];
    QLabel *fillRuleLabel;
    QLabel *fillGradientLabel;
    QLabel *fillToLabel;
    QLabel *penWidthLabel;
    QLabel *penColorLabel;
    QLabel *rotationAngleLabel;
    QComboBox *fillRuleComboBox;
    QComboBox *fillColor1ComboBox;
    QComboBox *fillColor2ComboBox;
    QSpinBox *penWidthSpinBox;
    QComboBox *penColorComboBox;
    QSpinBox *rotationAngleSpinBox;
};


#endif






#include <QtGui>

#include <math.h>

#include "renderarea.h"
#include "window.h"


const float Pi = 3.14159f;



Window::Window()
{
    QPainterPath rectPath;
    rectPath.moveTo(20.030.0);
    rectPath.lineTo(80.030.0);
    rectPath.lineTo(80.070.0);
    rectPath.lineTo(20.070.0);
    rectPath.closeSubpath();



    QPainterPath roundRectPath;
    roundRectPath.moveTo(80.035.0);
    roundRectPath.arcTo(70.030.010.010.00.090.0);
    roundRectPath.lineTo(25.030.0);
    roundRectPath.arcTo(20.030.010.010.090.090.0);
    roundRectPath.lineTo(20.065.0);
    roundRectPath.arcTo(20.060.010.010.0180.090.0);
    roundRectPath.lineTo(75.070.0);
    roundRectPath.arcTo(70.060.010.010.0270.090.0);
    roundRectPath.closeSubpath();



    QPainterPath ellipsePath;
    ellipsePath.moveTo(80.050.0);
    ellipsePath.arcTo(20.030.060.040.00.0360.0);



    QPainterPath piePath;
    piePath.moveTo(50.050.0);
    piePath.arcTo(20.030.060.040.060.0240.0);
    piePath.closeSubpath();



    QPainterPath polygonPath;
    polygonPath.moveTo(10.080.0);
    polygonPath.lineTo(20.010.0);
    polygonPath.lineTo(80.030.0);
    polygonPath.lineTo(90.070.0);
    polygonPath.closeSubpath();



    QPainterPath groupPath;
    groupPath.moveTo(60.040.0);
    groupPath.arcTo(20.020.040.040.00.0360.0);
    groupPath.moveTo(40.040.0);
    groupPath.lineTo(40.080.0);
    groupPath.lineTo(80.080.0);
    groupPath.lineTo(80.040.0);
    groupPath.closeSubpath();



    QPainterPath textPath;
    QFont timesFont("Times"50);
    timesFont.setStyleStrategy(QFont::ForceOutline);
    textPath.addText(1070, timesFont, tr("Qt"));



    QPainterPath bezierPath;
    bezierPath.moveTo(2030);
    bezierPath.cubicTo(80050508080);



    QPainterPath starPath;
    starPath.moveTo(9050);
    for (int i = 1; i < 5; ++i) {
        starPath.lineTo(50 40 * cos(0.8 * i * Pi),
                        50 40 * sin(0.8 * i * Pi));
    }
    starPath.closeSubpath();



    renderAreas[0new RenderArea(rectPath);
    renderAreas[1new RenderArea(roundRectPath);
    renderAreas[2new RenderArea(ellipsePath);
    renderAreas[3new RenderArea(piePath);
    renderAreas[4new RenderArea(polygonPath);
    renderAreas[5new RenderArea(groupPath);
    renderAreas[6new RenderArea(textPath);
    renderAreas[7new RenderArea(bezierPath);
    renderAreas[8new RenderArea(starPath);
    Q_ASSERT(NumRenderAreas == 9);



    fillRuleComboBox = new QComboBox;
    fillRuleComboBox->addItem(tr("Odd Even"), Qt::OddEvenFill);
    fillRuleComboBox->addItem(tr("Winding"), Qt::WindingFill);

    fillRuleLabel = new QLabel(tr("Fill &Rule:"));
    fillRuleLabel->setBuddy(fillRuleComboBox);



    fillColor1ComboBox = new QComboBox;
    populateWithColors(fillColor1ComboBox);
    fillColor1ComboBox->setCurrentIndex(
            fillColor1ComboBox->findText("mediumslateblue"));

    fillColor2ComboBox = new QComboBox;
    populateWithColors(fillColor2ComboBox);
    fillColor2ComboBox->setCurrentIndex(
            fillColor2ComboBox->findText("cornsilk"));

    fillGradientLabel = new QLabel(tr("&Fill Gradient:"));
    fillGradientLabel->setBuddy(fillColor1ComboBox);

    fillToLabel = new QLabel(tr("to"));
    fillToLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    penWidthSpinBox = new QSpinBox;
    penWidthSpinBox->setRange(020);

    penWidthLabel = new QLabel(tr("&Pen Width:"));
    penWidthLabel->setBuddy(penWidthSpinBox);

    penColorComboBox = new QComboBox;
    populateWithColors(penColorComboBox);
    penColorComboBox->setCurrentIndex(
            penColorComboBox->findText("darkslateblue"));

    penColorLabel = new QLabel(tr("Pen &Color:"));
    penColorLabel->setBuddy(penColorComboBox);

    rotationAngleSpinBox = new QSpinBox;
    rotationAngleSpinBox->setRange(0359);
    rotationAngleSpinBox->setWrapping(true);
    rotationAngleSpinBox->setSuffix("\xB0");

    rotationAngleLabel = new QLabel(tr("&Rotation Angle:"));
    rotationAngleLabel->setBuddy(rotationAngleSpinBox);



    connect(fillRuleComboBox, SIGNAL(activated(int)),
            this, SLOT(fillRuleChanged()));
    connect(fillColor1ComboBox, SIGNAL(activated(int)),
            this, SLOT(fillGradientChanged()));
    connect(fillColor2ComboBox, SIGNAL(activated(int)),
            this, SLOT(fillGradientChanged()));
    connect(penColorComboBox, SIGNAL(activated(int)),
            this, SLOT(penColorChanged()));

    for (int i = 0; i < NumRenderAreas; ++i) {
        connect(penWidthSpinBox, SIGNAL(valueChanged(int)),
                renderAreas[i], SLOT(setPenWidth(int)));
        connect(rotationAngleSpinBox, SIGNAL(valueChanged(int)),
                renderAreas[i], SLOT(setRotationAngle(int)));
    }

 
    QGridLayout *topLayout = new QGridLayout;
    for (int i = 0; i < NumRenderAreas; ++i)
        topLayout->addWidget(renderAreas[i], i / 3, i % 3);

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addLayout(topLayout, 0014);
    mainLayout->addWidget(fillRuleLabel, 10);
    mainLayout->addWidget(fillRuleComboBox, 1113);
    mainLayout->addWidget(fillGradientLabel, 20);
    mainLayout->addWidget(fillColor1ComboBox, 21);
    mainLayout->addWidget(fillToLabel, 22);
    mainLayout->addWidget(fillColor2ComboBox, 23);
    mainLayout->addWidget(penWidthLabel, 30);
    mainLayout->addWidget(penWidthSpinBox, 3113);
    mainLayout->addWidget(penColorLabel, 40);
    mainLayout->addWidget(penColorComboBox, 4113);
    mainLayout->addWidget(rotationAngleLabel, 50);
    mainLayout->addWidget(rotationAngleSpinBox, 5113);
    setLayout(mainLayout);



    fillRuleChanged();
    fillGradientChanged();
    penColorChanged();
    penWidthSpinBox->setValue(2);

    setWindowTitle(tr("Painter Paths"));
}



void Window::fillRuleChanged()
{
    Qt::FillRule rule = (Qt::FillRule)currentItemData(fillRuleComboBox).toInt();

    for (int i = 0; i < NumRenderAreas; ++i)
        renderAreas[i]->setFillRule(rule);
}



void Window::fillGradientChanged()
{
    QColor color1 = qvariant_cast<QColor>(currentItemData(fillColor1ComboBox));
    QColor color2 = qvariant_cast<QColor>(currentItemData(fillColor2ComboBox));

    for (int i = 0; i < NumRenderAreas; ++i)
        renderAreas[i]->setFillGradient(color1, color2);
}



void Window::penColorChanged()
{
    QColor color = qvariant_cast<QColor>(currentItemData(penColorComboBox));

    for (int i = 0; i < NumRenderAreas; ++i)
        renderAreas[i]->setPenColor(color);
}



void Window::populateWithColors(QComboBox *comboBox)
{
    QStringList colorNames = QColor::colorNames();
    foreach (QString name, colorNames)
        comboBox->addItem(name, QColor(name));
}



QVariant Window::currentItemData(QComboBox *comboBox)
{
    return comboBox->itemData(comboBox->currentIndex());
}







#include <QApplication>

#include "window.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Window window;
    window.show();
    return app.exec();
}

   
    
  
Related examples in the same category
1.Combo widget mapper
2.Calendar widget
3.Subclass QWidget
4.Subclass QWidget to do paint
5.Analog Clock
6.Child widget
7.Circle Widget
8.Using QStatus tip event
9.extends QWidget
10.Wiggly Widget
11.How to compile and run Qt from command line
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.