PDA

View Full Version : modifying tableview outside its constructor



bossy
28th April 2012, 18:39
i know this is simple but somehow i cant figure it out.

if i have

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

....
QTableView *myTable = new QTableView();
myTable->setModel(model);
QVBoxLayout *myBlayout = new QVBoxLayout();
myBlayout->addWidget(myTable);
ui->centralWidget->setLayout(myBlayout);
removeRow();
}

void MainWindow::removeRow()
{
myTable->hideRow(10);

}

it compiles but crashes when removeRow is called, how do i access myTable outside the constructor ?
thanks

bossy
29th April 2012, 14:54
took me a while to figure out but let me answer my own question,
the Qtableview has to be declare like that: myTable = new QTableView(); and usual has to be in the header

not too sure why, maybe someone smart can elaborate...

norobro
29th April 2012, 20:05
The pointer QTableView *myTable in the constructor goes out of scope at the end of the block. In your removeRow function, you are using the *myTable class member that is declared in the header file but never initialized. thus causing a segfault. Search the web for "variable shadowing".