Reputation: 37
I've been trying to implement a project in C++ using Qt Creator – this is Qt Creator 3.3.1 (opensource) based on Qt Creator 5.4.1. I use Ubuntu 14.04.
I've found some tutorials of which the subject was simillar to what I wanted to create, so I've been studying the code and trying to fit it to my needs. This is the GUI Project. I've been also dealing with this project to learn more C++, OOP. I did 2 forms.
The project consists of 3 classes for now. One class includes a form to gather some information about persons – it works. The main class includes QtableWidget to present the details about the persons from the database in the table, I also implemented the method to find persons (also in the main class), searching by Surnames – in form I used QlineEdit to do it.
But I need another form to edit the information about the strict person. I decided to implement the form to edit the information in another class. There occurred the problem, because to edit the information about the strict person, I need to be able to read what I typed into the gap of QlineEdit and then to make a search in the database using this information (from the QlineEdit which is included in the form of the main class).
The issue is that, in the second class (to edit) when I use a construction in the constructor as: QString Surname = ui->name_of_the_gap->text();
- where ”name_of_the_gap” is the name of the gap which includes the surname that I want to use but it happens to be unavailable for this UI (the ui in the class in which there is this form to edit information).
I have tried to use inheritance but non of it works. Could I please ask you to direct me / point me out how should I do it / what should I change?
Below I will present you the pieces of the code:
addrecord.h
#ifndef ADDRECORD_H
#define ADDRECORD_H
#include <QDialog>
#include <QtSql/qsqldatabase.h>
#include <QtSql/QSqlError>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/qsqldriver.h>
#include <QtSql/QSqlDriverPlugin>
#include <QtSql/qsqldriverplugin.h>
#include <QSqlQuery>
#include <QDebug>
#include <QString>
#include <QMessageBox>
namespace Ui {
class AddRecord;
}
class AddRecord : public QDialog
{
Q_OBJECT
public:
explicit AddRecord(QWidget *parent = 0);
~AddRecord();
private slots:
void on_btnQuit_clicked();
void on_btnAdd_clicked();
private:
Ui::AddRecord *ui;
};
**/*addrecord.h*/**
editrecord.h
#ifndef EDITRECORD_H
#define EDITRECORD_H
#include <QDialog>
//#include "mainwindow.h"
//#include "addrecord.h"
#include <QLineEdit>
namespace Ui {
class EditRecord;
}
class EditRecord : public QDialog
//class EditRecord : public MainWindow
{
Q_OBJECT
public:
explicit EditRecord(QWidget *parent = 0);
~EditRecord();
Ui::EditRecord *eui;
private slots:
void on_btnQuit_clicked();
private:
//Ui::EditRecord *ui;
//Ui::EditRecord *ui;
//Ui::MainWindow *mui;
QLineEdit *searchSurnameEdit;
};
#endif // EDITRECORD_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "addrecord.h"
#include "editrecord.h"
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QSql>
#include <QtSql/qsqldatabase.h>
#include <QtSql/QSqlError>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/qsqldriver.h>
#include <QtSql/QSqlDriverPlugin>
#include <QtSql/qsqldriverplugin.h>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlRecord>
#include <QSqlTableModel>
#include <QModelIndexList>
#include <QTableView>
#include "editrecord.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow *ui;
void fillTable();
private slots:
void on_tableWidget_cellChanged(int row, int column);
void on_btnQuit_clicked();
void on_btnAdd_clicked();
void on_btnSearchSurname_clicked();
void on_btnEditData_clicked();
private:
bool loading;
//Ui::MainWindow *ui;
QStandardItemModel *model;
QSqlDatabase *myDb;
QSqlTableModel *empmodel;
QItemSelectionModel *selection;
QTableView *view;
QModelIndexList indexes;
QSqlQuery *q;
//EditRecord *editrecord;
};
#endif // MAINWINDOW_H
addrecord.cpp
#include "addrecord.h"
#include "ui_addrecord.h"
AddRecord::AddRecord(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddRecord)
{
ui->setupUi(this);
}
AddRecord::~AddRecord()
{
delete ui;
}
void AddRecord::on_btnQuit_clicked()
{
this->close();
}
void AddRecord::on_btnAdd_clicked()
{
QSqlDatabase db1 = QSqlDatabase::addDatabase("QMYSQL");
db1.setHostName("localhost");
db1.setDatabaseName("dbname");
db1.setUserName("user");
db1.setPassword(„passwd");
db1.open();
QString gkUserid,name,second_name,surname,date_of_birth,NIP,street,postalcode,desc,telefhone,mobile_phone,email,sex,city;
name = ui->nameEdit->text();
second_name = ui->secondNameEdit->text();
surname = ui->surnameEdit->text();
date_of_birth = ui->dateofBirthEdit->text();
NIP = ui->nipEdit->text();
street = ui->streetEdit->text();
postalcode = ui->postalCodeEdit->text();
desc = ui->descEdit->acceptRichText();
telefhone = ui->telephoneEdit->text();
mobile_phone = ui->mobilePhoneEdit->text();
email = ui->eMailEdit->text();
sex = ui->sexEdit->text();
city = ui->cityEdit->text();
if(!db1.open()){
qDebug()<<"Failed to open database";
return;
} else {
qDebug()<<"OK";
}
QSqlQuery query("qt_mysql");
query.prepare("INSERT INTO gkUsers VALUES (:gkUserid,:name,:second_name,:surname,:date_of_birth,:NIP,:street,:postal_code,:desc,:telephone,:mobile_phone,:email,:sex,:city)");
query.bindValue(":name",name);
query.bindValue(":second_name",second_name);
query.bindValue(":surname",surname);
query.bindValue(":date_of_birth",date_of_birth);
query.bindValue(":NIP",NIP);
query.bindValue(":street",street);
query.bindValue(":postal_code",postal_code);
query.bindValue(":desc",desc);
query.bindValue(":telephone",telephone);
query.bindValue(":mobile_phone",mobile_phone);
query.bindValue(":email",email);
query.bindValue(":sex",sex);
query.bindValue(":city",city);
if(query.exec()){
QMessageBox::critical(this,tr("Save"),tr("Saved"));
db1.close();
ui->nameEdit->setText("");
ui->secondNameEdit->setText("");
ui->surnameEdit->setText("");
ui->dateofbirthEdit->setText("");
ui->nipEdit->setText("");
ui->streetEdit->setText("");
ui->postalCodeEdit->setText("");
ui->descEdit->acceptRichText();
ui->telephoneEdit->setText("");
ui->mobilephoneEdit->setText("");
ui->eMailEdit->setText("");
ui->sexEdit->setText("");
ui->cityEdit->setText("");
} else {
QMessageBox::critical(this,tr("Error"),query.lastError().text());
}
}
editrecord.cpp
#include "editrecord.h"
#include "ui_editrecord.h"
#include "mainwindow.h"
EditRecord::EditRecord(QWidget *parent):
// MainWindow(),
QDialog(parent),
//eui(new Ui::EditRecord), MainWindow(parent)
eui(new Ui::EditRecord)
{
eui->setupUi(this);
//ui->setupUi(mui->placeholder);
// EditRecord(Ui::MainWindow *ui)
//QString Surname = ui->szukajNazwiskoEdit->text();
// eui->setupUi(ui.placeholder);
}
EditRecord::~EditRecord()
{
delete eui;
}
void EditRecord::on_btnZamknij_clicked()
{
this->close();
}
**/*editrecord.cpp*/**
**/*main.cpp*/**
#include "mainwindow.h"
#include "editrecord.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
EditRecord e; //added
w.show();
return a.exec();
}
**/*main.cpp*/**
**/*mainwindow.cpp*/**
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addrecord.h"
#include <QSql>
#include <QtSql/qsqldatabase.h>
#include <QtSql/QSqlError>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/qsqldriver.h>
#include <QtSql/QSqlDriverPlugin>
#include <QtSql/qsqldriverplugin.h>
#include <QSqlQuery>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase myDb = QSqlDatabase::addDatabase("QMYSQL");
myDb.setHostName("localhost");
myDb.setDatabaseName("db");
myDb.setUserName("user");
myDb.setPassword("passwd");
myDb.open();
qDebug()<<myDb.open();
ui->tableWidget->hideColumn(0);
fillTable();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::fillTable()
{
loading = true;
int num_rows, r, c;
//QSqlQuery q(myDb);
QSqlQuery q;
//get the number of rows
if(!q.exec("SELECT count(gkUserid) as num_rows FROM gkUsers")) qDebug()<< q.lastError().text();
q.first();
num_rows = q.value(0).toInt();
ui->tableWidget->setRowCount(num_rows);
ui->tableWidget->setMaximumWidth(1700);
ui->tableWidget->setMaximumHeight(300);
if(!q.exec("SELECT gkUserid, name, second_name, surname, date_of_birth, NIP, street, postalcode, desc, telephone, mobile_phone, email, sex, city FROM gkUsers ORDER BY gkUserid")) qDebug() << q.lastError().text();
for(r = 0, q.first(); q.isValid(); q.next(), ++r)
{
//for(c = 0; c < q.numRowsAffected(); ++c)
for(c = 0; c < 14; ++c)
{
ui->tableWidget->setItem(r,c, new QTableWidgetItem(q.value(c).toString()));
}
}
loading = false;
}
void MainWindow::on_tableWidget_cellChanged(int row, int column)
{
//int id = ui->tableWidget->item(row, 0)->text().toInt();
if (loading) return;
QSqlQuery q;
q.prepare("UPDATE gkUsers SET name = :i, second_name = :d_i, surname = :n, date_of_birth = :d_u, NIP = :N, street = :u, postal_code = :k, opis = :o, telephone = :t, mobile_phone = :t_k, email = :e, sex = :p, city = :m WHERE gkUserid = :gkUserid");
q.bindValue(":i", ui->tableWidget->item(row, 1)->text());
q.bindValue(":d_i",ui->tableWidget->item(row, 2)->text());
q.bindValue(":n", ui->tableWidget->item(row, 3)->text());
q.bindValue(":d_u", ui->tableWidget->item(row, 4)->text());
q.bindValue(":N", ui->tableWidget->item(row, 5)->text());
q.bindValue(":u", ui->tableWidget->item(row, 6)->text());
q.bindValue(":k", ui->tableWidget->item(row, 7)->text());
q.bindValue(":o", ui->tableWidget->item(row, 8)->text());
q.bindValue(":t", ui->tableWidget->item(row, 9)->text());
q.bindValue(":t_k", ui->tableWidget->item(row, 10)->text());
q.bindValue(":e", ui->tableWidget->item(row, 11)->text());
q.bindValue(":p", ui->tableWidget->item(row, 12)->text());
q.bindValue(":m", ui->tableWidget->item(row, 13)->text());
q.bindValue(":gkUserid", ui->tableWidget->item(row, 0)->text().toInt());
if(!q.exec()) qDebug() << q.lastError().text();
fillTable();
}
void MainWindow::on_btnQuit_clicked()
{
this->close();
}
void MainWindow::on_btnAdd_clicked()
{
//QMainWindow window;
//AddRecord * addrecord = new AddRecord(this);
AddRecord addrecord;
addrecord.setModal(true);
addrecord.exec();
}
void MainWindow::on_btnSearchSurname_clicked()
{
QString Surname = ui->searchSurnameEdit->text();
qDebug()<<Surname;
QSqlDatabase myDb = QSqlDatabase::addDatabase("QMYSQL");
myDb.setHostName("localhost");
myDb.setDatabaseName("db");
myDb.setUserName("user");
myDb.setPassword("passwd");
qDebug()<<myDb.open();
if(!myDb.open()){
qDebug()<<"There is no connection to DB";
return;
}
QSqlQuery qry;
if(qry.exec("SELECT gkUserid, name, second_name, surname, date_of_birth, NIP, street, postal_code, desc, telephone, mobile_phone, email, sex, city FROM gkUsers WHERE surname = \'" + Surname + "\'"))
{
if(qry.next()){
QString msg1 = qry.value(1).toString();
QString msg2 = qry.value(2).toString();
QString msg3 = qry.value(3).toString();
QString msg4 = qry.value(4).toString();
QString msg5 = qry.value(5).toString();
QString msg6 = qry.value(6).toString();
QString msg7 = qry.value(7).toString();
QString msg8 = qry.value(8).toString();
QString msg9 = qry.value(9).toString();
QString msg10 = qry.value(10).toString();
QString msg11 = qry.value(11).toString();
QString msg12 = qry.value(12).toString();
QString msg13 = qry.value(13).toString();
QString msg14 = qry.value(14).toString();
QString msg15 = qry.value(15).toString();
ui->nameEdit->setText(msg1);
ui->surnameEdit->setText(msg3);
ui->dateofbirthEdit->setText(msg4);
ui->nipEdit->setText(msg5);
ui->telEdit->setText(msg9);
ui->sexEdit->setText(msg12);
ui->mobileEdit->setText(msg10);
ui->streetEdit->setText(msg5);
ui->cityEdit->setText(msg13);
ui->descEdit->setText(msg8);
myDb.close();
} else {
qDebug()<<"Something went wrong";
}
}
}
void MainWindow::on_btnEditData_clicked()
{
EditRecord editrecord;
editrecord.setModal(true);
editrecord.exec();
//editrecord = new EditRecord(this);
//editrecord->show();
}
mydelegate.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MyDelegate
TEMPLATE = app
QT += sql
SOURCES += main.cpp\
mainwindow.cpp \
addrecord.cpp \
editrecord.cpp
HEADERS += mainwindow.h \
addrecord.h \
editrecord.h
FORMS += mainwindow.ui \
addrecord.ui \
editrecord.ui \
edit_record.ui
Upvotes: 1
Views: 99
Reputation: 2290
It's because each class has its own Ui
which represents the specific UI elements you've defined for that Form and not others. So in EditRecord
class you can't access ui->name_of_the_gap
, because no such thing is defined in the EditRecord
form. The fact that it's been defined for your other class, is irrelevant here, because you have no access to it.
The solution is to get whatever info you need (in your case, the text that has been entered in the QLineEdit
of MainWindow
) before showing the EditRecord
and then pass that value to EditRecord
. In other words, you have to get the value from QLineEdit
when you can access it and pass that value, instead of trying to access that QLineEdit
when you can't.
For doing this you have to pass that value to the constructor of you EditForm
. The changes you need are like this:
//In editrecord.h:
explicit EditRecord(QString surname, QWidget *parent = 0);
//In editrecord.cpp:
EditRecord::EditRecord(QString surname, QWidget *parent):
QDialog(parent),
eui(new Ui::EditRecord)
{
eui->setupUi(this);
//Now you have access to 'surname'. Do whatever you need to do with it.
//...
}
//In maitwindow.cpp:
void MainWindow::on_btnEditData_clicked()
{
QString surname = ui->name_of_the_gap->text();
EditRecord editrecord(surname);
editrecord.setModal(true);
editrecord.exec();
}
Note that you can also send the ui
this way too, but it's considered bad in OOP.
Upvotes: 1