PDA

View Full Version : Problem with show Model-View-Deleaget inside MainWindow class



TomASS
20th July 2009, 15:36
Hello,

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

When I'm writting this code:

QStandardItemModel model( 5, 2 );
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
QStandardItem *item = new QStandardItem( QString("Row:%1, Column:%2").arg(r).arg(c) );
if( c == 0 )
for( int i=0; i<3; i++ )
item->appendRow( new QStandardItem( QString("Item %1").arg(i) ) );
model.setItem(r, c, item);
}

QTableView *table = new QTableView;
table->setModel(&model);
table->show();

into a:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
then when application is running there are two windows. First whith:

MainWindow w;
w.show();
and the second with:

QTableView *table = new QTableView;
table->setModel(&model);
table->show();
and everything is ok, but, when I write a
QStandardItemModel model( 5, 2 );
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
QStandardItem *item = new QStandardItem( QString("Row:%1, Column:%2").arg(r).arg(c) );
if( c == 0 )
for( int i=0; i<3; i++ )
item->appendRow( new QStandardItem( QString("Item %1").arg(i) ) );
model.setItem(r, c, item);
}

QTableView *table = new QTableView;
table->setModel(&model);
table->show();
into:

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
//here!

}
there are a empty window whith table->show() ? why?
when I'm trying put this code into button click:

void MainWindow::on_pushButton_clicked()
{
QStandardItemModel model( 5, 2 );
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
QStandardItem *item = new QStandardItem( QString("Row:%1, Column:%2").arg(r).arg(c) );
if( c == 0 )
for( int i=0; i<3; i++ )
item->appendRow( new QStandardItem( QString("Item %1").arg(i) ) );
model.setItem(r, c, item);
}

QTableView *table = new QTableView;
table->setModel(&model);
table->show();
}
I get a empty window too :/

Thanks!

vieraci
20th July 2009, 16:40
You're not creating the table on anything.You need to give your table an owner.

QTableView *table = new QTableView(yourWindow);

wysota
20th July 2009, 17:00
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.

TomASS
20th July 2009, 17:03
Thanks,

1. What should I write istead of "yourWindow" ?

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
w?
this?
parent?

2. How I can add widget into MainWindow?

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

wagmare
20th July 2009, 17:10
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()

TomASS
20th July 2009, 17:30
@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:

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
QVBoxLayout *layout = new QVBoxLayout( this );
QDialogButtonBox *box = new QDialogButtonBox( Qt::Horizontal );


layout->addWidget( new QLabel( "Try out the buttons!" ) );
layout->addWidget( box );
}
but I get a empty window :/

TomASS
20th July 2009, 17:35
for delete