Hello! Created a QTableView table on the form as follows:

QTableView *table = new QTableView(this); table->setEditTriggers(QAbstractItemView::AllEditTriggers); 

It seems to set the triggers to edit the table, but it still does not work. How to make so that the cells can be edited?

    1 answer 1

    Widgets containing the class name Q...View are designed to work with externally connected models. That is, for QTableView , QTreeView and QListView will need to first create a model:

     QStandardItemModel *model = new QStandardItemModel(4,4,this); for(int row = 0; row < 4; ++row) { for(int col = 0; col < 4; ++col) { QStandardItem *item = new QStandardItem(QString("row %0, col %1") .arg(row).arg(col)); model->setItem(row, col, item); } } QTableView *table = new QTableView(this); table->setModel(model); QTreeView *tree = new QTreeView(this); tree->setModel(model); QListView *list = new QListView(this); list->setModel(model); 

    By the way, in this example, one model will be available to three widgets at once, and in all of them it will be possible to edit it.

    For cases when the use of the external model is redundant, you need to create widgets containing Q...Widget in the class names. This saves time, because the model in these classes ( QTableWidget , QTreeWidget and QListWidget ) is already built.

     QTableWidget *table = new QTableWidget(4,4,this); table->show(); QTreeWidget *tree = new QTreeWidget(this); tree->setColumnCount(1); tree->show(); { QList<QTreeWidgetItem *> items; for(int i = 0; i < 10; ++i) { QStringList list; list.append(QString("item: %1").arg(i)); items.append(new QTreeWidgetItem(list)); } tree->insertTopLevelItems(0, items); } QListWidget *list = new QListWidget(this); new QListWidgetItem("1", list); new QListWidgetItem("2", list); new QListWidgetItem("3", list); list->show(); 
    • Forgot to mention that I use QSqlQueryModel. The problem is that I can’t edit the cell in any way, i.e. clicking on it doesn’t happen, even by double clicking (in the table itself) - Malice
    • one
      QSqlQueryModel does not allow editing data. Use QSqlTableModel . - alexis031182
    • How to build a job, if the data for the table I pull out the SQL query? It will take quite a long time to translate it into a model, since there are a lot of records (> 100000 records) - Malice
    • You can solve differently, starting from using views at the database level and ending with exporting data from a QSqlQueryModel to a different model (not necessarily SQL) that supports editing. If there are so many entries, then in no way you can drive them into the widget for editing. At a minimum, you should maintain limiting, or more simply pagination. But this is all a separate big topic and for the current question is offtopic. - alexis031182
    • Thanks for the advice, I understood the principle of limiting, but I don’t even know how to make a user-friendly interface. Could a little towards the submissions send? - Malice