c++ - Unavailable Ui properties -


i've been trying implement project in c++ using qt creator – qt creator 3.3.1 (opensource) based on qt creator 5.4.1. use ubuntu 14.04.

i've found tutorials of subject simillar wanted create, i've been studying code , trying fit needs. gui project. i've been dealing project learn more c++, oop. did 2 forms.

the project consists of 3 classes now. 1 class includes form gather information persons – works. main class includes qtablewidget present details persons database in table, implemented method find persons (also in main class), searching surnames – in form used qlineedit it.

but need form edit information strict person. decided implement form edit information in class. there occurred problem, because edit information strict person, need able read typed gap of qlineedit , make search in database using information (from qlineedit included in form of main class).

the issue that, in second class (to edit) when use construction in constructor as: qstring surname = ui->name_of_the_gap->text(); - ”name_of_the_gap” name of gap includes surname want use happens unavailable ui (the ui in class in there form edit information).

i have tried use inheritance non of works. please ask direct me / point me out how should / should change?

below present pieces of 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 open database";         return;     } else {         qdebug()<<"ok";     }     qsqlquery query("qt_mysql");     query.prepare("insert 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 number of rows     if(!q.exec("select count(gkuserid) num_rows 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 gkusers order 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 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 no connection 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 gkusers 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           

it's because each class has own ui represents specific ui elements you've defined form , not others. in editrecord class can't access ui->name_of_the_gap, because no such thing defined in editrecord form. fact it's been defined other class, irrelevant here, because have no access it.

the solution whatever info need (in case, text has been entered in qlineedit of mainwindow) before showing editrecord , pass value editrecord. in other words, have value qlineedit when can access , pass value, instead of trying access qlineedit when can't.

for doing have pass value constructor of editform. changes need 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 have access 'surname'. whatever need 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 can send ui way too, it's considered bad in oop.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -