Results 1 to 4 of 4

Thread: Obtaining data from QTableWidget

  1. #1
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Obtaining data from QTableWidget

    Hey there i have created a table using QTableWidget and i was wondering if there is a way to obtain the data from the table when the user enters data into it so that i can perform a calculation on it and display it back to the user. Do i have to create a new function/method to perfrom the calculation in?? Ive tried looking at the class for QTableWidget and i cant really find enough that will help me with this task. any help would be again very appreciated thanks. Jag

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Obtaining data from QTableWidget

    Use QTableWidget::item(x,y) method to access a particular item and QTableWidgetItem::data(int role) to get the data stored in it (You need to access the DisplayRole) as QVariant. You can then use QVariant::toInt() (or other) to convert it to a type you need to perform your calculations. Of course for calculations themselves you need some place in your code (some method -- a slot maybe?).

  3. #3
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Obtaining data from QTableWidget

    hey there i have one problem with my table now. when i want to get user data from it i have to enter the cell, enter the number, then leave the cell and then enter it again before the original value gets stored by my array?!! why does my table do this? heres my code to get the data from the table:

    Qt Code:
    1. table[demands->currentRow()][demands->currentColumn()] = demands->item(demands->currentRow(), demands->currentColumn())->data(Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 

    sorry about the lateness of the reply to the post but i forgot to ask before...

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Obtaining data from QTableWidget

    The problem sounds like that you havent' figured out the correct event when you should perform the calculation.

    Here's at least 2 working ways:

    1) Inherit QTableWidget and override protected slot:
    virtual void dataChanged( const QModelIndex & topLeft, const QModelIndex & bottomRight )
    By this way you don't need to create any extra connections manually, since thanks to the underlying model/view architecture and a virtual slot you'll get the connection automatically. But you will have to inherit QTableWidget.

    2) Create a custom slot of type:
    Qt Code:
    1. void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
    To copy to clipboard, switch view to plain text mode 
    ..and connect the table's model's dataChanged() signal signal to your custom slot:
    Qt Code:
    1. connect(table->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&)))
    To copy to clipboard, switch view to plain text mode 
    By this way you don't need to inherit QTableWidget. But you will have to create the connection manually.

    Then, in both ways, you'll put your calculation code into the dataChanged() slot.
    Basically the indexes topLeft and bottomRight are always the same, if you don't change the data programmatically. Meaning that the user is normally able to edit one cell at time..
    I don't know if this is different depending on the selection mode and behaviour.

    Anyway, the code in your slot could look something like this:
    Qt Code:
    1. table[topleft.row()][topLeft.column()] = topLeft.data().toInt();
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 15th March 2006 at 07:45.

Similar Threads

  1. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 22:46
  2. Replies: 4
    Last Post: 19th October 2007, 19:47
  3. reading and writing data from a QTableWidget
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2007, 20:51
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.