Results 1 to 5 of 5

Thread: An example 'Spin Box Delegate' code does not work using a tableView

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default An example 'Spin Box Delegate' code does not work using a tableView

    This is the original code :

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QStandardItemModel model(4, 2);
    5. QTableView tableView;
    6. tableView.setModel(&model);
    7. tableView.horizontalHeader()->setStretchLastSection(true);
    8. for (int row = 0; row < 4; ++row) {
    9. for (int column = 0; column < 2; ++column) {
    10. QModelIndex index = model.index(row, column, QModelIndex());
    11. model.setData(index, QVariant((row+1) * (column+1)));
    12. }
    13. }
    14.  
    15. tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
    16. tableView.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    And this is my code (I use a TableView out on a window using Designer)

    Qt Code:
    1. QStandardItemModel modelo(4, 2);
    2. ui->tableView->setModel(&modelo);
    3. ui->tableView->horizontalHeader()->setStretchLastSection(true);
    4. for (int row = 0; row < 4; ++row) {
    5. for (int column = 0; column < 2; ++column) {
    6. QModelIndex index = modelo.index(row, column, QModelIndex());
    7. modelo.setData(index, QVariant((row+1) * (column+1)));
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 


    (I only delete the 'delegate' code, in order to simply test how it works)
    Ok, in my case I see the table but i dont see the cells neither its content.
    What happens ?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: An example 'Spin Box Delegate' code does not work using a tableView

    You defined your model on the stack and it gets deleted at the end of the function (which I guess is your constructor)

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: An example 'Spin Box Delegate' code does not work using a tableView

    Thank you for your answer.
    But... I dont understand your explanation...
    The only difference between the original code and mine is that I use a tableView accesdesd 'via' the ui->.
    And, how can I avoid the destruction ?
    Thanks for your patience.

  4. #4
    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: An example 'Spin Box Delegate' code does not work using a tableView

    Quote Originally Posted by tonnot View Post
    The only difference between the original code and mine is that I use a tableView accesdesd 'via' the ui->.
    And that is exactly the difference why their code is working and yours not! And it is basic C++, so take any c++ book and read about dynamic and static allocation and how long objects lives.

  5. #5
    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: An example 'Spin Box Delegate' code does not work using a tableView

    Quote Originally Posted by tonnot View Post
    Thank you for your answer.
    But... I dont understand your explanation...
    Items allocated on the stack, like modelo, only exist to the end of the enclosing block (usually a method/function). You need to look at the differences between where you have modelo declared and where the original example has its model. What is the lifetime of the two objects?

Similar Threads

  1. Replies: 5
    Last Post: 21st September 2010, 19:16
  2. Replies: 2
    Last Post: 4th September 2010, 06:18
  3. Replies: 3
    Last Post: 21st January 2010, 23:02
  4. delegate isn't work with QSqlTableModel?
    By TomASS in forum Newbie
    Replies: 2
    Last Post: 3rd August 2009, 12:23
  5. TableView Delegate questions
    By No-Nonsense in forum Qt Programming
    Replies: 3
    Last Post: 11th December 2006, 09: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.