I am creating a model using this code:
QStandardItemModel table_model(4,4);
for(int row=0; row<4; row++){
for (int column=0; column<4; column++){
QStandardItem* item = new QStandardItem((QString("100")));
table_model.setItem(row,column,item);
}
}
Then, I am passing it to QTableView as follow:
QStandardItemModel* model = &table_model;
ui->table->setModel(model);
ui->table->show();
However something is wrong. The QTableView shows nothing, just white space all over it. yet, if, and only if, I click on the trigger button (which creates the model and links it to the QTableView) I can see the 4x4 table, and yet without any data. immediately after the "click" everything disappear (it is just the moment of clicking the button) ..
so what am I missing ? – thanks
You create your model on stack and pass a pointer which gets invalid right after you leave method where you declare it.
QStandardItemModel * table_model = new QStandardItemModel(4,4);
Related
I have a Qt project where I have a QStandardItemModel called stockModel. This model is linked to a QTableView called tvStock.
I have a button called btnDelete which has a clicked event set up as follows:
void StockItems::on_btnDelete_clicked()
{
//delete
}
How do I delete the selected row of tvStock from stockModel?
I assume I can start with this (but I'm not sure how to complete it):
stockModel->removeRow(/* What goes here? */);
Otherwise let me know if I'm completely on the wrong track.
Update:
I found and modified some code that allows to me to delete the row if the entire row's contents are selected:
QModelIndexList indexes = ui->tvStock->selectionModel()->selectedRows();
while (!indexes.isEmpty()) {
stockModel->removeRows(indexes.last().row(), 1);
indexes.removeLast();
}
Is there a way this code could be modified to delete the entire row if only one of the row items is selected?
Thanks!
I need help with customizing a QTableView, I have defined a QTableView as this example show, which I found on the internet:
model = new QStandardItemModel(2,3,this); //2 Rows and 3 Columns
model->setHorizontalHeaderItem(0, new QStandardItem(QString("ID")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Name")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Description")));
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableView->setModel(model);
How can I define a size for each column separately i.e using percentages:
I would get first column 10% of the width second 50%, third 40%.
When I run the program and double click on a row in the QTableView, I can change the value of the cell clicked on , although I have defined a QTableView onDoubleclick method , I mean its like when you click on rename a file it highlights the text so you can modify, how can I disable that?
How to make the columns resizable, meaning can be resized by dragging their columns edge.
First: Use setColumnWidth() method after setModel(). For example:
//...
ui->tableView->setModel(model);
double ii = ui->tableView->columnWidth(0);
ui->tableView->setColumnWidth(1,0.4*ii);
ui->tableView->setColumnWidth(2,0.5*ii);
Third: To do this remove
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
from your code.
I need a piece of code to check that the user has selected at least one row in a QTableWidget
The QTableWidget can be referenced using ui->tableWidget.
I'm trying to check there are selecting, if not, display a message box, if so, moving on to code I have written.
Thanks.
You can obtain the selected rows from the selection model like:
QItemSelectionModel *selectionModel = ui->tableWidget->selectionModel();
QModelIndexList *selectedRows = selectionModel->selectedRows();
if (selectedRows.size() > 0) {
// There is at lease one selected row.
}
I am New to Qt creator .
I want to create a table View with 1 row and 2 columns using QmodelIndex.
There was an error in assigning the model to the tableView and creating data .
QTableView* const tableView
= { htca_ui->tableView,
};
tableView->setModel(&mymodel);
How to create a model and assign to the view to create the row with 2 columns using QmodelIndex?
Please Help
If you have added a tableView with the UI designer you don't need to create it again in code. Assuming your QTableView is called tableView and that htca_ui is your UI pointer, you can do
htca_ui->tableView->setModel(&mymodel);
Edit : And it occurs to me the &mymodel means you're creating it on the stack, which may not be the best idea. Better to do :
MyModel *mymodelinstance = new MyModel(this);
htca_ui->tableView->setModel(mymodelinstance);
However: the model classes can be complex to work with. If you are just after a simple table with minimum code, I would suggest using a QTableWidget rather than a QTableView. Add a QTableWidget to your UI, then you can do
htca_ui->tableWidget->setRowCount(1);
htca_ui->tableWidget->setColumnCount(2);
QTableWidgetItem *item = new QTableWidgetItem("An Item");
htca_ui->tableWidget->setItem(0,0, item); // The item at the top left will read "An Item".
See the documentation for QTableWidget for more info
From the book I'm reading:
By default, QListWidget is read-only. If we wanted the user to edit
the items, we could set the view's edit triggers using
QAbstractItemView::setEditTriggers(); for example, a setting of
QAbstractItemView::AnyKeyPressed means that the user can begin editing
an item just by starting to type.
So, I call the function in my code:
ui->listWidget->setEditTriggers(QAbstractItemView::AnyKeyPressed);
But when I select an item and start typing, nothing happens.
It turns out that the items themselves also have an editable flag, so after adding them I had to iterate all of them and set it. Now it's working.
// set the editable flag for each item
for (int ii = 0; ii < ui->listWidget->count(); ii++) {
ui->listWidget->item(ii)->setFlags(ui->listWidget->item(ii)->flags() | Qt::ItemIsEditable);
}
// set the editable triggers for the list widget
ui->listWidget->setEditTriggers(QAbstractItemView::AnyKeyPressed);