Hello,
I have got two problems and I don't know how to repair to run correctly.
First problem:
Firstly I give some code.
#include <QtGui>
#include <QtSql>
#include "listresourcesdialog.h"
#include "ui_listresourcesdialog.h"
{
model->setQuery("SELECT * FROM resources");
model
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("ID"));
model
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Name"));
}
listResourcesDialog
::listResourcesDialog(QWidget *parent
) : QWidget(parent
), ui
(new Ui
::listResourcesDialog){
ui->setupUi(this);
initializeModel(&model);
ui->tableView->setModel(&model);
}
listResourcesDialog::~listResourcesDialog()
{
delete ui;
}
void listResourcesDialog
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
#include <QtGui>
#include <QtSql>
#include "listresourcesdialog.h"
#include "ui_listresourcesdialog.h"
void listResourcesDialog::initializeModel(QSqlQueryModel *model)
{
model->setQuery("SELECT * FROM resources");
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
}
listResourcesDialog::listResourcesDialog(QWidget *parent) : QWidget(parent), ui(new Ui::listResourcesDialog)
{
ui->setupUi(this);
QSqlQueryModel model;
initializeModel(&model);
ui->tableView->setModel(&model);
}
listResourcesDialog::~listResourcesDialog()
{
delete ui;
}
void listResourcesDialog::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
To copy to clipboard, switch view to plain text mode
All is ok but when I called constructor of this class I saw table but there is nothing there. But if I give in:
listResourcesDialog
::listResourcesDialog(QWidget *parent
) : QWidget(parent
), ui
(new Ui
::listResourcesDialog){
ui->setupUi(this);
initializeModel(&model);
ui->tableView->setModel(&model);
QMessageBox::information(this, tr
("blabla"), tr
("blalbla"));
}
listResourcesDialog::listResourcesDialog(QWidget *parent) : QWidget(parent), ui(new Ui::listResourcesDialog)
{
ui->setupUi(this);
QSqlQueryModel model;
initializeModel(&model);
ui->tableView->setModel(&model);
QMessageBox::information(this, tr("blabla"), tr("blalbla"));
}
To copy to clipboard, switch view to plain text mode
So I saw table with data from database, but if I accept QMessageBox (I click "OK") then data from table disappear. I don't know what is wrong and what can I do to run correctly.
Second problem:
query.prepare("SELECT * FROM profiles WHERE name = :name");
query.bindValue(":name", ui->lineEdit_2->text());
QSqlQuery query;
query.prepare("SELECT * FROM profiles WHERE name = :name");
query.bindValue(":name", ui->lineEdit_2->text());
To copy to clipboard, switch view to plain text mode
All is ok, when this query has been executed I have in object results of this query, but I don't know how can I check how records are in objects (like numRows()) I have to know how many records are in this object.
Has somebody any solutions?
Bookmarks