Results 1 to 9 of 9

Thread: single timer for multiple objects

  1. #1
    Join Date
    Aug 2013
    Location
    India
    Posts
    44
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default single timer for multiple objects

    hello everyone i am new in qt. i made a gui in qt in which on mousepressevent i am creating a object of button and on button i am adding a image .
    when will be user clicked on mousepress a button will be appear on screen.now on a button click i want to rotate this image according to timer.
    every image will be rotate on buttonclick.i am attaching my code with this.pleaase give me a solution how i will use a time for multiple objects.


    this is .h file
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QMainWindow>
    6. #include<QMouseEvent>
    7. #include <QPushButton>
    8. #include<QLabel>
    9.  
    10. namespace Ui
    11. {
    12. class MainWindow;
    13. class MyWidget;
    14. }
    15.  
    16. class MainWindow : public QMainWindow
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. MainWindow(QWidget *parent = 0);
    22. ~MainWindow();
    23.  
    24. bool inc;
    25. void paintEvent(QPaintEvent * event);
    26. void mousePressEvent(QMouseEvent *ev);
    27. void mouseMoveEvent ( QMouseEvent * event );
    28. double x[100];
    29. double y[100];
    30. QTimer *timer;
    31.  
    32. private:
    33. Ui::MainWindow *ui;
    34.  
    35. public slots:
    36.  
    37. void upd();
    38.  
    39. private slots:
    40. void on_pushButton_clicked();
    41. };
    To copy to clipboard, switch view to plain text mode 


    this is .cpp file

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include<QPainter>
    4. #include<QPushButton>
    5. #include<QMessageBox>
    6. #include<QDebug>
    7. #include<QLayout>
    8. #include<QToolTip>
    9. #include<QTimer>
    10.  
    11.  
    12.  
    13. int Ship_Cont =0;
    14.  
    15. QPushButton* button[100];
    16.  
    17.  
    18. bool Ship_Flag = true;
    19.  
    20. //double x[100];
    21. //double y[100];
    22.  
    23. MainWindow::MainWindow(QWidget *parent)
    24. : QMainWindow(parent), ui(new Ui::MainWindow)
    25. {
    26.  
    27. inc =false;
    28.  
    29. timer = new QTimer(this);
    30. timer->setInterval(1000);
    31. timer->start();
    32. connect(timer, SIGNAL(timeout()), this, SLOT(upd()));
    33.  
    34.  
    35.  
    36. }
    37.  
    38.  
    39.  
    40.  
    41. MainWindow::~MainWindow()
    42. {
    43. delete ui;
    44. }
    45.  
    46. void MainWindow::paintEvent(QPaintEvent *event)
    47. {
    48. QPainter painter(this);
    49. painter.setBrush(Qt::black);
    50. painter.drawRect(0,0,500,500);
    51.  
    52. if(Ship_Flag == false)
    53. {
    54. for (int j =1 ;j<= Ship_Cont; j++)
    55. {
    56.  
    57. button[j]->setGeometry(QRect(x[j] , y[j] , 12, 25));
    58.  
    59. button[j]->show();
    60. }
    61. // Ship_Flag = true;
    62. }
    63.  
    64.  
    65.  
    66. }
    67.  
    68. void MainWindow::mousePressEvent(QMouseEvent *ev)
    69. {
    70. Ship_Cont++;
    71.  
    72. QIcon icon("C:/Users/Pankaj/Desktop/ship1.jpg");
    73. button[Ship_Cont] = new QPushButton(this);
    74. button[Ship_Cont]->setIcon(icon);
    75. button[Ship_Cont]->setStyleSheet("background-color: black");
    76.  
    77. button[Ship_Cont]->setStyleSheet( "border:none");
    78.  
    79. x[Ship_Cont] = ev->pos().x();
    80. y [Ship_Cont]= ev->pos().y();
    81.  
    82. // qDebug()<<"Ship_Cont";
    83. // qDebug()<<Ship_Cont;
    84.  
    85.  
    86. button[Ship_Cont]->setGeometry(QRect(x[Ship_Cont] , y[Ship_Cont] , 12, 25));
    87.  
    88. button[Ship_Cont]->show();
    89.  
    90.  
    91.  
    92.  
    93.  
    94. }
    95.  
    96. void MainWindow::mouseMoveEvent ( QMouseEvent * event )
    97. {
    98. QToolTip::showText(event->globalPos(), QString::number( event->pos().x() ) + ", " + QString::number( event->pos().y() ) + ", " + "prabhat ",this, rect() );
    99. }
    100.  
    101. void MainWindow::upd()
    102. {
    103. if (inc == true)
    104. {
    105.  
    106.  
    107. for(int i =1; i<=Ship_Cont;i++)
    108. {
    109. qDebug()<<"X";
    110. qDebug()<< Ship_Cont;
    111. qDebug()<< x[Ship_Cont];
    112. qDebug()<<"Y";
    113. qDebug()<< Ship_Cont;
    114. qDebug()<< y[Ship_Cont];
    115. x[i] = x[i]+10;
    116.  
    117. y[i] = y[i] +10;
    118. }
    119. update();
    120. Ship_Flag = false;
    121. }
    122. }
    123.  
    124.  
    125. void MainWindow::on_pushButton_clicked()
    126. {
    127. inc = true;
    128.  
    129. }
    To copy to clipboard, switch view to plain text mode 

    this is main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QMainWindow *window = new QMainWindow();
    9.  
    10.  
    11. MainWindow w;
    12. w.show();
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 29th August 2013 at 09:43. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: single timer for multiple objects

    This not a solution but just some pointer to how to go about
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QMouseEvent>
    6. #include <QPushButton>
    7. #include <QDebug>
    8. #include <QLabel>
    9. #include <QList>
    10.  
    11. class RotatingButton : public QPushButton
    12. {
    13. Q_OBJECT
    14. public :
    15. explicit RotatingButton(int x, int y, QWidget * parent = 0)
    16. : QPushButton(parent)
    17. , rect(x, y, 12, 25)
    18. {
    19. QIcon icon("C:/Users/Pankaj/Desktop/ship1.jpg");
    20. setIcon(icon);
    21. setStyleSheet("{"
    22. " background-color: black"
    23. " border:none"
    24. "}");
    25.  
    26. setGeometry(rect);
    27.  
    28. connect(this, SIGNAL(clicked()), SLOT(rotate()));
    29. }
    30.  
    31. private slots:
    32. void rotate(void)
    33. {
    34. int w = rect.width();
    35. int h = rect.height();
    36. qSwap(h, w);
    37. rect.setWidth(w);
    38. rect.setHeight(h);
    39.  
    40. setGeometry(rect);
    41. qDebug() << "rotate" << rect;
    42. }
    43.  
    44. void move(void)
    45. {
    46. rect.adjust(10, 10, 10, 10);
    47. setGeometry(rect);
    48. qDebug() << "move" << rect;
    49. }
    50.  
    51. private:
    52. QRect rect;
    53. };
    54.  
    55. class MainWindow : public QMainWindow
    56. {
    57. Q_OBJECT
    58.  
    59. public:
    60. MainWindow(QWidget *parent = 0);
    61. ~MainWindow();
    62.  
    63. bool inc;
    64.  
    65. protected:
    66. void paintEvent(QPaintEvent * event);
    67. void mousePressEvent(QMouseEvent *ev);
    68. void mouseMoveEvent(QMouseEvent * event);
    69.  
    70. private:
    71. QWidget * centerWidget;
    72. QTimer * timer;
    73. QList<RotatingButton *> buttons;
    74. };
    75.  
    76. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QPainter>
    3. #include <QPushButton>
    4. #include <QMessageBox>
    5. #include <QDebug>
    6. #include <QLayout>
    7. #include <QToolTip>
    8. #include <QTimer>
    9.  
    10. MainWindow::MainWindow(QWidget * parent)
    11. : QMainWindow(parent)
    12. {
    13. centerWidget = new QWidget;
    14. setCentralWidget(centerWidget);
    15.  
    16. timer = new QTimer(this);
    17. timer->setInterval(1000);
    18. timer->start();
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. ;
    24. }
    25.  
    26. void MainWindow::paintEvent(QPaintEvent *event)
    27. {
    28. QPainter painter(this);
    29. painter.setBrush(Qt::black);
    30. painter.drawRect(0,0,500,500);
    31.  
    32. QMainWindow::paintEvent(event);
    33. }
    34.  
    35. void MainWindow::mousePressEvent(QMouseEvent *ev)
    36. {
    37. RotatingButton * button = new RotatingButton(ev->pos().x(), ev->pos().y(), centerWidget);
    38. connect(timer, SIGNAL(timeout()), button, SLOT(move()));
    39. buttons.append(button);
    40. button->show();
    41. }
    42.  
    43. void MainWindow::mouseMoveEvent(QMouseEvent * event)
    44. {
    45. QMainWindow::mouseMoveEvent(event);
    46. QToolTip::showText(event->globalPos(), QString::number(event->pos().x() ) + ", " + QString::number( event->pos().y() ) + ", " + "prabhat ",this, rect() );
    47. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. MainWindow w;
    9. w.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 29th August 2013 at 09:26.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Aug 2013
    Location
    India
    Posts
    44
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: single timer for multiple objects

    thanks a lot ....for giving such a idea.
    but this was not my problem actually i want when user will press the mouse a button(image) will be appear on screen and it will move when user will cllick on move button.on move button my timer will be start .and every button(image) will be move when user click on move button.lets we press mouse first time a button image will be appear on screen and now when we will click move button the image will start to rotate.and now we pressed again mouse an other button(image) will be appear on screen and it will not move until user click on move button.please give me some solution.thanks in advance.

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: single timer for multiple objects

    one way is
    QPropertyAnimation
    QPropertyAnimation* anim = new QPropertyAnimation(button, "rotation");
    http://qt-project.org/doc/qt-4.8/sta...ty-assignments
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: single timer for multiple objects

    So where is code for move push button? If that is you question, where do you want to put the move push button?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: single timer for multiple objects

    direct rotation on QPushButton is not possible
    see this sample code how to rotate a pushButton
    Qt Code:
    1. int main(int argc, char** argv)
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. QGraphicsScene *scene = new QGraphicsScene(-100, -100, 200, 200, &view);
    6. view.setScene(scene);
    7. view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    8.  
    9. QIcon icon;
    10. icon.addPixmap(QPixmap(":/Images/minitools.png"));
    11. QPushButton *rotateButton = new QPushButton();
    12. rotateButton->setFixedSize(60, 50);
    13. rotateButton->setIcon(icon);
    14. rotateButton->setIconSize(QSize(rotateButton->width()-2, rotateButton->height()-2));
    15. QGraphicsProxyWidget *rotateItem = scene->addWidget(rotateButton);
    16. rotateItem->setTransformOriginPoint(rotateButton->width()/2,
    17. rotateButton->height()/2);
    18. rotateItem->setPos(-rotateButton->width()/2, 10);
    19.  
    20. QPropertyAnimation *rotateAnimation =
    21. new QPropertyAnimation(rotateItem, "rotation", &view);
    22. rotateAnimation->setStartValue(0.0);
    23. rotateAnimation->setEndValue(360.0);
    24. rotateAnimation->setDuration(2000);
    25. rotateAnimation->setEasingCurve(QEasingCurve::OutBounce);
    26. QObject::connect(rotateButton, SIGNAL(clicked()),
    27. rotateAnimation, SLOT(start()));
    28. view.show();
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wagmare; 29th August 2013 at 11:03. Reason: setFixedSize
    "Behind every great fortune lies a crime" - Balzac

  7. #7
    Join Date
    Aug 2013
    Location
    India
    Posts
    44
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: single timer for multiple objects

    my move pushbutton is on mainwindow ....and timer will be start on this button please look a view on give code...on pushbuttonclicked bool inc will be true and when boll inc will be true the my timer will be start.


    Added after 5 minutes:


    hello santosh sir may i get ur mail id...
    i can send my code on dis..
    Last edited by prabhatjha; 29th August 2013 at 11:12.

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: single timer for multiple objects

    Can't you post it here?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Aug 2013
    Location
    India
    Posts
    44
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: single timer for multiple objects

    i am adding my code ..please give a look on this.


    this is .h file

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QMainWindow>
    6. #include<QMouseEvent>
    7. #include <QPushButton>
    8. #include<QLabel>
    9.  
    10. namespace Ui
    11. {
    12. class MainWindow;
    13. class MyWidget;
    14. }
    15.  
    16. class MainWindow : public QMainWindow
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. MainWindow(QWidget *parent = 0);
    22. ~MainWindow();
    23.  
    24. bool inc;
    25. void paintEvent(QPaintEvent * event);
    26. void mousePressEvent(QMouseEvent *ev);
    27. void mouseMoveEvent ( QMouseEvent * event );
    28. double x[100];
    29. double y[100];
    30. QTimer *timer;
    31.  
    32. private:
    33. Ui::MainWindow *ui;
    34.  
    35. public slots:
    36.  
    37. void upd();
    38.  
    39. private slots:
    40. void on_pushButton_clicked();
    41. };
    42.  
    43. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    this is .cpp file

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include<QPainter>
    4. #include<QPushButton>
    5. #include<QMessageBox>
    6. #include<QDebug>
    7. #include<QLayout>
    8. #include<QToolTip>
    9. #include<QTimer>
    10.  
    11.  
    12.  
    13. int Ship_Cont =0;
    14.  
    15. QPushButton* button[100];
    16.  
    17.  
    18. bool Ship_Flag = true;
    19.  
    20. //double x[100];
    21. //double y[100];
    22.  
    23. MainWindow::MainWindow(QWidget *parent)
    24. : QMainWindow(parent), ui(new Ui::MainWindow)
    25. {
    26.  
    27.  
    28. MyWidget *wid;
    29. ui->setupUi(this);
    30. wid = ui->widget;
    31.  
    32. inc =false;
    33.  
    34.  
    35. QRect *rect = new QRect(0,0,500,500);
    36. QRegion* region = new QRegion(*rect,QRegion::Ellipse);
    37. wid->setMask(*region);
    38. // wid->setStyleSheet("background-color: yellow");
    39. // connect(ui->pushButton,SIGNAL(clicked()),wid,SLOT(Button()));
    40. // connect(ui->pushButton_2,SIGNAL(clicked()),wid,SLOT(Button1()));
    41.  
    42.  
    43. timer = new QTimer(this);
    44. timer->setInterval(1000);
    45. timer->start();
    46. connect(timer, SIGNAL(timeout()), this, SLOT(upd()));
    47.  
    48.  
    49.  
    50. }
    51.  
    52. MainWindow::~MainWindow()
    53. {
    54. delete ui;
    55. }
    56.  
    57. void MainWindow::paintEvent(QPaintEvent *event)
    58. {
    59. QPainter painter(this);
    60. painter.setBrush(Qt::black);
    61. painter.drawRect(0,0,500,500);
    62.  
    63. if(Ship_Flag == false)
    64. {
    65. for (int j =1 ;j<= Ship_Cont; j++)
    66. {
    67.  
    68. button[j]->setGeometry(QRect(x[j] , y[j] , 12, 25));
    69.  
    70. button[j]->show();
    71. }
    72. }
    73.  
    74. }
    75.  
    76. void MainWindow::mousePressEvent(QMouseEvent *ev)
    77. {
    78. Ship_Cont++;
    79. for(int i = 1 ; i<= Ship_Cont;i++)
    80. {
    81. QIcon icon("C:/Users/Pankaj/Desktop/ship1.jpg");
    82. button[Ship_Cont] = new QPushButton(this);
    83. button[Ship_Cont]->setIcon(icon);
    84. button[Ship_Cont]->setStyleSheet("background-color: black");
    85.  
    86. button[Ship_Cont]->setStyleSheet( "border:none");
    87.  
    88. x[Ship_Cont] = ev->pos().x();
    89. y [Ship_Cont]= ev->pos().y();
    90.  
    91. // qDebug()<<"Ship_Cont";
    92. // qDebug()<<Ship_Cont;
    93.  
    94.  
    95. button[Ship_Cont]->setGeometry(QRect(x[Ship_Cont] , y[Ship_Cont] , 12, 25));
    96.  
    97. button[Ship_Cont]->show();
    98. }
    99.  
    100.  
    101. }
    102.  
    103. void MainWindow::mouseMoveEvent ( QMouseEvent * event )
    104. {
    105. QToolTip::showText(event->globalPos(), QString::number( event->pos().x() ) + ", " + QString::number( event->pos().y() ) + ", " + "prabhat ",this, rect() );
    106. }
    107.  
    108. void MainWindow::upd()
    109. {
    110. if (inc == true)
    111. {
    112.  
    113.  
    114. for(int i =1; i<=Ship_Cont;i++)
    115. {
    116. qDebug()<<"X";
    117. qDebug()<< Ship_Cont;
    118. qDebug()<< x[Ship_Cont];
    119. qDebug()<<"Y";
    120. qDebug()<< Ship_Cont;
    121. qDebug()<< y[Ship_Cont];
    122. x[i] = x[i]+10;
    123.  
    124. y[i] = y[i] +10;
    125. }
    126. update();
    127. Ship_Flag = false;
    128. }
    129. }
    130.  
    131.  
    132. void MainWindow::on_pushButton_clicked()
    133. {
    134. inc = true;
    135.  
    136. }
    To copy to clipboard, switch view to plain text mode 


    this is main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QMainWindow *window = new QMainWindow();
    9.  
    10.  
    11. MainWindow w;
    12. w.show();
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 


    in this programm in mainwindow constructor i added timer and added SLOT (upd) with this.
    on pushbutton clicked i did true the inc variable.
    when inc will be true my timer will be start.In upd function value of x and y will be increase with 10 every time.
    and now in paintevet i am passing update value of x and y so, button is moving. but i want every button will start to move when pushbutton will be click.
    Last edited by wysota; 30th August 2013 at 09:06. Reason: missing [code] tags

Similar Threads

  1. Multiple model to single view
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 4
    Last Post: 9th March 2012, 09:57
  2. multiple table in a single variable
    By sattu in forum Qt Programming
    Replies: 0
    Last Post: 1st March 2011, 11:01
  3. Should I use multiple or a single model(s)?
    By Cotlone in forum Newbie
    Replies: 2
    Last Post: 7th May 2010, 03:39
  4. Multiple appearances in a single ui file
    By zgulser in forum Qt Tools
    Replies: 1
    Last Post: 5th June 2009, 19:12
  5. Replies: 2
    Last Post: 16th April 2008, 10:31

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.