Results 1 to 7 of 7

Thread: Problem with show Model-View-Deleaget inside MainWindow class

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with show Model-View-Deleaget inside MainWindow class

    Hello,

    I'm trying code a Model-View-Deleage and I've one question:

    When I'm writting this code:
    Qt Code:
    1. QStandardItemModel model( 5, 2 );
    2. for( int r=0; r<5; r++ )
    3. for( int c=0; c<2; c++)
    4. {
    5. QStandardItem *item = new QStandardItem( QString("Row:%1, Column:%2").arg(r).arg(c) );
    6. if( c == 0 )
    7. for( int i=0; i<3; i++ )
    8. item->appendRow( new QStandardItem( QString("Item %1").arg(i) ) );
    9. model.setItem(r, c, item);
    10. }
    11.  
    12. QTableView *table = new QTableView;
    13. table->setModel(&model);
    14. table->show();
    To copy to clipboard, switch view to plain text mode 

    into a:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    To copy to clipboard, switch view to plain text mode 
    then when application is running there are two windows. First whith:
    Qt Code:
    1. MainWindow w;
    2. w.show();
    To copy to clipboard, switch view to plain text mode 
    and the second with:
    Qt Code:
    1. QTableView *table = new QTableView;
    2. table->setModel(&model);
    3. table->show();
    To copy to clipboard, switch view to plain text mode 
    and everything is ok, but, when I write a
    Qt Code:
    1. QStandardItemModel model( 5, 2 );
    2. for( int r=0; r<5; r++ )
    3. for( int c=0; c<2; c++)
    4. {
    5. QStandardItem *item = new QStandardItem( QString("Row:%1, Column:%2").arg(r).arg(c) );
    6. if( c == 0 )
    7. for( int i=0; i<3; i++ )
    8. item->appendRow( new QStandardItem( QString("Item %1").arg(i) ) );
    9. model.setItem(r, c, item);
    10. }
    11.  
    12. QTableView *table = new QTableView;
    13. table->setModel(&model);
    14. table->show();
    To copy to clipboard, switch view to plain text mode 
    into:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5. //here!
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    there are a empty window whith table->show() ? why?
    when I'm trying put this code into button click:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QStandardItemModel model( 5, 2 );
    4. for( int r=0; r<5; r++ )
    5. for( int c=0; c<2; c++)
    6. {
    7. QStandardItem *item = new QStandardItem( QString("Row:%1, Column:%2").arg(r).arg(c) );
    8. if( c == 0 )
    9. for( int i=0; i<3; i++ )
    10. item->appendRow( new QStandardItem( QString("Item %1").arg(i) ) );
    11. model.setItem(r, c, item);
    12. }
    13.  
    14. QTableView *table = new QTableView;
    15. table->setModel(&model);
    16. table->show();
    17. }
    To copy to clipboard, switch view to plain text mode 
    I get a empty window too :/

    Thanks!
    Last edited by TomASS; 20th July 2009 at 16:12.

  2. #2
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question from begginer

    You're not creating the table on anything.You need to give your table an owner.
    Qt Code:
    1. QTableView *table = new QTableView(yourWindow);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to vieraci for this useful post:

    TomASS (20th July 2009)

  4. #3
    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: Question from begginer

    You are creating the model on stack so it gets deleted when you exit the constructor. Make it a member variable of the class as well or create it on heap.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    TomASS (20th July 2009)

  6. #4
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question from begginer

    Thanks,

    1. What should I write istead of "yourWindow" ?
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    To copy to clipboard, switch view to plain text mode 
    w?
    this?
    parent?

    2. How I can add widget into MainWindow?

    I'm trying w->addWidget, but it is no method addWidget :/

  7. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question from begginer

    2. How I can add widget into MainWindow?

    I'm trying w->addWidget, but it is no method addWidget :/
    setup all the widgets , layout it into a single widget "baaseWidget"
    use mainWindow::setCentralWidget( baseWidget );
    setCentralWidget()
    "Behind every great fortune lies a crime" - Balzac

  8. #6
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with show Model-View-Deleaget inside MainWindow class

    @vieraci:
    You're not creating the table on anything.You need to give your table an owner.
    thanks!
    @wysota
    You are creating the model on stack so it gets deleted when you exit the constructor. Make it a member variable of the class as well or create it on heap.
    thanks! I've added model in member of the class, but how create it on a heap?
    @wagmare:
    setup all the widgets , layout it into a single widget "baaseWidget"
    use mainWindow::setCentralWidget( baseWidget );
    setCentralWidget()
    How can I do this?

    I'm trying:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. QVBoxLayout *layout = new QVBoxLayout( this );
    5. QDialogButtonBox *box = new QDialogButtonBox( Qt::Horizontal );
    6.  
    7.  
    8. layout->addWidget( new QLabel( "Try out the buttons!" ) );
    9. layout->addWidget( box );
    10. }
    To copy to clipboard, switch view to plain text mode 
    but I get a empty window :/
    Last edited by TomASS; 20th July 2009 at 16:38.

  9. #7
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with show Model-View-Deleaget inside MainWindow class

    for delete

Similar Threads

  1. Plugin implementation question
    By JPNaude in forum Qt Programming
    Replies: 12
    Last Post: 27th August 2008, 20:24
  2. Exceptions / setjmp/longjmp question
    By Aceman2000 in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2008, 17:14
  3. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49
  4. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 15:34

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.