Solved Overloaded function type error
-
@SGaist
This part displays the DB data in a table view where the user can review and correct data. Some of the fields are corrected as text fields, some of them as drop downs and I also have an image field. That's why I have the mydelegate and image delegate classes to handle the different editors. My struggle is to pass the image file name to a function where the corrected image can be saved to the DB replacing the old, incorrect image.
The problem is that the original delegate functions are const functions and as I most likely incorrectly handle them I keep getting different error messages.
Thank you for your help. -
Then there's no need for a "do-it-all" delegate.
Assign your delegate only to the column where it make sense. That will simplify your life.
-
@SGaist
Which is the better: to have a separate delegate for each column where needed or have one delegate for each type of input method ? (For example one delegate handles all dropdowns, one all check idea etc.)
Thank you. -
You only need as many delegate as you have special cases.
Note that I didn't say "as many as the column count". If you have several columns of a special type then juste instantiate one delegate per column.
-
@SGaist
How can I use the model directly to save the newly picked image? Is there a sample code you know about? -
Well, you have a pointer to your model in setModelData, so why not use it ?
-
@SGaist
I started to use the pointer in setModelData. Addedoid ImageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); if(imgPick->selectedFile().isEmpty()) { model->setData(index, QVariant(), Qt::UserRole); model->setData(index, QVariant(), Qt::DecorationRole); } else { model->setData(index, imgPick->selectedFile(), Qt::UserRole); model->setData(index, QIcon(imgPick->selectedFile()), Qt::DecorationRole); //Adding image to db qDebug() << "Selected file: " << imgPick->selectedFile(); qDebug() << "Selected file name: " << imgPick->m_selectedFile; //new model->setData (index, imgPick->selectedFile (), Qt::DecorationRole); }
Causes no error, but doesn't save the img to the db ether. What am I supposed to use to commit the changes to the db?
-
What kind of model are you using ?
-
@SGaist
I use QStandardItemModel.#include "fixdb.h" #include "ui_fixdb.h" #include <QDebug> FixDb::FixDb(QWidget* parent) : QDialog(parent), ui(new Ui::FixDb) { ui->setupUi(this); correctFriend (); } FixDb::~FixDb() { delete ui; } void FixDb::correctFriend() { QSqlQuery query_fix ("SELECT * FROM Items ORDER BY name asc"); if(query_fix.isActive () == true) { qDebug() << "The query_fix is active!"; } else { qDebug() << "The query_fix is NOT active!" << query_fix.lastError (); } QStandardItemModel* fixModel = new QStandardItemModel(this); ui->tableView_Fix->setModel (fixModel); ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this)); for(int row1 = 0; query_fix.next (); row1++) { if(row1 == 0) { const QSqlRecord qRec = query_fix.record(); qDebug() << "The number of records (MainWindow): " << qRec; fixModel->insertColumns (0, qRec.count()); qDebug() << "fixdb record count: " << qRec.count (); } fixModel->insertRow (row1); fixModel->setData (fixModel->index (row1, 0), query_fix.value (0)); fixModel->setData (fixModel->index (row1, 1), query_fix.value (1)); fixModel->setData (fixModel->index (row1, 2), query_fix.value (11)); fixPixmap.loadFromData (query_fix.value (2).toByteArray ()); fixPixmap = fixPixmap.scaled (100, 100, Qt::KeepAspectRatio); fixModel->setData (fixModel->index (row1, 3), fixPixmap, Qt::DecorationRole); fixModel->setData (fixModel->index (row1, 4), query_fix.value (11)); fixModel->setData (fixModel->index (row1, 5), query_fix.value (10)); fixModel->setData (fixModel->index (row1, 6), query_fix.value (3)); fixModel->setData (fixModel->index(row1, 7), query_fix.value (4)); fixModel->setData (fixModel->index (row1, 8), query_fix.value (12)); fixModel->setData (fixModel->index (row1, 9), query_fix.value (7)); fixModel->setData (fixModel->index (row1, 10), query_fix.value (8)); fixModel->setData (fixModel->index (row1, 11), query_fix.value (9)); ui->tableView_Fix->setRowHeight (row1, 100); } ui->tableView_Fix->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold; text-decoration: underline;}"); ui->tableView_Fix->verticalHeader ()->setVisible (false); ui->tableView_Fix->setAlternatingRowColors (true); ui->tableView_Fix->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial; "); fixModel->setHeaderData (0, Qt::Horizontal, QObject::tr ("ID")); fixModel->setHeaderData (1, Qt::Horizontal, QObject::tr ("Name")); fixModel->setHeaderData (2, Qt::Horizontal, QObject::tr ("What")); fixModel->setHeaderData (3, Qt::Horizontal, QObject::tr ("Image")); fixModel->setHeaderData (4, Qt::Horizontal, QObject::tr ("Material")); fixModel->setHeaderData (5, Qt::Horizontal, QObject::tr ("Color")); fixModel->setHeaderData (6, Qt::Horizontal, QObject::tr ("Description")); fixModel->setHeaderData (7, Qt::Horizontal, QObject::tr ("Adoption Date")); fixModel->setHeaderData (8, Qt::Horizontal, QObject::tr ("Signed by")); fixModel->setHeaderData (9, Qt::Horizontal, QObject::tr ("History")); fixModel->setHeaderData (10, Qt::Horizontal, QObject::tr ("Age")); fixModel->setHeaderData (11, Qt::Horizontal, QObject::tr ("Notes")); ui->tableView_Fix->setColumnWidth (0, 60); ui->tableView_Fix->setColumnWidth (1, 120); ui->tableView_Fix->setColumnWidth (2, 150); ui->tableView_Fix->setColumnWidth (3, 100); ui->tableView_Fix->setColumnWidth (4, 130); ui->tableView_Fix->setColumnWidth (5, 120); ui->tableView_Fix->setColumnWidth (6, 400); ui->tableView_Fix->setColumnWidth (7, 150); ui->tableView_Fix->setColumnWidth (8, 200); ui->tableView_Fix->setColumnWidth (9, 400); ui->tableView_Fix->setColumnWidth (10, 130); ui->tableView_Fix->setColumnWidth (11, 300); ui->tableView_Fix->setWordWrap (true); }
-
Why not use a QSqlTableModel ?
-
@SGaist
I had difficulties with the elegates. Can I still use different delegates for some columns? If so I will redo that section. -
Yes, you can.
-
@SGaist
Thank you.
I'm working on it.