Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Send action Codes to PushButton
QtWS25 Last Chance

Send action Codes to PushButton

Scheduled Pinned Locked Moved Solved General and Desktop
actionpush buttondrawlines
10 Posts 3 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M4RZB4Ni
    wrote on 2 Jun 2016, 17:08 last edited by M4RZB4Ni 6 Feb 2016, 17:09
    #1

    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;
    }
    
    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
    

    mainWindow.cpp:

    #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);
    }
    

    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
    
    

    scene.cpp:

    #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);
    }
    
    

    Thanks
    M4RZB4Ni

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Maxim DC
      wrote on 2 Jun 2016, 17:41 last edited by
      #2

      You could subclass the Button and then add an enum to define what to draw an make a variable of it, in the drawing function, do a switch case statement for every draw type on that one variable.

      M 1 Reply Last reply 2 Jun 2016, 17:58
      0
      • M Maxim DC
        2 Jun 2016, 17:41

        You could subclass the Button and then add an enum to define what to draw an make a variable of it, in the drawing function, do a switch case statement for every draw type on that one variable.

        M Offline
        M Offline
        M4RZB4Ni
        wrote on 2 Jun 2016, 17:58 last edited by
        #3

        @Maxim-DC
        i dont know what are you saying
        Exactly
        can you edit code please?

        Thanks
        M4RZB4Ni

        M 1 Reply Last reply 2 Jun 2016, 18:28
        0
        • M M4RZB4Ni
          2 Jun 2016, 17:58

          @Maxim-DC
          i dont know what are you saying
          Exactly
          can you edit code please?

          M Offline
          M Offline
          Maxim DC
          wrote on 2 Jun 2016, 18:28 last edited by
          #4
          This post is deleted!
          M 1 Reply Last reply 2 Jun 2016, 18:31
          0
          • M Maxim DC
            2 Jun 2016, 18:28

            This post is deleted!

            M Offline
            M Offline
            M4RZB4Ni
            wrote on 2 Jun 2016, 18:31 last edited by
            #5

            @Maxim-DC
            i have a graphicView and i want to draw line on it and erase lines
            what i must do?
            can you write a example for me?
            thanks:)

            Thanks
            M4RZB4Ni

            M 1 Reply Last reply 2 Jun 2016, 18:47
            0
            • M M4RZB4Ni
              2 Jun 2016, 18:31

              @Maxim-DC
              i have a graphicView and i want to draw line on it and erase lines
              what i must do?
              can you write a example for me?
              thanks:)

              M Offline
              M Offline
              Maxim DC
              wrote on 2 Jun 2016, 18:47 last edited by
              #6

              @M4RZB4Ni

              I think this will work, but i haven't tested it yet

              // view.h
              ...
              class View: public QGraphicsView
              {
              public:
                  View();
                  enum DrawType { Draw, Erase };
                  void paintEvent(QPaintEvent *) override;
                  
                  View::DrawType type;
              
              public slots:
                  void toggleType();
              private:
                  QPushButton button;
                  QPainter painter;
              };
              
              // view.cpp
              #include <view.h>
              
              View::View()
              {
                  type = View::Draw;
              
                  QObject::connect(&button, SIGNAL(clicked(bool)), this, SLOT(toggleType()));
              }
              
              void View::paintEvent(QPaintEvent *) 
              {
                  painter.restore()
                  switch (type)
                  {
                      case: View::Draw: painter.drawPoint(10, 10);
                      case: View::Erase: painter.eraseRect(10, 10, 100, 100)
                  }
                  painter.save();
              }
              
              void View::toggleType()
              {
                  switch (type)
                  {
                      case: View::Draw: type = View::Erase
                      case: View::Erase: type = View::Draw
                  }
              }
              
              M 1 Reply Last reply 2 Jun 2016, 19:21
              1
              • M Maxim DC
                2 Jun 2016, 18:47

                @M4RZB4Ni

                I think this will work, but i haven't tested it yet

                // view.h
                ...
                class View: public QGraphicsView
                {
                public:
                    View();
                    enum DrawType { Draw, Erase };
                    void paintEvent(QPaintEvent *) override;
                    
                    View::DrawType type;
                
                public slots:
                    void toggleType();
                private:
                    QPushButton button;
                    QPainter painter;
                };
                
                // view.cpp
                #include <view.h>
                
                View::View()
                {
                    type = View::Draw;
                
                    QObject::connect(&button, SIGNAL(clicked(bool)), this, SLOT(toggleType()));
                }
                
                void View::paintEvent(QPaintEvent *) 
                {
                    painter.restore()
                    switch (type)
                    {
                        case: View::Draw: painter.drawPoint(10, 10);
                        case: View::Erase: painter.eraseRect(10, 10, 100, 100)
                    }
                    painter.save();
                }
                
                void View::toggleType()
                {
                    switch (type)
                    {
                        case: View::Draw: type = View::Erase
                        case: View::Erase: type = View::Draw
                    }
                }
                
                M Offline
                M Offline
                M4RZB4Ni
                wrote on 2 Jun 2016, 19:21 last edited by
                #7

                @Maxim-DC
                code showed a error !
                error: 'void Scene::paintEvent(QPaintEvent*)' marked override, but does not override
                void paintEvent(QPaintEvent *) override;
                ^

                Thanks
                M4RZB4Ni

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 2 Jun 2016, 20:39 last edited by
                  #8

                  Hi,

                  Why not use QGraphicsLineItem ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply 2 Jun 2016, 22:22
                  0
                  • S SGaist
                    2 Jun 2016, 20:39

                    Hi,

                    Why not use QGraphicsLineItem ?

                    M Offline
                    M Offline
                    M4RZB4Ni
                    wrote on 2 Jun 2016, 22:22 last edited by M4RZB4Ni 6 Feb 2016, 22:25
                    #9

                    @SGaist
                    Hi,
                    Because id dont know how i must use it!
                    for my graphicView!
                    i want to draw line with mouse!

                    Thanks
                    M4RZB4Ni

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 3 Jun 2016, 20:03 last edited by
                      #10

                      You could use a QRubberBand to setup the line and once you release it add the QGraphicsLineItem to your scene.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      10/10

                      3 Jun 2016, 20:03

                      • Login

                      • Login or register to search.
                      10 out of 10
                      • First post
                        10/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved