Results 1 to 5 of 5

Thread: QLineEdit update before external method

  1. #1
    Join Date
    Apr 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QLineEdit update before external method

    Greetings All

    I have something like the following in my project.

    Qt Code:
    1. MainRoutine doStuff;
    2.  
    3. QLineEdit test = new QLineEdit();
    4. test.setText("Updating...");
    5.  
    6. doStuff.runSTLcppRoutine();
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jul 2011
    Location
    Brasil
    Posts
    39
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit update before external method

    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.

  3. #3
    Join Date
    Apr 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit update before external method

    Thank you very much for your help

    Unfortunately
    Qt Code:
    1. QCoreApplication::processEvents()
    To copy to clipboard, switch view to plain text mode 
    has a bizarre effect.

    Here is a snipet of my code.

    Qt Code:
    1. void Ui_MainGUI::startMainRoutine() {
    2.  
    3. string errTxt;
    4. double testValue;
    5.  
    6. wxParameters wxStn;
    7.  
    8. if(w->toolBox_WX->currentIndex() == 0){
    9.  
    10. wxStn.stnName = "Lacrosse WS2300";
    11. wxStn.stnInterface = "SERIAL";
    12. wxStn.stnProtocol = "SERIAL";
    13.  
    14. wxStn.stnPort = w->cmbo_2300Port->currentText().toLocal8Bit().data();
    15. wxStn.stnDataBit = w->cmbo_2300DataBits->currentText().toLocal8Bit().data();
    16. wxStn.stnParity = w->cmbo_2300Parity->currentText().toLocal8Bit().data();
    17. wxStn.stnStopBit = w->cmbo_2300StopBits->currentText().toLocal8Bit().data();
    18.  
    19. wxStn.stnBaud = w->cmbo_2300Speed->currentText().toInt();
    20. wxStn.stnPollFreq = w->spin_2300PollFreq->text().toInt();
    21. wxStn.stnRetries = w->spin_2300Retries->text().toInt();
    22. wxStn.stnTimeout = w->spin_2300Timeout->text().toInt();
    23.  
    24. }
    25.  
    26. w->ledt_2300Status->setText("Connecting...");
    27. QCoreApplication::processEvents();
    28.  
    29. if(!(MainRoutine.connStn(wxStn, stnNumber, &errTxt))){
    30. w->ledt_2300Status->setText("ERROR");
    31. QString err = QString::fromStdString(errTxt);
    32. QMessageBox::critical(this, tr("ERROR"), tr("%1").arg(err));
    33. return;
    34. }
    35.  
    36. w->ledt_2300Status->setText("Testing Connection.");
    37. QCoreApplication::processEvents();
    38.  
    39. if(!(MainRoutine.testStn(stnNumber, &testValue, &errTxt))){
    40. w->ledt_2300Status->setText("ERROR");
    41. QString err = QString::fromStdString(errTxt);
    42. QMessageBox::critical(this, tr("ERROR"), tr("%1").arg(err));
    43. return;
    44. }
    45.  
    46. w->ledt_2300Status->setText("ACTIVE");
    47.  
    48. return;
    49. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. struct wxParameters {
    2.  
    3. const char *stnName;
    4. const char *stnInterface;
    5. const char *stnProtocol;
    6. const char *stnPort;
    7. const char *stnDataBit;
    8. const char *stnParity;
    9. const char *stnStopBit;
    10. int stnBaud;
    11. int stnPollFreq;
    12. int stnTimeout;
    13. int stnRetries;
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by Ishtar; 17th August 2011 at 17:46.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLineEdit update before external method

    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:
    Qt Code:
    1. char * ptr = currentText().toLocal8Bit().data()
    To copy to clipboard, switch view to plain text mode 
    this is something like:
    Qt Code:
    1. char * ptr;
    2. {
    3. QByteArray str = "something";
    4. ptr = str.data();
    5. }
    6. // is it ok to use ptr here ? not really
    To copy to clipboard, switch view to plain text mode 
    You should allocate your strings with malloc() and use sprintf or memcpy to initialize it.
    Or even better, use QString instead (if you can).

  5. #5
    Join Date
    Apr 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit update before external method

    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

Similar Threads

  1. QLineEdit Delegate value update
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2011, 23:02
  2. Replies: 2
    Last Post: 1st October 2010, 15:57
  3. Replies: 2
    Last Post: 21st August 2010, 10:26
  4. How to use input method with QLineEdit in HP-UX (Qt4.5)
    By justalittle in forum Qt Programming
    Replies: 0
    Last Post: 11th March 2010, 08:22
  5. QLineEdit isModified method
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2009, 14:06

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.