Results 1 to 9 of 9

Thread: Application windows lags while loop doing.

  1. #1
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Application windows lags while loop doing.

    Hi.

    At first ... sorry for my language skill...

    I need your help... I'm trying to write simple application (bot for game) in last version of QT program. First "more advanced" function (Tasker) works, but all application windows lags after function call.

    Main window open Tasker window by this code ...

    Qt Code:
    1. void MainWindow::on_actionTasker_activated()
    2. {
    3.  
    4. AddressBook *tasker = new AddressBook();
    5. tasker->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    ... it look's like on this picture:


    ... and like on second picture while Tasker is runned (checkbox is sticked) ...




    After closing application (ctrl + alt + delete) in "application messages" have showed:

    Qt Code:
    1. C:\Users\Wojtek\Desktop\bot_gui\bot-build-desktop\release\bot.exe closed with code -805306369
    To copy to clipboard, switch view to plain text mode 

    I've attached tasker code, I hope you won't kill me that I don't have too much knowledge to write this application :P


    addressbook.h
    Qt Code:
    1. #ifndef ADDRESSBOOK_H
    2. #define ADDRESSBOOK_H
    3.  
    4. #include <QWidget>
    5. #include <QMap>
    6. #include <QMessageBox>
    7. #include <QString>
    8.  
    9. namespace Ui {
    10. class AddressBook;
    11. }
    12.  
    13. class AddressBook : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit AddressBook(QWidget *parent = 0);
    19. ~AddressBook();
    20.  
    21. private:
    22. Ui::AddressBook *ui;
    23.  
    24. public slots:
    25.  
    26. void submitContact();
    27. int checkBox();
    28. //void podzial_liter ();
    29. int wielka_litera(char);
    30. void wiadomosc(char *wsk, int dl);
    31. void tasker();
    32. //void wiadomosc();
    33.  
    34. };
    35.  
    36. #endif // ADDRESSBOOK_H
    To copy to clipboard, switch view to plain text mode 

    addressbook.cpp
    Qt Code:
    1. #include "addressbook.h"
    2. #include "ui_addressbook.h"
    3. #include "mainwindow.h"
    4. #include "mainwindow.cpp"
    5. #include <stdio.h>
    6. #include <iostream>
    7. #include <windows.h>
    8. #include <conio.h>
    9. #include <string.h>
    10. #include <QtGui>
    11. #include <QString>
    12. #include <QStringList>
    13.  
    14.  
    15. static const int EXPERIENCE = 0x49A00C;
    16. static const int LEVEL = 0x49A008;
    17. static const int MANA = 0x499FF8;
    18. static const int HP = 0x49A014;
    19.  
    20. using namespace std;
    21.  
    22. int oldName;
    23. QString oldAddress;
    24.  
    25. AddressBook::AddressBook(QWidget *parent) :
    26. QWidget(parent),
    27. ui(new Ui::AddressBook)
    28. {
    29. ui->setupUi(this);
    30. Qt::WindowFlags flags;
    31. flags = Qt::Window | Qt::WindowMinimizeButtonHint;
    32. setWindowFlags( flags );
    33. connect(ui->OK, SIGNAL(clicked()), this,SLOT(submitContact()));
    34. connect(ui->checkBox, SIGNAL(clicked()), this,SLOT(checkBox()));
    35.  
    36. }
    37.  
    38. AddressBook::~AddressBook()
    39. {
    40. delete ui;
    41. }
    42.  
    43. int AddressBook::wielka_litera (char znak) {
    44. if ((int(znak) >= 33 && int(znak) <=43) || (int(znak) >= 58 && int(znak) <= 90)) return 1;
    45. else return 0;
    46. }
    47.  
    48. void AddressBook::wiadomosc (char *wsk, int dl)
    49. {
    50.  
    51. char znak;
    52.  
    53. for(int i = 0; i < dl; i++)
    54. {
    55. znak=wsk[i];
    56.  
    57. Sleep(100);
    58.  
    59. if (wielka_litera(znak)) keybd_event(VK_SHIFT, 0, 0, 0);
    60.  
    61.  
    62. keybd_event(VkKeyScan(znak), 0, 0, 0);
    63. keybd_event(VkKeyScan(znak), 0, KEYEVENTF_KEYUP, 0);
    64.  
    65. if (wielka_litera(znak)) keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    66.  
    67. }
    68. keybd_event(VK_RETURN, 0, 0, 0);
    69. keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
    70. }
    71.  
    72. void AddressBook::tasker () {
    73.  
    74. std::string message = oldAddress.toStdString();
    75. //string message=std::string((const char*)oldAddress);
    76. //string message = oldAdress;
    77.  
    78. for (int i=0;i<=oldName;i=i+6) {
    79. Sleep(60);
    80. }
    81.  
    82. int dlugosc = message.length();
    83. char *message_char = new char[dlugosc+1];
    84. strcpy(message_char, message.c_str());
    85.  
    86. wiadomosc (message_char,dlugosc);
    87. }
    88.  
    89.  
    90. int AddressBook::checkBox() {
    91. Qt::CheckState state;
    92.  
    93. state = (ui->checkBox)->checkState();
    94. if ( state == Qt::Checked ) return 1;
    95. else return 0;
    96. }
    97.  
    98. void AddressBook::submitContact()
    99. {
    100. oldName = ui->time->value();
    101. oldAddress = ui->message->text();
    102. while (checkBox()==1) tasker();
    103. }
    To copy to clipboard, switch view to plain text mode 

    Please help me
    PS. AddressBook - strange name because I've edited example of Address book included to QT library (I'm too lazy to change these names) :P
    Last edited by Wojtek1990; 22nd July 2010 at 20:05.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Application windows lags while loop doing.

    You have to delete the address book after you have used it otherwise you get memory leaks. Or you can let Qt do it for you passing a parent:
    Qt Code:
    1. AddressBook *tasker = new AddressBook(this);
    To copy to clipboard, switch view to plain text mode 
    Then all addressBook's get destroyed when your MainWindow gets destroyed. Or create your dialog (if it is one) on the stack.

  3. The following user says thank you to Lykurg for this useful post:

    Wojtek1990 (22nd July 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Application windows lags while loop doing.

    Ok, thanks for reply. I've changed in mainwindow.cpp to:

    Qt Code:
    1. void MainWindow::on_actionTasker_activated()
    2. {
    3. AddressBook *tasker = new AddressBook(this);
    4. //AddressBook *tasker = new AddressBook();
    5. this->setAttribute(Qt::WA_DeleteOnClose);
    6. tasker->show();
    7. }
    To copy to clipboard, switch view to plain text mode 

    ... tasker window was closed but main window of app still lags. How to change it (I don't want to close main window) ?
    Last edited by Wojtek1990; 22nd July 2010 at 23:10.

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Application windows lags while loop doing.

    Quote Originally Posted by Wojtek1990 View Post
    ... tasker window was closed but main window of app still lags.
    How did you meassure that?

  6. The following user says thank you to Lykurg for this useful post:

    Wojtek1990 (22nd July 2010)

  7. #5
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Application windows lags while loop doing.

    Quote Originally Posted by Lykurg View Post
    How did you meassure that?
    Sorry, my mistake ... tasker window was closed by signal/slot.
    Could anyone write code which I should put to my program to destroy this f****** lag ?

    edit:
    Sorry I think that I've understood you wrong.
    What to do with this code?

    Quote Originally Posted by Lykurg View Post
    Qt Code:
    1. AddressBook *tasker = new AddressBook(this);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Wojtek1990; 22nd July 2010 at 22:58.

  8. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Application windows lags while loop doing.

    With passing this as parent, you ensure that the allocated memory will be freed. To really "close" you window use window attribute Qt::WA_DeleteOnClose.

  9. The following user says thank you to Lykurg for this useful post:

    Wojtek1990 (23rd July 2010)

  10. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Application windows lags while loop doing.

    I think "tasker window was closed but main window of app still lags" means that the tasker window no longer responds to input. This line:
    Qt Code:
    1. while (checkBox()==1) tasker();
    To copy to clipboard, switch view to plain text mode 
    calls tasker() in a loop that never returns to the Qt event loop. The checkbox cannot change and emit clicked() and the window is generally unresponsive unless the event loop is entered.

    You could look at using QTimer to run tasker() regularly and then use the checkbox to start/stop the timer.

  11. The following user says thank you to ChrisW67 for this useful post:

    Wojtek1990 (23rd July 2010)

  12. #8
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Application windows lags while loop doing.

    Quote Originally Posted by ChrisW67 View Post
    You could look at using QTimer to run tasker() regularly and then use the checkbox to start/stop the timer.
    Okey, but when I've changed it to ...
    Qt Code:
    1. if (checkBox()==1) tasker();
    To copy to clipboard, switch view to plain text mode 
    ... app laggs too, but just just once (tasker is called 1 time)- I would like to definitely destroy this lag. And thanks for advice how to change this loop - I'll read about QTimer - but as I said problem of lag seems not be here.
    Last edited by Wojtek1990; 23rd July 2010 at 12:30.

  13. #9
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Application windows lags while loop doing.

    Okey, problem solved by using QThread class.
    Thanks for every reply. I've learned a lot from you.

Similar Threads

  1. Replies: 3
    Last Post: 12th July 2010, 06:25
  2. Application won't quit until event loop quits
    By caelestis in forum Qt Programming
    Replies: 6
    Last Post: 11th February 2010, 07:21
  3. Qt plug-in for GLib event loop based application
    By profoX in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2008, 14:27
  4. GLWidget lags behind
    By waveydavey in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2006, 16:18
  5. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55

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.