Results 1 to 2 of 2

Thread: Data not being added to my QTableView?

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Data not being added to my QTableView?

    Hi,

    I have the following code :

    Qt Code:
    1. CSendMessage::CSendMessage(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. this->setMinimumHeight(400);
    7. this->setMinimumWidth(600);
    8.  
    9. QStandardItemModel model(4, 4);
    10. ui.tableView->setModel(&model);
    11.  
    12. SliderDelegate delegate;
    13. ui.tableView->setItemDelegate( &delegate );
    14.  
    15. for (int row = 0; row < 4; ++row) {
    16. for (int column = 0; column < 4; ++column) {
    17. QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    18. model.setItem(row, column, item);
    19. }
    20. }
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    Is there any reason why the above code does not put items into the table view?

    The class that holds the table view is a standard QT ui one with a table view on the form and is added to a QMdiSubWindow as a widget.

    This is added as follows :

    Qt Code:
    1. m_pSendMessage = new CSendMessage( this );
    2. m_pSendMessageWindow = new QMdiSubWindow;
    3. m_pSendMessageWindow->setWidget( m_pSendMessage );
    4. m_pSendMessageWindow->setAttribute( Qt::WA_DeleteOnClose );
    5.  
    6. QMdiSubWindow* subWindow = m_pworkspace->addSubWindow( m_pSendMessageWindow );
    7. subWindow->setWindowIcon(QIcon(QString::fromUtf8(":/QTCanMonitor/Resources/Envelope.png")));
    8. m_pSendMessageWindow->show();
    To copy to clipboard, switch view to plain text mode 

    I'm confused?

    Regards,
    Steve

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Data not being added to my QTableView?

    You are creating the model on stack. It gets destroyed when you leave the function. Create it on heap (using operator new) instead.

Similar Threads

  1. QTableView : accessing the data
    By marvaneke in forum Newbie
    Replies: 10
    Last Post: 30th March 2012, 11:31
  2. Replies: 4
    Last Post: 19th October 2007, 19:47
  3. QTableView contextMenuEvent
    By derrickbj in forum Qt Programming
    Replies: 1
    Last Post: 1st March 2007, 16:37
  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.