QTableView not working as expected.
Hi guys, im rather new to Qt and im following a book : Foudations of Qt Development. Im trying to use the QTableView to display data, but it doesnt seem to work, it only displays a empty viewer.
This is my constructor:
Code:
MainDialog
::MainDialog(QWidget *parent
) : QDialog(parent
), ui
(new Ui
::MainDialog) {
// ui->setupUi(this);
for (int row = 0; row < 5; ++row){
for (int col = 0; col < 2; ++col){
if (col == 0){
for (int i = 0; i < 3; ++i){
}
}
model.setItem(row, col, item);
}
}
table->setModel(&model);
table->show();
layout->addWidget(table);
setLayout(layout);
}
Please be forgiving with your words as im really new thanks:p
Re: QTableView not working as expected.
you creates QStandardItemModel model(5, 2); in a stack (an object will be destroyed when out of scope). you should create in the heap, i.e. QStandardItemModel *model = new QStandardItemModel(5, 2);.
Re: QTableView not working as expected.
ahh ty, cause the book used it in the main function. Thanks for your expertise:)
Re: QTableView not working as expected.
After successfully implementing the QTableView, how do i make it stretch fully? As there are gaps on the right and bottom.
Re: QTableView not working as expected.
im not sure if im allowed to do this, bumps.
Re: QTableView not working as expected.
Re: QTableView not working as expected.
Thanks for the help but it doesnt seem to be working. :(
edit: it seems that it isnt the QTableView that is not expanding, rather it is the model that it is displaying that doesnt expand.
Re: QTableView not working as expected.
You don't need line 23 in your original post.
Maybe:
Code:
table->horizontalHeader()->setStretchLastSection(true);
OR
table
->horizontalHeader
()->setResizeMode
(0,
QHeaderView::Stretch);
are more to your liking?