I am designing a major user interface with several fields and functions. However in order to shrink the problem I created a small application with 4 columns: name, image, dataDatabase, dateTime. I have a MainWindow with a QTableView, as soon as I right click inside the QTableView an AddItemDialog opens up with: 1) nameLineEdit, 2) ImLineEdit, 3) imageLineEdit 4) dateTimeEdit
The issue that I have is that I can't find a way to accept the 4) dateTimeEdit through the AddItemDialog. I didn't "go to slot" of the dateTimeEdit as I didn't think that I had to. I think that I am not doing the proper conversion for the date and time but please advise on what the issue might be.
I am including the most important parts of the application below with the related description of the procedure I followed:
I created an Item with the fields item.h:
class Item
{
public:
Item(const double dateTime,
const QString &name = "", const QString &image = "",
const QByteArray &imagesData = QByteArray());
QString name() const { return mName; }
QString image() const { return mImage; }
QByteArray imagesData() const { return mImagesData; }
double dateTime() const { return mDateTime; }
private:
QString mName;
QString mImage;
QByteArray mImagesData;
double mDateTime;
};
and its related item.cpp
Item::Item(const double dateTime,
const QString &name, const QString &image,
const QByteArray &imagesData)
{
mName = name;
mImage = image;
mImagesData = imagesData;
mDateTime = dateTime;
}
I created a database.h table that will contain the parameters as follows:
class dataBase : public QObject
{
Q_OBJECT
public:
explicit dataBase(QObject *parent = nullptr);
bool inizializationDataBase(const QString &nameDataBase);
bool configureDataBase();
QString getError() const { return mError; }
bool addItem(const Item &item);
private:
QSqlDatabase mDatabase;
QString mError;
};
And its related database.cpp file - I am only including the most important piece of the code for this file:
#define CREATE_TABLE \
" CREATE TABLE IF NOT EXISTS Fish_Table" \
" (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
", name TEXT NOT NULL" \
", image TEXT NOT NULL" \
", dataDataBase BLOB NOT NULL" \
", dateTime DOUBLE NOT NULL)"
dataBase::dataBase(QObject *parent)
: QObject(parent)
{
}
bool dataBase::inizializationDataBase(const QString &nameDataBase) {
// code
}
bool dataBase::addItem(const Item &item) {
QSqlQuery q;
q.prepare("INSERT INTO Fish_Table (name, image, dataDatabase, dateTime) VALUES (?,?,?,?)");
q.addBindValue(item.name());
q.addBindValue(item.image());
q.addBindValue(item.imagesData());
q.addBindValue(item.dateTime());
bool ok = q.exec();
if (!ok) {
mError = q.lastError().text();
}
return ok;
}
and finally the AddItemDialog.h and AddItemDialog.cpp that contains the fields I am trying to pass to the QTableView of the MainWindow. AddItemDialog.h
namespace Ui
{
class AddItemDialog;
}
class AddItemDialog : public QDialog
{
Q_OBJECT
public:
explicit AddItemDialog(QWidget *parent = nullptr);
~AddItemDialog();
Item item() const { return mItem; }
private slots:
void on_toolButton_clicked();
void on_buttonBox_accepted();
void on_buttonBox_rejected();
private:
Ui::AddItemDialog *ui;
Item mItem;
};
AddItemDialog.cpp
AddItemDialog::AddItemDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::AddItemDialog)
{
ui->setupUi(this);
auto fileSystemModel = new QFileSystemModel(this);
fileSystemModel->setRootPath(QDir::rootPath());
ui->imageLineEdit->setCompleter(new QCompleter(fileSystemModel,this));
QDateTime dateTime;
dateTime.setDate(QDate::currentDate());
}
AddItemDialog::~AddItemDialog()
{
delete ui;
}
void AddItemDialog::on_toolButton_clicked()
{
auto nameDataBase = QFileDialog::getOpenFileName(this, "Open Images", QDir::rootPath(),
"Images (*.png *.jpg *jpeg *.tif *.tiff);;Any type (*.*)");
ui->imageLineEdit->setText(nameDataBase);
}
void AddItemDialog::on_buttonBox_accepted()
{
QFile dataBase(ui->imageLineEdit->text());
if (!dataBase.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, "Error", dataBase.errorString());
return;
}
mItem = Item(ui->nameLineEdit->text(),
ui->ImLineEdit->text(),dataBase.readAll());
dataBase.close();
accept();
}
So to recap: 1) I am not sure I provided the right format to the QDateTime conversion on the Item.h / Item.cpp and database.h / database.cpp 2) I am not sure how to pass the QDateTime from the AddItemDialog.cpp to the MainWindow
Additionally on the AddItemDialog.cpp I am having the following error right on the beginning: Constructor for AddItemDialog must explicitly initialize the member mItem which does not have a default constructor
Thanks for providing information on this issue that I have been having for a couple of days.
Aucun commentaire:
Enregistrer un commentaire