Results 1 to 8 of 8

Thread: QTableView not working as expected.

  1. #1
    Join Date
    Jul 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView not working as expected.

    Hi guys, im rather new to Qt and im following a book : Foudations of Qt Development. Im trying to use the QTableView to display data, but it doesnt seem to work, it only displays a empty viewer.

    This is my constructor:
    Qt Code:
    1. MainDialog::MainDialog(QWidget *parent)
    2. : QDialog(parent), ui(new Ui::MainDialog)
    3. {
    4. // ui->setupUi(this);
    5. QTableView *table = new QTableView;
    6.  
    7. QStandardItemModel model(5, 2);
    8. for (int row = 0; row < 5; ++row){
    9. for (int col = 0; col < 2; ++col){
    10. QStandardItem *item = new QStandardItem(QString("Row:%1, Column:%2").arg(row).arg(col));
    11.  
    12. if (col == 0){
    13. for (int i = 0; i < 3; ++i){
    14. item->appendRow(new QStandardItem( QString("Item %1").arg(i)));
    15. }
    16. }
    17.  
    18. model.setItem(row, col, item);
    19. }
    20. }
    21.  
    22. table->setModel(&model);
    23. table->show();
    24.  
    25. QHBoxLayout *layout = new QHBoxLayout;
    26. layout->addWidget(table);
    27. setLayout(layout);
    28. }
    To copy to clipboard, switch view to plain text mode 

    Please be forgiving with your words as im really new thanks

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView not working as expected.

    you creates QStandardItemModel model(5, 2); in a stack (an object will be destroyed when out of scope). you should create in the heap, i.e. QStandardItemModel *model = new QStandardItemModel(5, 2);.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jul 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView not working as expected.

    ahh ty, cause the book used it in the main function. Thanks for your expertise

  4. #4
    Join Date
    Jul 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView not working as expected.

    After successfully implementing the QTableView, how do i make it stretch fully? As there are gaps on the right and bottom.

  5. #5
    Join Date
    Jul 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView not working as expected.

    im not sure if im allowed to do this, bumps.

  6. #6
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView not working as expected.

    Try:
    Qt Code:
    1. table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jul 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView not working as expected.

    Thanks for the help but it doesnt seem to be working.

    edit: it seems that it isnt the QTableView that is not expanding, rather it is the model that it is displaying that doesnt expand.
    Last edited by junxuan; 29th July 2009 at 13:48.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView not working as expected.

    You don't need line 23 in your original post.

    Maybe:
    Qt Code:
    1. table->horizontalHeader()->setStretchLastSection(true);
    2. OR
    3. table->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
    To copy to clipboard, switch view to plain text mode 
    are more to your liking?

Similar Threads

  1. Replies: 2
    Last Post: 7th June 2009, 10:47
  2. Replies: 3
    Last Post: 26th April 2009, 17:54
  3. QTableView doubleclicked event not working
    By sgmurphy19 in forum Qt Programming
    Replies: 6
    Last Post: 15th March 2009, 10:00
  4. QTableView ::setColumnWidth not working?
    By killerwookie99 in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2008, 17:14
  5. Need help integrating sigc++ with a KDE3/Qt3 app
    By Valheru in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2007, 15:39

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.