Results 1 to 2 of 2

Thread: Adding numbers to circles in QPaint

  1. #1
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Angry Adding numbers to circles in QPaint

    hey there, i created a little application that lets the user create circles and connect them with lines using the tooltips example as a base. im just wondering if theres a way to number the circles so that everytime a user creates a new circle it gets numbered starting from 0. im sure its to do wth the shapeitem.cpp file but i dont know what to add to make it work. any ideas??? ive listed my shapeItem.cpp file below and most of the sortingBox.cpp file.....any help would be much appreciated because i am really stuck with this - thanks, Jag

    -----------------------------------shapeItem.cpp--------------------------------------------
    #include <QtGui>

    #include "shapeitem.h"

    QPainterPath ShapeItem:ath() const
    {
    return myPath;
    }

    QPoint ShapeItem:osition() const
    {
    return myPosition;
    }

    QColor ShapeItem::color() const
    {
    return myColor;
    }

    QString ShapeItem::toolTip() const
    {
    return myToolTip;
    }

    void ShapeItem::setPath(const QPainterPath &path)
    {
    myPath = path;
    }

    void ShapeItem::setToolTip(const QString &toolTip)
    {
    myToolTip = toolTip;
    }

    void ShapeItem::setPosition(const QPoint &position)
    {
    myPosition = position;
    }

    void ShapeItem::setColor(const QColor &color)
    {
    myColor = color;
    }

    ------------------------------------------------------------------------------------------
    ----------------------------------------sortingBox.cpp----------------------------------

    #include <QtGui>
    #include <stdlib.h>
    #include <QtCore>

    #include "sortingbox.h"

    SortingBox::SortingBox()///////////////////CONSTRUCTOR////////////////////////////
    {
    setAttribute(Qt::WA_StaticContents);
    setMouseTracking(true);
    setBackgroundRole(QPalette::Base);

    itemInMotion = 0;
    points1 = new QPoint[MAXPOINTS];
    points2 = new QPoint[MAXPOINTS];
    count = 0;
    down = FALSE;
    recordLine = FALSE;


    circlePath.addEllipse(QRect(0, 0, 20, 20));//used to change size of the circle//

    setWindowTitle(tr("Topology Creator"));
    resize(500, 300);


    }

    SortingBox::~SortingBox()///////////////////DE-CONSTRUCTOR////////////////////////////
    {
    delete[] points1; // cleanup
    delete[] points2; // cleanup
    }


    bool SortingBox::event(QEvent *event)
    {
    if (event->type() == QEvent::ToolTip) {
    QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
    int index = itemAt(helpEvent->pos());
    if (index != -1)
    QToolTip::showText(helpEvent->globalPos(), shapeItems[index].toolTip());
    }
    return QWidget::event(event);
    }

    void SortingBox::resizeEvent(QResizeEvent * /* event */)
    {
    int margin = style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin);
    int x = width() - margin;
    int y = height() - margin;

    y = updateButtonGeometry(newCircleButton, x, y);
    y = updateButtonGeometry(newArrowButton, x, y);


    }

    void SortingBox:aintEvent(QPaintEvent * /* event */)
    {
    QPainter painter(this);

    foreach (ShapeItem shapeItem, shapeItems)
    {
    painter.translate(shapeItem.position());
    painter.setBrush(shapeItem.color());
    painter.drawPath(shapeItem.path());
    painter.translate(-shapeItem.position());
    }

    QPoint midpoint;
    QPoint test(100,100);
    if(lineCheck >1)
    {
    for(int i=0;i<count-1;i= i+2)
    {
    /*for(int j=j+1;j<count;j++)
    {*/

    painter.setPen(initialItemColor() );

    painter.drawLine( points1[i], points1[i+1] );
    midpoint = ((points1[i] + points1[i+1])/2);

    QSize textbox(100,100);
    QString str = "50";

    if(clearer = 1 && lineCheck>2)
    {

    painter.setPen(myTextColor());
    painter.drawText(QRect(midpoint,textbox),0,tr("%1" ).arg(str));


    }update();
    } update();

    }
    }



    void SortingBox::mousePressEvent(QMouseEvent *event)
    {
    down = FALSE;

    if (event->button() == Qt::LeftButton) {
    int index = itemAt(event->pos());
    if (index != -1) {
    itemInMotion = &shapeItems[index];
    previousPosition = event->pos();
    shapeItems.move(index, shapeItems.size() - 1);
    update();
    recordLine = true;


    points1[count++] = QPoint(event->pos()); // add point

    lineCheck = lineCheck +1;

    }
    else
    createShapeItem(circlePath, tr("Node"), QPoint(event->pos()),
    randomItemColor());
    recordLine = false;
    }

    }

    void SortingBox::mouseMoveEvent(QMouseEvent *event)
    {
    if ((event->buttons() & Qt::LeftButton) && itemInMotion)
    moveItemTo(event->pos());

    if(recordLine == true)
    {
    if ( down && count < MAXPOINTS ) {
    QPainter painter( this );
    points1[count++] = event->pos(); // add point

    }
    }
    }

    void SortingBox::mouseReleaseEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton && itemInMotion) {
    moveItemTo(event->pos());
    itemInMotion = 0;
    }
    down = FALSE;
    update();
    if(recordLine == true)
    { if (count < MAXPOINTS ) {
    QPainter painter( this );
    points1[count++] = QPoint(event->pos());

    }
    }

    }


    int SortingBox::itemAt(const QPoint &pos)
    {
    for (int i = shapeItems.size() - 1; i >= 0; --i) {
    const ShapeItem &item = shapeItems[i];
    if (item.path().contains(pos - item.position()))
    return i;
    }
    return -1;
    }



    int SortingBox::updateButtonGeometry(QToolButton *button, int x, int y)
    {
    QSize size = button->sizeHint();
    button->setGeometry(x - size.rwidth(), y - size.rheight(),
    size.rwidth(), size.rheight());

    return y - size.rheight()
    - style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
    }

    void SortingBox::createShapeItem(const QPainterPath &path,
    const QString &toolTip, const QPoint &pos,
    const QColor &color)
    {
    ShapeItem shapeItem;
    shapeItem.setPath(path);
    shapeItem.setToolTip(toolTip);
    shapeItem.setPosition(pos);
    shapeItem.setColor(color);
    shapeItems.append(shapeItem);
    update();
    }

    QToolButton *SortingBox::createToolButton(const QString &toolTip,
    const QIcon &icon, const char *member)
    {
    QToolButton *button = new QToolButton(this);
    button->setToolTip(toolTip);
    button->setIcon(icon);
    button->setIconSize(QSize(30, 30));
    connect(button, SIGNAL(clicked()), this, member);

    return button;
    }

    QPoint SortingBox::initialItemPosition(const QPainterPath &path)
    {
    int x;
    int y = (height() - (int)path.controlPointRect().height()) / 2;
    if (shapeItems.size() == 0)
    x = ((3 * width()) / 2 - (int)path.controlPointRect().width()) / 2;
    else
    x = (width() / shapeItems.size()
    - (int)path.controlPointRect().width()) / 2;

    return QPoint(x, y);
    }

    QPoint SortingBox::randomItemPosition()
    {
    return QPoint(rand() % (width() - 120), rand() % (height() - 120));
    }

    QColor SortingBox::initialItemColor()
    {
    return QColor::fromHsv(256 % 256, 255, 190);
    }

    QColor SortingBox::randomItemColor()
    {
    return QColor::fromHsv(200 % 256, 255, 190);//256 % 256 makes red//

    }

    QColor SortingBox::myTextColor()
    {
    return QColor::fromHsv(235 % 256, 255, 190);//256 % 256 makes red//
    }

    void SortingBox::clear()
    {
    clearer = 1;
    }

    ---------------------------------------------------------------------------------------------------

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding numbers to circles in QPaint

    Use a static field in your item class to denote how many instances of the class were created and store the number of an instance in the instance itself, like so:

    Qt Code:
    1. class Item{
    2. Item(){
    3. static uint __inst=0;
    4. _instance_no = __inst++;
    5. }
    6. uint instanceNumber(){ return _instance_no; }
    7. private:
    8. uint _instance_no;
    9. };
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 7
    Last Post: 12th September 2012, 06:42
  2. Adding qpaint object to a Layout
    By Rooster in forum Newbie
    Replies: 6
    Last Post: 13th February 2008, 13:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.