Solved Overloaded function type error
-
Hi,
I have the following code:
in imagedelegate.h:const QByteArray imageArray; void addNewImage(QString imageFile) const;
in imagedelegate.cpp:
void ImageDelegate::addNewImage(QString imageFile) const { qDebug() << "Entered addNewImage"; QFile newImage(imageFile); if(newImage.open (QIODevice::ReadOnly)) { imageArray = newImage.readAll; } QSqlQuery querys(db); if(!db.open ()) { qDebug() << "The database is not open (submit)!"; } else { qDebug() << "The database is open (submit)!"; } //Get Id from somewhere querys.prepare("INSERT INTO Items (ID, Pic) VALUES(:ID, :Pic)" ); querys.bindValue (":ID", sIDReview); }
The line
imageArray = newImage.readAll;
causes the following error message:
C:\Programming\Projects\Folkfriends_bzr\trunk\imagedelegate.cpp:44: error: no match for 'operator=' (operand types are 'const QByteArray' and '<unresolved overloaded function type>')
imageArray = newImage.readAll;
^
What did I do wrong?
Thank you for your help. -
@gabor53 said in Overloaded function type error:
if(newImage.open (QIODevice::ReadOnly)) {
imageArray = newImage.readAll;
}Hi,
readAll is defined as a function
QByteArray QIODevice::readAll()
you're trying to access it, as if it were a member variable of QFile, which it is not
imageArray = newImage.readAll();
-
@J.Hilk
I did what you recommended and it worked.
I got a new error message saying
C:\Programming\Projects\Folkfriends_bzr\trunk\imagedelegate.cpp:44: error: passing 'const QByteArray' as 'this' argument discards qualifiers [-fpermissive]
imageArray = newImage.readAll();
^
I believe everything is a const so it supposed to work, but it doesn't. Any idea what is still wrong?
Thank you. -
@gabor53 remove
const
inconst QByteArray imageArray;
line in your header file... -
@artwaw
In the .h file I haveQByteArray imageArray; QString imageFile; void addNewImage(QString imageFile) const;
and in the cpp file I have
if(newImage.open (QIODevice::ReadOnly)) { imageArray = newImage.readAll(); } QSqlQuery querys(db); if(!db.open ()) { qDebug() << "The database is not open (submit)!"; } else { qDebug() << "The database is open (submit)!"; } //Get Id from somewhere querys.prepare("INSERT INTO Items (ID, Pic) VALUES(:ID, :Pic)" ); querys.bindValue (":ID", sIDReview); }
It still gives me the error message
C:\Programming\Projects\Folkfriends_bzr\trunk\imagedelegate.cpp:44: error: passing 'const QByteArray' as 'this' argument discards qualifiers [-fpermissive]
imageArray = newImage.readAll();
^Thank you for your help.
-
@gabor53
Why have youvoid addNewImage(QString imageFile) const;
? Please try to remove const at the end. -
Artur,
Now it gives a new error message:
C:\Programming\Projects\Folkfriends_bzr\trunk\imagedelegate.cpp:30: error: passing 'const ImageDelegate' as 'this' argument discards qualifiers [-fpermissive]
addNewImage (imageFile);
^My imagedelegate.h:
void addNewImage(QString imageFile);
the call in imagedelegate.cpp:
void 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); } addNewImage (imageFile); }
(this causes the error)
and the function
oid ImageDelegate::addNewImage(QString imageFile) { qDebug() << "Entered addNewImage"; QFile newImage(imageFile); if(newImage.open (QIODevice::ReadOnly)) { imageArray = newImage.readAll(); } QSqlQuery querys(db); if(!db.open ()) { qDebug() << "The database is not open (submit)!"; } else { qDebug() << "The database is open (submit)!"; } //Get Id from somewhere querys.prepare("INSERT INTO Items (ID, Pic) VALUES(:ID, :Pic)" ); querys.bindValue (":ID", sIDReview); }
Thank you for your help.
-
@gabor53 Why is ImageDelegate::setModelData const?!
Only methods which do not change the STATE of the object are allowed to be const.
And you cannot call non const methods in const methods.
So, remove the const from ImageDelegate::setModelData... -
@jsulm In this case, setModelData should stay const, it's the original method signature. addNewImage should be made const (and its parameter a const reference). And also, why not use the model directly to store the new data ?
-
@SGaist
Is there an example how to do it? I have 2 data types: image and text. If there is a way to let the model store the data and update the db instead of my delegate that would save a lot of coding.
Thank you. -
Can you give a more complete description of what you are trying to achieve ?
-
@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); }