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:
Code:
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
if( c == 0 )
for( int i=0; i<3; i++ )
model.setItem(r, c, item);
}
table->setModel(&model);
table->show();
into a:
Code:
int main(int argc, char *argv[])
{
then when application is running there are two windows. First whith:
Code:
MainWindow w;
w.show();
and the second with:
Code:
table->setModel(&model);
table->show();
and everything is ok, but, when I write a
Code:
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
if( c == 0 )
for( int i=0; i<3; i++ )
model.setItem(r, c, item);
}
table->setModel(&model);
table->show();
into:
Code:
MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
//here!
}
there are a empty window whith table->show() ? why?
when I'm trying put this code into button click:
Code:
void MainWindow::on_pushButton_clicked()
{
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
if( c == 0 )
for( int i=0; i<3; i++ )
model.setItem(r, c, item);
}
table->setModel(&model);
table->show();
}
I get a empty window too :/
Thanks!
Re: Question from begginer
You're not creating the table on anything.You need to give your table an owner.
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.
Re: Question from begginer
Thanks,
1. What should I write istead of "yourWindow" ?
Code:
MainWindow
::MainWindow(QWidget *parent
){
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 :/
Re: Question from begginer
Quote:
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()
Re: Problem with show Model-View-Deleaget inside MainWindow class
@vieraci:
Quote:
You're not creating the table on anything.You need to give your table an owner.
thanks!
@wysota
Quote:
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:
Quote:
setup all the widgets , layout it into a single widget "baaseWidget"
use mainWindow::setCentralWidget( baseWidget );
setCentralWidget()
How can I do this?
I'm trying:
Code:
MainWindow
::MainWindow(QWidget *parent
){
layout
->addWidget
( new QLabel( "Try out the buttons!" ) );
layout->addWidget( box );
}
but I get a empty window :/
Re: Problem with show Model-View-Deleaget inside MainWindow class