Results 1 to 2 of 2

Thread: Code from main or from QMainWindow differences

  1. #1
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Code from main or from QMainWindow differences

    Hi all,

    I have the next code and works properly:

    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include <QMainWindow>
    4. #include <QVBoxLayout>
    5.  
    6. #include <QTreeView>
    7. #include <QListView>
    8. #include <QTableView>
    9.  
    10. #include <QStandardItemModel>
    11.  
    12. int main( int argc, char **argv )
    13. {
    14. QApplication app( argc, argv );
    15.  
    16. QMainWindow* mainWindow = new QMainWindow();
    17. QVBoxLayout* verticalLayout = new QVBoxLayout;
    18.  
    19. QTreeView *tree = new QTreeView();
    20. QListView *list = new QListView();
    21. QTableView *table = new QTableView();
    22.  
    23. verticalLayout->addWidget(tree);
    24. verticalLayout->addWidget(list);
    25. verticalLayout->addWidget(table);
    26.  
    27. QWidget* window = new QWidget();
    28. window->setLayout(verticalLayout);
    29. mainWindow->setCentralWidget(window);
    30.  
    31. QStandardItemModel model( 5, 2 );
    32. for( int r=0; r<5; r++ )
    33. for( int c=0; c<2; c++)
    34. {
    35. QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) );
    36.  
    37. if( c == 0 )
    38. for( int i=0; i<3; i++ )
    39. {
    40. QStandardItem *child = new QStandardItem( QString("Item %0").arg(i) );
    41. child->setEditable( false );
    42. item->appendRow( child );
    43. }
    44.  
    45. model.setItem(r, c, item);
    46. }
    47.  
    48. model.setHorizontalHeaderItem( 0, new QStandardItem( "Foo" ) );
    49. model.setHorizontalHeaderItem( 1, new QStandardItem( "Bar-Baz" ) );
    50.  
    51. tree->setModel( &model );
    52. list->setModel( &model );
    53. table->setModel( &model );
    54.  
    55. list->setSelectionModel( tree->selectionModel() );
    56.  
    57.  
    58. mainWindow->show();
    59.  
    60. return app.exec();
    61. }
    To copy to clipboard, switch view to plain text mode 

    The problem is when I put the same code inside a QMainWindow, a new window is opened but the widgets: tree, list and table, do not shown any information, they appear empty.

    What's wrong?

    Best regards.


    Added after 10 minutes:


    I have changed the line:

    Qt Code:
    1. QStandardItemModel model( 5, 2 );
    To copy to clipboard, switch view to plain text mode 

    by the line:

    Qt Code:
    1. QStandardItemModel *model = new QStandardItemModel( 5, 2, this );
    To copy to clipboard, switch view to plain text mode 

    And now works properly.
    Last edited by qt_developer; 20th February 2013 at 16:25.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Code from main or from QMainWindow differences

    Do you know why that is?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. pyQT how do I enable or disable a pushbutton from the main code?
    By Michael Druckenmiller Sr in forum Newbie
    Replies: 1
    Last Post: 12th January 2012, 18:44
  2. Lots of code in QMainWindow
    By GimpMaster in forum Qt Programming
    Replies: 11
    Last Post: 23rd October 2009, 07:59
  3. Qt\bin and Qt\qt\bin dll differences
    By HyperB in forum Installation and Deployment
    Replies: 2
    Last Post: 6th May 2009, 07:12
  4. Replies: 11
    Last Post: 11th August 2008, 09:14
  5. a question conerning code in main.cpp
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2006, 16:55

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.