Hi, so here is what I got for myLine.h
#ifndef MYLINE_H
#define MYLINE_H
#include <QGraphicsLineItem>
{
Q_OBJECT
public:
myLine();
public slots:
void mySlot();
private:
};
#endif // MYLINE_H
#ifndef MYLINE_H
#define MYLINE_H
#include <QGraphicsLineItem>
class myLine : public QObject, public QGraphicsLineItem
{
Q_OBJECT
public:
myLine();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
public slots:
void mySlot();
private:
QLineF thisline;
};
#endif // MYLINE_H
To copy to clipboard, switch view to plain text mode
myLine.cpp just to draw the line and try to implement the SLOT()
#include "myLine.h"
#include <QPainter>
#include <QApplication>
myLine::myLine()
{
thisline.setLine(0,0,50,50);
}
{
painter
->setRenderHint
(QPainter::Antialiasing);
painter
->setPen
(QPen(Qt
::red,
8, Qt
::SolidLine, Qt
::RoundCap, Qt
::MiterJoin));
painter
->setBrush
(QBrush(Qt
::blue, Qt
::DiagCrossPattern));
painter->drawLine(thisline);
}
void myLine::mySlot()
{
for (int i = 1; i < 100; i++)
{
line.setLine(0,0,50+i,50+i);
update();
}
}
#include "myLine.h"
#include <QPainter>
#include <QApplication>
myLine::myLine()
{
thisline.setLine(0,0,50,50);
}
void myLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(QPen(Qt::red, 8, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
painter->setBrush(QBrush(Qt::blue, Qt::DiagCrossPattern));
painter->drawLine(thisline);
}
void myLine::mySlot()
{
for (int i = 1; i < 100; i++)
{
QLineF line = this->line();
line.setLine(0,0,50+i,50+i);
update();
}
}
To copy to clipboard, switch view to plain text mode
There's a Dialog to display
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QGraphicsScene *scene;
QTimer *timer;
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
And the source for dialog.cpp where things happen!
#include "dialog.h"
#include "ui_dialog.h"
#include "myLine.h"
#include <QGraphicsView>
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->setSceneRect(0,0,700,700);
ui
->graphicsView
->setRenderHint
(QPainter::Antialiasing);
myLine *line = new myLine();
scene->addItem(line);
connect(timer, SIGNAL(timeout()), line, SLOT(mySlot()));
timer->start(100);
}
Dialog::~Dialog()
{
delete ui;
}
#include "dialog.h"
#include "ui_dialog.h"
#include "myLine.h"
#include <QGraphicsView>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->setSceneRect(0,0,700,700);
ui->graphicsView->setRenderHint(QPainter::Antialiasing);
myLine *line = new myLine();
scene->addItem(line);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), line, SLOT(mySlot()));
timer->start(100);
}
Dialog::~Dialog()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
- when compiles it only prints out the first line which is (0,0,50,50) I think and there's no animation. I've been trying to figure it out, can you please tell me what is wrong here? And thanks for being patience with me, I'm learning so... pardon my noobish!
Bookmarks