Domanda

Ho un QTimer collegato a una funzione TimerHandler.Il TimerHandler dovrebbe fare la mia funzione SendKeys che ho dichiarato nella sezione pubblica della mia intestazione.Se digito manualmente il testo per la funzione SendKeys fornisce un'uscita corretta.Ma se passo il testo da un LPSTR predefinito emette la spazzatura.Ecco il mio codice:

myproject.h

#ifndef MYPROJECT_H
#define MYPROJECT_H

#include <QtGui/QMainWindow>
#include "ui_myproject.h"
#include <qtimer.h>
#include <qmessagebox.h>
#include <Windows.h>

class MyProject : public QMainWindow
{
    Q_OBJECT

public:
    MyClass(QWidget *parent = 0, Qt::WFlags flags = 0);
    Ui::MyProjectClass ui;
    QTimer* SpamTimer;

    void SendText(char* message, int size)
    {
        int lc=0;
        do{
        keybd_event(VkKeyScan(message[lc]),0,KEYEVENTF_EXTENDEDKEY,0);
        keybd_event(VkKeyScan(message[lc]),0,KEYEVENTF_KEYUP,0);
        lc=lc+1;
        }while(lc<size);
        keybd_event(VK_RETURN,0,KEYEVENTF_EXTENDEDKEY,0);
        keybd_event(VK_RETURN,0,KEYEVENTF_KEYUP,0);
    }

public slots:
    void StartBTNClick();
    void StopBTNClick();
    void TimerHandler();
};
#endif // MYPROJECT_H
.

myproject.cpp

#include "MyProject.h"

LPSTR txtMessage; // Message for SendKeys function.
int buffer;
bool TimerEnabled = 0;

MyClass::MainWindow(QWidget *parent, Qt::WFlags flags) // Intializing MainWindow
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    statusBar()->showMessage("Status: Idle.");
    connect(ui.StartBTN, SIGNAL(clicked()), this, SLOT(StartBTNClick()));
    connect(ui.StopBTN, SIGNAL(clicked()), this, SLOT(StopBTNClick()));
}

void MyClass::StartBTNClick() // Starts the timer.
{
    int delay; // delay for QTimer
    bool ok;
    std::string convertme;

    QString TextMSG = ui.TextBox->text(); // Get text from 'line edit' for txtMessage.
    QString TimeMSG = ui.TimeBox->text(); // Get text from 2nd 'line edit' for delay.
    convertme = TextMSG.toStdString();
    txtMessage = const_cast<char*> (convertme.c_str()); // converted QString to LPSTR.
    buffer = strlen(txtMessage);
    delay = TimeMSG.toInt(&ok, 10); // converted QString to int.
    if (delay > 0)
    {
        QtTimer = new QTimer(this);
        connect(QtTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
        TimerEnabled = 1;
        QtTimer->start(delay);
        statusBar()->showMessage("Status: Running.");
    }
    else if (delay < 0)
    {
        QMessageBox::warning(this, "Warning!", "Delay can't be \"0\" or lower than \"0\"!");
    }
    else
    {
        QMessageBox::warning(this, "Warning!", "Delay was not specified properly.");
    }
}

void MyClass::StopBTNClick() // Stops the timer.
{
    if (TimerEnabled == 1)
    {
        QtTimer->stop();
        disconnect(QtTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
        TimerEnabled = 0;
        statusBar()->showMessage("Status: Idle.");
    }
}

void MyClass::TimerHandler() // Timer handles the SendKeys function
{
    SendText(txtMessage, buffer);
}
.

Ciò rende il mio timer in uscita spazzatura invece il testo all'interno di txtMessage. Se uso

SendText("test message", strlen("test message"));
.

Invece, emette il messaggio correttamente.C'è qualcosa di sbagliato nel mio codice?

Ho provato a dichiarare LPSTR txtMessage all'interno della mia classe nella sezione pubblica a MyProject.h ma anche questo non ha funzionato.

È stato utile?

Soluzione

Realizza txtMessage Un oggetto string (std::string o QString, dal momento che stai usando QT), non un puntatore.Ottieni il puntatore da quell'oggetto proprio prima di effettuare la chiamata a SendText o, ancora più semplice, basta creare SendText prendere un oggetto stringa invece di un puntatore.

void SendText(const QString& str)
{
  const char* message = str.c_str();
  // whatever else you want to do
}
.

Il problema è che stai memorizzando un puntatore su dati in un oggetto temporaneo (convertme).Questo oggetto esce dallo scopo e viene distrutto e la memoria viene riscritta.Il motivo per cui funziona con `" Messaggio TEST "è che Le linee di stringa sono archiviate in modo diverso .Devi mantenere il messaggio che stai cercando di memorizzare in memoria tutto il tempo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top