Hello,
I have a class with scene name
and this class have codes that draw lines in a GraphicView
this class is wrote for mainwindow that Use toolBar for
Drawing
but i want to
So change the code that draw lines when i click pushButton
not toolBar.
Can any body change codes for me?
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QToolBar>
#include "scene.h"
#include <QAction>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
public slots:
private:
Ui::MainWindow *ui;
Scene* scene;
void createActions();
void createConnections();
void createToolBar();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QToolBar>
#include "scene.h"
#include <QAction>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void actionGroupClicked(QAction*);
private:
Ui::MainWindow *ui;
QGraphicsView* view;
Scene* scene;
void createActions();
void createConnections();
void createToolBar();
QAction* lineAction;
QAction* selectAction;
QActionGroup *actionGroup;
QToolBar* drawingToolBar;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new Scene(this);
scene->setSceneRect(0,0,200,200);
ui->graphicsView->setScene(scene);
ui
->graphicsView
->setRenderHints
(QPainter::Antialiasing);
//setCentralWidget(ui->graphicsView);
createActions();
createConnections();
createToolBar();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions(){
lineAction
= new QAction("Draw line",
this);
lineAction->setData(int(Scene::DrawLine));
lineAction
->setIcon
(QIcon(":/icons/line.png"));
lineAction->setCheckable(true);
selectAction
= new QAction("Select object",
this);
selectAction->setData(int(Scene::SelectObject));
selectAction
->setIcon
(QIcon(":/icons/select.png"));
selectAction->setCheckable(true);
actionGroup->setExclusive(true);
actionGroup->addAction(lineAction);
actionGroup->addAction(selectAction);
}
void MainWindow::createConnections(){
connect(actionGroup,
SIGNAL(triggered
(QAction*)),
this,
SLOT(actionGroupClicked
(QAction*)));
}
void MainWindow
::actionGroupClicked(QAction *action
){ scene->setMode(Scene::Mode(action->data().toInt()));
}
void MainWindow::createToolBar(){
addToolBar(Qt::TopToolBarArea, drawingToolBar);
drawingToolBar->addAction(selectAction);
drawingToolBar->addAction(lineAction);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new Scene(this);
scene->setSceneRect(0,0,200,200);
ui->graphicsView->setScene(scene);
ui->graphicsView->setRenderHints(QPainter::Antialiasing);
//setCentralWidget(ui->graphicsView);
createActions();
createConnections();
createToolBar();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions(){
lineAction = new QAction("Draw line", this);
lineAction->setData(int(Scene::DrawLine));
lineAction->setIcon(QIcon(":/icons/line.png"));
lineAction->setCheckable(true);
selectAction = new QAction("Select object", this);
selectAction->setData(int(Scene::SelectObject));
selectAction->setIcon(QIcon(":/icons/select.png"));
selectAction->setCheckable(true);
actionGroup = new QActionGroup(this);
actionGroup->setExclusive(true);
actionGroup->addAction(lineAction);
actionGroup->addAction(selectAction);
}
void MainWindow::createConnections(){
connect(actionGroup, SIGNAL(triggered(QAction*)),
this, SLOT(actionGroupClicked(QAction*)));
}
void MainWindow::actionGroupClicked(QAction *action){
scene->setMode(Scene::Mode(action->data().toInt()));
}
void MainWindow::createToolBar(){
drawingToolBar = new QToolBar;
addToolBar(Qt::TopToolBarArea, drawingToolBar);
drawingToolBar->addAction(selectAction);
drawingToolBar->addAction(lineAction);
}
To copy to clipboard, switch view to plain text mode
scene.h:
#ifndef SCENE_H
#define SCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsLineItem>
#include <QAction>
#include <QGraphicsView>
#include <QKeyEvent>
{
public:
scene();
enum Mode {NoMode, SelectObject, DrawLine};
void setMode(Mode mode);
protected:
private:
Mode sceneMode;
void makeItemsControllable(bool areControllable);
};
#endif // SCENE_H
#ifndef SCENE_H
#define SCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsLineItem>
#include <QAction>
#include <QGraphicsView>
#include <QKeyEvent>
class Scene : public QGraphicsScene
{
public:
scene();
enum Mode {NoMode, SelectObject, DrawLine};
Scene(QObject* parent = 0);
void setMode(Mode mode);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
private:
Mode sceneMode;
QPointF origPoint;
QGraphicsLineItem* itemToDraw;
void makeItemsControllable(bool areControllable);
};
#endif // SCENE_H
To copy to clipboard, switch view to plain text mode
scene.cpp:
#include "scene.h"
{
sceneMode = NoMode;
itemToDraw = 0;
}
void Scene::setMode(Mode mode){
sceneMode = mode;
if(mode == DrawLine){
makeItemsControllable(false);
}
else if(mode == SelectObject){
makeItemsControllable(true);
}
if(mView)
mView->setDragMode(vMode);
}
void Scene::makeItemsControllable(bool areControllable){
areControllable);
areControllable);
}
}
if(sceneMode == DrawLine)
origPoint = event->scenePos();
}
if(sceneMode == DrawLine){
if(!itemToDraw){
this->addItem(itemToDraw);
itemToDraw
->setPen
(QPen(Qt
::black,
3, Qt
::SolidLine));
itemToDraw->setPos(origPoint);
}
itemToDraw->setLine(0,0,
event->scenePos().x() - origPoint.x(),
event->scenePos().y() - origPoint.y());
}
else
}
itemToDraw = 0;
}
if(event->key() == Qt::Key_Delete)
removeItem(item);
delete item;
}
else
}
#include "scene.h"
Scene::Scene(QObject* parent): QGraphicsScene(parent)
{
sceneMode = NoMode;
itemToDraw = 0;
}
void Scene::setMode(Mode mode){
sceneMode = mode;
QGraphicsView::DragMode vMode =
QGraphicsView::NoDrag;
if(mode == DrawLine){
makeItemsControllable(false);
vMode = QGraphicsView::NoDrag;
}
else if(mode == SelectObject){
makeItemsControllable(true);
vMode = QGraphicsView::RubberBandDrag;
}
QGraphicsView* mView = views().at(0);
if(mView)
mView->setDragMode(vMode);
}
void Scene::makeItemsControllable(bool areControllable){
foreach(QGraphicsItem* item, items()){
item->setFlag(QGraphicsItem::ItemIsSelectable,
areControllable);
item->setFlag(QGraphicsItem::ItemIsMovable,
areControllable);
}
}
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event){
if(sceneMode == DrawLine)
origPoint = event->scenePos();
QGraphicsScene::mousePressEvent(event);
}
void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
if(sceneMode == DrawLine){
if(!itemToDraw){
itemToDraw = new QGraphicsLineItem;
this->addItem(itemToDraw);
itemToDraw->setPen(QPen(Qt::black, 3, Qt::SolidLine));
itemToDraw->setPos(origPoint);
}
itemToDraw->setLine(0,0,
event->scenePos().x() - origPoint.x(),
event->scenePos().y() - origPoint.y());
}
else
QGraphicsScene::mouseMoveEvent(event);
}
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
itemToDraw = 0;
QGraphicsScene::mouseReleaseEvent(event);
}
void Scene::keyPressEvent(QKeyEvent *event){
if(event->key() == Qt::Key_Delete)
foreach(QGraphicsItem* item, selectedItems()){
removeItem(item);
delete item;
}
else
QGraphicsScene::keyPressEvent(event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks