Results 1 to 9 of 9

Thread: Live Update data in table

  1. #1
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Question Live Update data in table

    Hi
    I have a table with 1 row initially.
    Now when I insert a row in the table, I want to read some specif data into the cells. These values are retrieved using two different nested slots. Also at the same time I want to update the cells continuously as and when the slot retrieve data. I am using timer in a slot to continuosly read data from the source. But how can I update the table contents.


    Thanks in advance for any help.


    Abhijit

  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: Live Update data in table

    You can update the data e.g. via
    Qt Code:
    1. QTableWidget *tw = /*...*/
    2. tw->item(0,0)->setText("foo");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Post Re: Live Update data in table

    Quote Originally Posted by Lykurg View Post
    You can update the data e.g. via
    Qt Code:
    1. QTableWidget *tw = /*...*/
    2. tw->item(0,0)->setText("foo");
    To copy to clipboard, switch view to plain text mode 
    Hi LyKurg

    As I told you that I am using two SLOTS which are nested and in one of these SLOTS I am using a timer to retrieve data continuously. Let me explain my problem with the code:
    Qt Code:
    1. //First Slot
    2. void MainWindow::getData()
    3. {
    4. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    5. connect(manager, SIGNAL(finished(QNetworkReply*)),
    6. this, SLOT(readData(QNetworkReply*)));
    7. QNetworkRequest request;
    8. request.setUrl(QUrl("http://finance.yahoo.com/q?s=GOOG"));
    9. manager->get(request);
    10. }
    11.  
    12. //2nd Slot
    13. void MainWindow::readData(QNetworkReply *reply){
    14. QByteArray bytes = reply->readAll();
    15. connect(reply, SIGNAL(readyRead()), this, SLOT(repaint()));
    16. QString string = QString::fromUtf8(bytes);
    17. QString Temp1;
    18. Temp1 = string.section("<div class=\"hd\"><h2>", 2, 2);
    19. QTimer *timer = new QTimer(this);
    20. connect(timer, SIGNAL(timeout()), this, SLOT(getData()));
    21. timer->start(10000);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Now suppose instead of "GOOG" I want to read a string from a cell of the row that I have recently inserted and get Temp1 value to be displayed in the second column. So basically I want to insert a row on the run and insert a value in the first column of the row. After Inserting the value I want the program to run slot 1 and 2 with "GOOG" replaced by the value in column 1 and column 2 giving m the result which will be updated regularly as in Slot 2.

    If you have more query pls feel free to ask.
    Once again thanks for your help.

    Abhijit

  4. #4
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Live Update data in table

    If I understood your problem then a stupid solution is to keep a const QString of your initial url ("http://finance.yahoo.com/q?s="), then add a private QString variable that represents the retrieved data to be appended to the initial url.
    Create a function that it's purpose is to retrieve the data you want from the table, let it be the 1st thing to be called in the slot getData(). Create a temp QString that will hold the new url and then pass it to the request.

    Or the moment you insert the new value to the table create the string with the new url, store it as a private member (let's call it m_finalUrlStr) and then in getData(), request.setUrl(QUrl(m_finalUrlStr));
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  5. The following user says thank you to Archimedes for this useful post:

    abghosh (25th February 2010)

  6. #5
    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: Live Update data in table

    Hi, the first time I misunderstood you totally. For now, just a quick side note: You are creating a QNetworkAccessManager and QTimer on the heap every time the slots are called without deleting them which will end in a hight memory usage for your program, since they are only deleted after destroying Mainwindow.

  7. #6
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Default Re: Live Update data in table

    Quote Originally Posted by Archimedes View Post
    If I understood your problem then a stupid solution is to keep a const QString of your initial url ("http://finance.yahoo.com/q?s="), then add a private QString variable that represents the retrieved data to be appended to the initial url.
    Create a function that it's purpose is to retrieve the data you want from the table, let it be the 1st thing to be called in the slot getData(). Create a temp QString that will hold the new url and then pass it to the request.

    Or the moment you insert the new value to the table create the string with the new url, store it as a private member (let's call it m_finalUrlStr) and then in getData(), request.setUrl(QUrl(m_finalUrlStr));
    Hi Archimedes

    Thanks for your reply. Can you please demonstarate the same with an example?
    Actually I am not at all getting the idea about how it will work.

    Thanks

  8. #7
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Live Update data in table

    Well let's say you are making the new url when you are about to insert the data in the table (you know the value at that time and you don't need to insert it and then retrieve it again). The following code is not tested (compile or run) and just shows the basic idea nothing more.
    Qt Code:
    1. // header file
    2. MainWindow
    3. {
    4. ....
    5. // let's say you want to insert a QString in the table
    6. // cause I'm not sure what you're passing in it
    7. void insertInTable(QString &toInsert);
    8. ...
    9. public slots:
    10. void getData();
    11. void readData();
    12. ...
    13. private:
    14. QString m_finalUrlStr; // holds the new url
    15. QNetworkAccessManager *manager;
    16. QTimer *timer;
    17. ...
    18. }
    19.  
    20. // cpp file
    21. const QString initialUrlStr = "http://finance.yahoo.com/q?s=";
    22.  
    23. // Constructor
    24. MainWindow::MainWindow()
    25. {
    26. // initialize m_finalUrlStr to the 1st url you want to send a request
    27. m_finalUrlStr = "http://finance.yahoo.com/q?s=GOOG";
    28. // putting the mem allocation and the connections here, Lykurg described why
    29. manager = new QNetworkAccessManager(this);
    30. connect(manager, SIGNAL(finished(QNetworkReply*)),
    31. this, SLOT(readData(QNetworkReply*)));
    32. timer = new QTimer(this);
    33. connect(timer, SIGNAL(timeout()), this, SLOT(getData()));
    34. ...
    35. }
    36.  
    37. void MainWindow::insertInTable(QString &toInsert)
    38. {
    39. m_finalUrlStr.clear(); // clear the previous url
    40. m_finalUrlStr = initialUrlStr;
    41. m_finalUrlStr += toInsert; // you got the new url
    42. ...// code that does the insertion in the table
    43. }
    44.  
    45. //1st Slot
    46. void MainWindow::getData()
    47. {
    48. QNetworkRequest request;
    49. request.setUrl(QUrl(m_finalUrlStr ));
    50. manager->get(request);
    51. }
    52.  
    53. //2nd Slot
    54. void MainWindow::readData(QNetworkReply *reply){
    55. QByteArray bytes = reply->readAll();
    56. connect(reply, SIGNAL(readyRead()), this, SLOT(repaint()));
    57. QString string = QString::fromUtf8(bytes);
    58. QString Temp1 = string.section("<div class=\"hd\"><h2>", 2, 2);
    59. insertInTable(Temp1); // I assume that Temp1 holds the data you want to insert
    60. // in the table, I'm just guessing actually
    61. timer->start(10000);
    62. }
    To copy to clipboard, switch view to plain text mode 

    If the insertion in the table is done by the user and it's not a result of the reply (that I assumed here) then simply remove the call of insertInTable in the 2nd slot and do the string manipulations in the function (probably a slot) that does the insertion.
    Anyway, I hope the above sample will give you an idea of how to do this task
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  9. #8
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Default Re: Live Update data in table

    Thanks Archemedis,
    But suppose I have two rows in the table & I want to update the 2nd column of both the rows. How ill that work??

  10. #9
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Live Update data in table

    Quote Originally Posted by Lykurg View Post
    You can update the data e.g. via
    Qt Code:
    1. QTableWidget *tw = /*...*/
    2. tw->item(0,0)->setText("foo");
    To copy to clipboard, switch view to plain text mode 
    QTableWidgetItem * QTableWidget::item ( int row, int column ) is the function, so simply put the row and the column you want.

    Also Qt is the best documented API out there with a great tool (Qt Assistant). So use that to your advantage, you'll find a lot of answers there! Anyway, read the following starting with QTableWidget, QTableWidgetItem and for a complete explanation of the Model/View read this Model/View Programming.
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. SQL slow query - table update
    By lasher in forum Newbie
    Replies: 4
    Last Post: 21st October 2009, 23:12
  3. Replies: 2
    Last Post: 21st October 2009, 08:13
  4. Replies: 8
    Last Post: 25th February 2008, 17:00
  5. Not corect record pos in data table after update
    By zlatko in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2006, 16:51

Tags for this Thread

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.