Results 1 to 2 of 2

Thread: Send action Codes to PushButton

  1. #1
    Join Date
    Jun 2016
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Send action Codes to PushButton

    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:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsView>
    6. #include <QToolBar>
    7. #include "scene.h"
    8. #include <QAction>
    9.  
    10.  
    11. namespace Ui {
    12. class MainWindow;
    13. }
    14.  
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit MainWindow(QWidget *parent = 0);
    21. ~MainWindow();
    22. public slots:
    23. void actionGroupClicked(QAction*);
    24. private:
    25. Ui::MainWindow *ui;
    26. Scene* scene;
    27.  
    28. void createActions();
    29. void createConnections();
    30. void createToolBar();
    31.  
    32. QAction* lineAction;
    33. QAction* selectAction;
    34. QActionGroup *actionGroup;
    35. QToolBar* drawingToolBar;
    36. };
    37.  
    38. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainWindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. scene = new Scene(this);
    11. scene->setSceneRect(0,0,200,200);
    12. ui->graphicsView->setScene(scene);
    13. ui->graphicsView->setRenderHints(QPainter::Antialiasing);
    14. //setCentralWidget(ui->graphicsView);
    15.  
    16. createActions();
    17. createConnections();
    18. createToolBar();
    19.  
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26. void MainWindow::createActions(){
    27. lineAction = new QAction("Draw line", this);
    28. lineAction->setData(int(Scene::DrawLine));
    29. lineAction->setIcon(QIcon(":/icons/line.png"));
    30. lineAction->setCheckable(true);
    31.  
    32. selectAction = new QAction("Select object", this);
    33. selectAction->setData(int(Scene::SelectObject));
    34. selectAction->setIcon(QIcon(":/icons/select.png"));
    35. selectAction->setCheckable(true);
    36.  
    37. actionGroup = new QActionGroup(this);
    38. actionGroup->setExclusive(true);
    39. actionGroup->addAction(lineAction);
    40. actionGroup->addAction(selectAction);
    41. }
    42.  
    43. void MainWindow::createConnections(){
    44. connect(actionGroup, SIGNAL(triggered(QAction*)),
    45. this, SLOT(actionGroupClicked(QAction*)));
    46. }
    47.  
    48. void MainWindow::actionGroupClicked(QAction *action){
    49. scene->setMode(Scene::Mode(action->data().toInt()));
    50. }
    51.  
    52. void MainWindow::createToolBar(){
    53. drawingToolBar = new QToolBar;
    54. addToolBar(Qt::TopToolBarArea, drawingToolBar);
    55. drawingToolBar->addAction(selectAction);
    56. drawingToolBar->addAction(lineAction);
    57. }
    To copy to clipboard, switch view to plain text mode 
    scene.h:
    Qt Code:
    1. #ifndef SCENE_H
    2. #define SCENE_H
    3.  
    4. #include <QGraphicsScene>
    5. #include <QGraphicsSceneMouseEvent>
    6. #include <QGraphicsLineItem>
    7. #include <QAction>
    8. #include <QGraphicsView>
    9. #include <QKeyEvent>
    10.  
    11. class Scene : public QGraphicsScene
    12. {
    13. public:
    14. scene();
    15. enum Mode {NoMode, SelectObject, DrawLine};
    16. Scene(QObject* parent = 0);
    17. void setMode(Mode mode);
    18. protected:
    19. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    20. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    21. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    22. void keyPressEvent(QKeyEvent *event);
    23. private:
    24. Mode sceneMode;
    25. QPointF origPoint;
    26. QGraphicsLineItem* itemToDraw;
    27. void makeItemsControllable(bool areControllable);
    28. };
    29.  
    30. #endif // SCENE_H
    To copy to clipboard, switch view to plain text mode 
    scene.cpp:
    Qt Code:
    1. #include "scene.h"
    2.  
    3. Scene::Scene(QObject* parent): QGraphicsScene(parent)
    4. {
    5. sceneMode = NoMode;
    6. itemToDraw = 0;
    7. }
    8.  
    9. void Scene::setMode(Mode mode){
    10. sceneMode = mode;
    11. QGraphicsView::DragMode vMode =
    12. QGraphicsView::NoDrag;
    13. if(mode == DrawLine){
    14. makeItemsControllable(false);
    15. vMode = QGraphicsView::NoDrag;
    16. }
    17. else if(mode == SelectObject){
    18. makeItemsControllable(true);
    19. vMode = QGraphicsView::RubberBandDrag;
    20. }
    21. QGraphicsView* mView = views().at(0);
    22. if(mView)
    23. mView->setDragMode(vMode);
    24. }
    25.  
    26. void Scene::makeItemsControllable(bool areControllable){
    27. foreach(QGraphicsItem* item, items()){
    28. item->setFlag(QGraphicsItem::ItemIsSelectable,
    29. areControllable);
    30. item->setFlag(QGraphicsItem::ItemIsMovable,
    31. areControllable);
    32. }
    33. }
    34.  
    35. void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event){
    36. if(sceneMode == DrawLine)
    37. origPoint = event->scenePos();
    38. QGraphicsScene::mousePressEvent(event);
    39. }
    40.  
    41. void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    42. if(sceneMode == DrawLine){
    43. if(!itemToDraw){
    44. itemToDraw = new QGraphicsLineItem;
    45. this->addItem(itemToDraw);
    46. itemToDraw->setPen(QPen(Qt::black, 3, Qt::SolidLine));
    47. itemToDraw->setPos(origPoint);
    48. }
    49. itemToDraw->setLine(0,0,
    50. event->scenePos().x() - origPoint.x(),
    51. event->scenePos().y() - origPoint.y());
    52. }
    53. else
    54. QGraphicsScene::mouseMoveEvent(event);
    55. }
    56.  
    57. void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    58. itemToDraw = 0;
    59. QGraphicsScene::mouseReleaseEvent(event);
    60. }
    61.  
    62. void Scene::keyPressEvent(QKeyEvent *event){
    63. if(event->key() == Qt::Key_Delete)
    64. foreach(QGraphicsItem* item, selectedItems()){
    65. removeItem(item);
    66. delete item;
    67. }
    68. else
    69. QGraphicsScene::keyPressEvent(event);
    70. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 2nd June 2016 at 22:33. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Send action Codes to PushButton

    Store the data value in a dynamic property of the button, or use a QSginalMapper to map from the button click to a specific int value.
    Or connect the button's clicked() signal to the action's trigger() slot.

    Cheers,
    _

Similar Threads

  1. Replies: 7
    Last Post: 20th September 2014, 12:03
  2. Replies: 2
    Last Post: 22nd April 2011, 00:39
  3. Replies: 5
    Last Post: 27th April 2009, 23:29
  4. pushbutton action
    By mickey in forum Newbie
    Replies: 9
    Last Post: 21st April 2006, 19:46
  5. No action checked in an exclusive action group
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 06:19

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.