PDA

View Full Version : Application windows lags while loop doing.



Wojtek1990
22nd July 2010, 20:53
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 ...



void MainWindow::on_actionTasker_activated()
{

AddressBook *tasker = new AddressBook();
tasker->show();
}


... it look's like on this picture:
http://img375.imageshack.us/img375/7088/74884425.png

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

http://img101.imageshack.us/img101/1373/47199874.png


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



C:\Users\Wojtek\Desktop\bot_gui\bot-build-desktop\release\bot.exe closed with code -805306369


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



#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H

#include <QWidget>
#include <QMap>
#include <QMessageBox>
#include <QString>

namespace Ui {
class AddressBook;
}

class AddressBook : public QWidget
{
Q_OBJECT

public:
explicit AddressBook(QWidget *parent = 0);
~AddressBook();

private:
Ui::AddressBook *ui;

public slots:

void submitContact();
int checkBox();
//void podzial_liter ();
int wielka_litera(char);
void wiadomosc(char *wsk, int dl);
void tasker();
//void wiadomosc();

};

#endif // ADDRESSBOOK_H


addressbook.cpp



#include "addressbook.h"
#include "ui_addressbook.h"
#include "mainwindow.h"
#include "mainwindow.cpp"
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string.h>
#include <QtGui>
#include <QString>
#include <QStringList>


static const int EXPERIENCE = 0x49A00C;
static const int LEVEL = 0x49A008;
static const int MANA = 0x499FF8;
static const int HP = 0x49A014;

using namespace std;

int oldName;
QString oldAddress;

AddressBook::AddressBook(QWidget *parent) :
QWidget(parent),
ui(new Ui::AddressBook)
{
ui->setupUi(this);
Qt::WindowFlags flags;
flags = Qt::Window | Qt::WindowMinimizeButtonHint;
setWindowFlags( flags );
connect(ui->OK, SIGNAL(clicked()), this,SLOT(submitContact()));
connect(ui->checkBox, SIGNAL(clicked()), this,SLOT(checkBox()));

}

AddressBook::~AddressBook()
{
delete ui;
}

int AddressBook::wielka_litera (char znak) {
if ((int(znak) >= 33 && int(znak) <=43) || (int(znak) >= 58 && int(znak) <= 90)) return 1;
else return 0;
}

void AddressBook::wiadomosc (char *wsk, int dl)
{

char znak;

for(int i = 0; i < dl; i++)
{
znak=wsk[i];

Sleep(100);

if (wielka_litera(znak)) keybd_event(VK_SHIFT, 0, 0, 0);


keybd_event(VkKeyScan(znak), 0, 0, 0);
keybd_event(VkKeyScan(znak), 0, KEYEVENTF_KEYUP, 0);

if (wielka_litera(znak)) keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

}
keybd_event(VK_RETURN, 0, 0, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
}

void AddressBook::tasker () {

std::string message = oldAddress.toStdString();
//string message=std::string((const char*)oldAddress);
//string message = oldAdress;

for (int i=0;i<=oldName;i=i+6) {
Sleep(60);
}

int dlugosc = message.length();
char *message_char = new char[dlugosc+1];
strcpy(message_char, message.c_str());

wiadomosc (message_char,dlugosc);
}


int AddressBook::checkBox() {
Qt::CheckState state;

state = (ui->checkBox)->checkState();
if ( state == Qt::Checked ) return 1;
else return 0;
}

void AddressBook::submitContact()
{
oldName = ui->time->value();
oldAddress = ui->message->text();
while (checkBox()==1) tasker();
}


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

Lykurg
22nd July 2010, 21:09
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:
AddressBook *tasker = new AddressBook(this); Then all addressBook's get destroyed when your MainWindow gets destroyed. Or create your dialog (if it is one) on the stack.

Wojtek1990
22nd July 2010, 22:16
Ok, thanks for reply. I've changed in mainwindow.cpp to:



void MainWindow::on_actionTasker_activated()
{
AddressBook *tasker = new AddressBook(this);
//AddressBook *tasker = new AddressBook();
this->setAttribute(Qt::WA_DeleteOnClose);
tasker->show();
}


... tasker window was closed but main window of app still lags. How to change it (I don't want to close main window) ?

Lykurg
22nd July 2010, 22:22
... tasker window was closed but main window of app still lags.
How did you meassure that?

Wojtek1990
22nd July 2010, 22:33
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?



AddressBook *tasker = new AddressBook(this);

Lykurg
23rd July 2010, 07:12
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.

ChrisW67
23rd July 2010, 07:45
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:


while (checkBox()==1) tasker();

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.

Wojtek1990
23rd July 2010, 13:22
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 ...


if (checkBox()==1) tasker();

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

Wojtek1990
23rd July 2010, 21:41
Okey, problem solved by using QThread class.
Thanks for every reply. I've learned a lot from you.