PDA

View Full Version : QLineEdit update before external method



Ishtar
16th August 2011, 22:41
Greetings All

I have something like the following in my project.




MainRoutine doStuff;

QLineEdit test = new QLineEdit();
test.setText("Updating...");

doStuff.runSTLcppRoutine();


The call to QLineEdit::setText() does not get updated until doStuff.runSTLCppRoutine() returns despite being physically before it. Can somebody help with this?

Many Thanks.

NullPointer
16th August 2011, 23:02
Hi,

You didn't give the time to loop event call the paint() event from QLineEdit...
You can use the QCoreApplication::processEvents() after setting the text.

Hth.

Ishtar
17th August 2011, 17:40
Thank you very much for your help

Unfortunately
QCoreApplication::processEvents() has a bizarre effect.

Here is a snipet of my code.




void Ui_MainGUI::startMainRoutine() {

string errTxt;
double testValue;

wxParameters wxStn;

if(w->toolBox_WX->currentIndex() == 0){

wxStn.stnName = "Lacrosse WS2300";
wxStn.stnInterface = "SERIAL";
wxStn.stnProtocol = "SERIAL";

wxStn.stnPort = w->cmbo_2300Port->currentText().toLocal8Bit().data();
wxStn.stnDataBit = w->cmbo_2300DataBits->currentText().toLocal8Bit().data();
wxStn.stnParity = w->cmbo_2300Parity->currentText().toLocal8Bit().data();
wxStn.stnStopBit = w->cmbo_2300StopBits->currentText().toLocal8Bit().data();

wxStn.stnBaud = w->cmbo_2300Speed->currentText().toInt();
wxStn.stnPollFreq = w->spin_2300PollFreq->text().toInt();
wxStn.stnRetries = w->spin_2300Retries->text().toInt();
wxStn.stnTimeout = w->spin_2300Timeout->text().toInt();

}

w->ledt_2300Status->setText("Connecting...");
QCoreApplication::processEvents();

if(!(MainRoutine.connStn(wxStn, stnNumber, &errTxt))){
w->ledt_2300Status->setText("ERROR");
QString err = QString::fromStdString(errTxt);
QMessageBox::critical(this, tr("ERROR"), tr("%1").arg(err));
return;
}

w->ledt_2300Status->setText("Testing Connection.");
QCoreApplication::processEvents();

if(!(MainRoutine.testStn(stnNumber, &testValue, &errTxt))){
w->ledt_2300Status->setText("ERROR");
QString err = QString::fromStdString(errTxt);
QMessageBox::critical(this, tr("ERROR"), tr("%1").arg(err));
return;
}

w->ledt_2300Status->setText("ACTIVE");

return;
}



1] wxParameters wxStn is a struct which looks like this:



struct wxParameters {

const char *stnName;
const char *stnInterface;
const char *stnProtocol;
const char *stnPort;
const char *stnDataBit;
const char *stnParity;
const char *stnStopBit;
int stnBaud;
int stnPollFreq;
int stnTimeout;
int stnRetries;

};


2] The call at line 29 fails because wxStn.stnPort is full of garbage.

If I take the processEvents() away then the code works perfectly alas without the QLineEdit being updated until the call returns.

Do you have any idea why my const char *stnPort is full of garbage?

Many thanks
Nikki

stampede
17th August 2011, 19:44
Do you have any idea why my const char *stnPort is full of garbage?
Yeah, because you are pointing to a block of memory that is no longer valid:

char * ptr = currentText().toLocal8Bit().data()
this is something like:


char * ptr;
{
QByteArray str = "something";
ptr = str.data();
}
// is it ok to use ptr here ? not really

You should allocate your strings with malloc() and use sprintf or memcpy to initialize it.
Or even better, use QString instead (if you can).

Ishtar
19th August 2011, 19:52
Sorry, I was being silly.

In the end I used std::string and then converted to const char later on because QString's are not appropriate for my application.

Many thanks.
Nikki