Results 1 to 8 of 8

Thread: Qt DataGridView & Objects

  1. #1
    Join Date
    Jun 2013
    Posts
    22
    Qt products
    Qt5
    Thanks
    1

    Default Qt DataGridView & Objects

    Hello!

    I want to create Qt program with similar layout as C# DataGridView. I'm choosing between QTableWidget and QTableView - which one should be more suitable? In C# it's practice to create class for DGV and list where are multiple objects from that class stored. How to achieve the same in Qt?

    Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Qt DataGridView & Objects

    A QTableWidget is a QTableView that stores the data in itself.

    So it mostly depends on how you plan on accessing the data and how much data you have.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2013
    Posts
    22
    Qt products
    Qt5
    Thanks
    1

    Default Re: Qt DataGridView & Objects

    I want to store music playlist into table, the file has average 1000 lines of plain text. It is possible to create objects like in C# and push it into some list?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Qt DataGridView & Objects

    You can do that with both widgets so my suggestion would be to try QTableWidget first, it is generally easier to get used to.

    Cheers,
    _

  5. #5
    Join Date
    Jun 2013
    Posts
    22
    Qt products
    Qt5
    Thanks
    1

    Default Re: Qt DataGridView & Objects

    Ok. In C# I need some class in which I store parts of data and that data is almost independent of application. When data is imported from file there's just a call from datagridview to source which is for example list with class objects. How to do the same with Qt? Can you please provide some example code? I'm searching for example of that for almost three hours and I can't find anything since I'm not familiar with Qt. Thank you!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Qt DataGridView & Objects

    There are two approaches:

    1) QTableWidget: something that loads the data create QTableWidgetItem instances and adds them to the widget

    2) QTableView: a class derived from QAbstractTableModel provides access to the data by providing a row count, a column count and data for a given row/column index when being asked for it.

    Which one of the two do you want to go with?

    Cheers,
    _

  7. #7
    Join Date
    Jun 2013
    Posts
    22
    Qt products
    Qt5
    Thanks
    1

    Default Re: Qt DataGridView & Objects

    I started working with QTableWidget and it seems pretty similar to C# DGV. The main problem on which I encountered was defining of rows & columns. I can't define them before I put data into list or something like that, because I can't guess how many lines is in file. If there is no defined rows & columns at the start, QTableWidgetItem doesn't appear in QTableWidget when adding it. Another thing is that I would like to put parts of data into class's properties because I need to save/export it into file. What do I have to do now?

    It seems that there's a big difference between Qt and Visual C#... I didn't expect that Thank you for help!

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Qt DataGridView & Objects

    Quote Originally Posted by janck View Post
    If there is no defined rows & columns at the start, QTableWidgetItem doesn't appear in QTableWidget when adding it.
    See insertRow()

    Quote Originally Posted by janck View Post
    Another thing is that I would like to put parts of data into class's properties because I need to save/export it into file. What do I have to do now?
    Two options:
    1) QTableWidgetItem::setData()
    2) Derive you own class fom QTableWidgetItem, use whatever variables you need for your data

    Cheers,
    _

Similar Threads

  1. 3d objects in QT
    By waman.prabhu in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2011, 10:11
  2. Replies: 3
    Last Post: 9th January 2010, 15:47
  3. Replies: 7
    Last Post: 18th July 2006, 21:33
  4. How to Use Objects
    By raphaelf in forum Newbie
    Replies: 1
    Last Post: 2nd June 2006, 09:59
  5. Using K Objects in Qt project
    By dec0ding in forum Qt Programming
    Replies: 8
    Last Post: 17th January 2006, 18:44

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
  •  
Qt is a trademark of The Qt Company.