Results 1 to 9 of 9

Thread: single timer for multiple objects

Hybrid View

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

    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 12:12.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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.

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

    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 10: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, 10:57
  2. multiple table in a single variable
    By sattu in forum Qt Programming
    Replies: 0
    Last Post: 1st March 2011, 12:01
  3. Should I use multiple or a single model(s)?
    By Cotlone in forum Newbie
    Replies: 2
    Last Post: 7th May 2010, 04:39
  4. Multiple appearances in a single ui file
    By zgulser in forum Qt Tools
    Replies: 1
    Last Post: 5th June 2009, 20:12
  5. Replies: 2
    Last Post: 16th April 2008, 11: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
  •  
Qt is a trademark of The Qt Company.