Results 1 to 4 of 4

Thread: Question about using of static QTimer::singleShot() function

  1. #1
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question Question about using of static QTimer::singleShot() function

    Hello!

    I'm trying to use the static QTimer::singleShot() function to create delay between write and read cycles via serial interface.

    I have done the following code:

    Hedaer:
    Qt Code:
    1. #ifndef CAN_TRANSFER_H
    2. #define CAN_TRANSFER_H
    3.  
    4. #include <QMainWindow>
    5. #include "qextserialport.h"
    6. #include <Qtimer.h>
    7.  
    8.  
    9. namespace Ui {
    10. class CAN_transfer;
    11.  
    12. }
    13.  
    14. class CAN_transfer : public QMainWindow {
    15. Q_OBJECT
    16.  
    17. public:
    18. CAN_transfer(QWidget *parent = 0);
    19. ~CAN_transfer();
    20.  
    21.  
    22. protected:
    23. void changeEvent(QEvent *e);
    24.  
    25. private:
    26. Ui::CAN_transfer *ui;
    27. QextSerialPort* _comPort;
    28. unsigned char mas[64];
    29. unsigned char* DATA_pointer;
    30.  
    31. private slots:
    32.  
    33. void readBR(unsigned char t);
    34. void on_store_all_button_clicked();
    35. void on_store_sel_button_clicked();
    36. void on_connectbutton_clicked();
    37. void timer_event();
    38. };
    39.  
    40. #endif // CAN_TRANSFER_H
    To copy to clipboard, switch view to plain text mode 
    Source:

    Qt Code:
    1. #include "can_transfer.h"
    2. #include "ui_can_transfer.h"
    3. #include "qextserialport.h"
    4. #include "win_qextserialport.h"
    5. //#include <Qtimer.h>
    6. #include <QTimer>
    7. unsigned char clkdt,te;
    8. CAN_transfer::CAN_transfer(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::CAN_transfer)
    11. {
    12. ui->setupUi(this);
    13. ui->logw->setReadOnly(true);
    14. ui->logw->setFontWeight(QFont::Bold);
    15. ui->logw->append(tr("CAN transfer configurator is ready to use!"));
    16. ui->logw->setFontWeight(QFont::QFont::Normal);
    17. ui->logw->setTextColor(Qt::black);
    18. QString s="COM1";
    19. _comPort = new QextSerialPort(s);
    20. _comPort->setBaudRate(BAUD9600);
    21. _comPort->setFlowControl(FLOW_OFF);
    22. _comPort->setParity(PAR_NONE);
    23. _comPort->setDataBits(DATA_8);
    24. _comPort->setStopBits(STOP_1);
    25. DATA_pointer = &mas[0];
    26. clkdt = 0;
    27. }
    28.  
    29. CAN_transfer::~CAN_transfer()
    30. {
    31. _comPort->close();
    32. delete(_comPort);
    33. delete ui;
    34. }
    35.  
    36. void CAN_transfer::changeEvent(QEvent *e)
    37. {
    38. QMainWindow::changeEvent(e);
    39. switch (e->type()) {
    40. case QEvent::LanguageChange:
    41. ui->retranslateUi(this);
    42. break;
    43. default:
    44. break;
    45. }
    46. }
    47.  
    48.  
    49. void CAN_transfer :: timer_event()
    50. {
    51. te = 0;
    52. timer->stop();
    53. }
    54. void CAN_transfer::on_connectbutton_clicked()
    55. {unsigned char rb=0;
    56. ui->logw->append(tr("COM1 configurating...."));
    57. _comPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
    58. ui->logw->append(tr("conneting...."));
    59. *DATA_pointer = 0x55;
    60. if(_comPort->write((const char*)DATA_pointer, 1))
    61. {
    62.  
    63. te = 1;
    64. QTimer :: singleShot(100, this, SLOT(timer_event()));
    65. while(te);
    66. rb = _comPort->bytesAvailable();
    67. if(rb)
    68. {
    69. _comPort->read((char*)DATA_pointer,rb);
    70. if(*DATA_pointer==0xAA)
    71. ui->logw->append(tr("connection established!"));
    72. }
    73. else
    74. {
    75. ui->logw->append(tr("no connection"));
    76. }
    77.  
    78. }
    79.  
    80. statusBar()->showMessage(tr("Connected"));
    81. //_comPort->close();
    82. }
    To copy to clipboard, switch view to plain text mode 

    The problem is in that what application do not affect the SLOT(event_timer) code, at least it seems to be.

    Compiler don not issue any warning or errors and I casue of that I don't understand in what the problem is.

    I need some help to solve this problem, I would glad of any information about it.


    Thanks in advance

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about using of static QTimer::singleShot() function

    Why not use the QThread::msleep() function for that?
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about using of static QTimer::singleShot() function

    Problem is with line 65. With this construction You disable event dispatcher. Yours loop must be like this :
    Qt Code:
    1. while(te)
    2. QCoreApplication::processEvents();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about using of static QTimer::singleShot() function

    Quote Originally Posted by Lesiok View Post
    Problem is with line 65. With this construction You disable event dispatcher. Yours loop must be like this :
    Qt Code:
    1. while(te)
    2. QCoreApplication::processEvents();
    To copy to clipboard, switch view to plain text mode 
    Thank You very much, finally it works!

Similar Threads

  1. Replies: 8
    Last Post: 10th December 2009, 10:06
  2. multiple QTimer::singleShot() calls?
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2009, 19:22
  3. Replies: 22
    Last Post: 8th October 2008, 13:54
  4. Can we connect QTimer::SingleShot with a slot taking arguments?
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2008, 18:02
  5. Replies: 1
    Last Post: 6th April 2006, 12:24

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.