Results 1 to 9 of 9

Thread: single timer for multiple objects

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.