Ok, so my dimensioning and positioning problems were solved but now I'm trying to create my own customized QGraphicsItem and I've run into a bit of a problem.
Heres is my code for my peg.h
#ifndef PEG_H
#define PEG_H
#include <QtGui>
#include <QtCore>
{
public:
private:
};
#endif // PEG_H
#ifndef PEG_H
#define PEG_H
#include <QtGui>
#include <QtCore>
class Peg : public QGraphicsItem
{
public:
Peg(QColor color);
void paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
QRectF boundingRect() const;
private:
QColor color;
};
#endif // PEG_H
To copy to clipboard, switch view to plain text mode
and its corresponding peg.cpp
#include "peg.h"
color = col;
setFlag(ItemIsMovable);
}
QRectF Peg
::boundingRect() const { }
painter
->setPen
(QPen(Qt
::black));
gradient.setColorAt(0.0,Qt::white);
gradient.setColorAt(0.5,color.lighter(120));
gradient.setColorAt(1.0,color);
painter->setBrush(gradient);
painter->drawEllipse(0,0,10,10);
}
#include "peg.h"
Peg::Peg(QColor col){
color = col;
setFlag(ItemIsMovable);
}
QRectF Peg::boundingRect() const {
return QRectF(-1,-1,12,12);
}
void Peg::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->setPen(QPen(Qt::black));
QRadialGradient gradient(3,3,9);
gradient.setColorAt(0.0,Qt::white);
gradient.setColorAt(0.5,color.lighter(120));
gradient.setColorAt(1.0,color);
painter->setBrush(gradient);
painter->drawEllipse(0,0,10,10);
}
To copy to clipboard, switch view to plain text mode
Then this is the code of the constructor of my mainwindow:
Mastermind
::Mastermind(QWidget *parent
){
ui->setupUi(this);
ui->gvDrawArea->setScene(scene);
ui
->gvDrawArea
->setRenderHints
(QPainter::Antialiasing|QPainter
::TextAntialiasing);
Peg peg(Qt::red);
scene->addItem(&peg);
}
Mastermind::Mastermind(QWidget *parent)
: QMainWindow(parent), ui(new Ui::Mastermind)
{
ui->setupUi(this);
scene = new QGraphicsScene(-50,-50,100,100);
scene->setBackgroundBrush(QBrush(QColor(23,23,23)));
ui->gvDrawArea->setScene(scene);
ui->gvDrawArea->setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing);
Peg peg(Qt::red);
scene->addItem(&peg);
}
To copy to clipboard, switch view to plain text mode
The only problem is, I thought I was supposed to see a somewhat red circle somewhere on the screen yet nothing has appeared.
What did I do wrong?
Thanks for the help.
Bookmarks