how to call a cpp from another cpp



  • Hi all,
    I wrote this code in a.cpp "ui->roomnolineEdit->setText(qry.value(0).toString());" and roomlineEdit comes from
    b.cpp, but it does't work, i mean the roomlineEdit cant be read or called from b.cpp. How can i make it run?



  • That's probably because whatever class you have defined roomnolineEdit in isn't included or known by whatever class or function you use in a.cpp.

    Could you give us an overview of what you are trying to achieve here?



  • @helgur in a.cppp file i have an tableview which shows some row from database, after i select the data from tableview, i want the data i selected goes to LineEdit which locates in b.cpp.
    If i want this data goes to lineEdit which locates in same cpp with tableview, "ui->roomnolineEdit->setText(qry.value(0).toString());" is used and work perfectly.
    But here in my code "ui->roomnolineEdit->setText(qry.value(0).toString());", roomlineEdit belongs to (b.cpp,b.h,b.ui).
    You can have a look my code, i commentted in the line by writing "to do ??? what should be code here??"

    Btw, I use Grafical User Interface.



  • here is the place where i want to call lineEdit from #include <reservation.h>

    #include "roomavability.h"
    #include "ui_roomavability.h"
    #include <login.h>
    #include <reservation.h>
    #include <QMessageBox>
    #include <mainwindow.h>
    #include "ui_reservation.h"

    roomavability::roomavability(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::roomavability)
    {
    ui->setupUi(this);
    //this->setFixedSize(514,424);

    }

    roomavability::~roomavability()
    {
    delete ui;
    }

    void roomavability::on_showpushButton_clicked()
    {
    login conn;
    QSqlQueryModel * modal=new QSqlQueryModel();
    conn.connOpen();
    QSqlQuery* qry= new QSqlQuery(conn.db);
    qry->prepare("select Room_No from Room_Info");
    qry->exec();
    modal->setQuery(*qry);
    ui->listView->setModel(modal);
    ui->showcomboBox->setModel(modal);
    ui->tableView->setModel(modal);
    conn.close();
    qDebug() << (modal->rowCount());
    //close();
    }

    void roomavability::on_showcomboBox_currentIndexChanged(const QString &arg1)
    {

    QString roomNo=ui->showcomboBox->currentText();
    
    login conn;
        if(!conn.connClose()){
            qDebug()<<"Failed to open the database";
            return;
        }
       conn.connOpen();
       QSqlQuery qry;
    
       qry.prepare("select * from ROOM_INFO where Room_No='"+roomNo+"'");
    
    
    if (qry.exec()){
            while(qry.exec()){
    
                //ui->roomnolineEdit->setText(qry.value(0).toString());
                //Ui_reservation::roomnolineEdit->setText(qry.value(0).toString());
                //reservation.ui->cidLine->setText(qry.value(0).toString());
                //reservation::ui->cidLine->setText(qry.value(0).toString());
                //Ui_reservation->cidLine->setText(qry.value(0).toString());
                
                to do ?????What should be code here?????
    
            }
    
        conn.connClose();
    }
    else {
        QMessageBox::critical(this,tr("error::"),qry.lastError().text());
    }
    

    }



  • And here i have second part where the lineEdit comes from

    #include <login.h>
    #include <database.h>
    #include "reservation.h"
    #include "ui_reservation.h"
    #include "mainwindow.h"
    #include "roomavability.h"

    #include <QDateEdit>
    #include <QCalendarWidget>
    #include <QMenuBar>
    #include <QMenu>
    #include <QMessageBox>
    #include <reservation.h>
    #include <roomavability.h>

    reservation::reservation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::reservation)
    {
    ui->setupUi(this);
    ui->calendarWidget->hide();

    QMenuBar *menu = new QMenuBar(this);
    QMenu* menu1 = new QMenu("MENU1");
    QMenu* menu2 = new QMenu("MENU2");
    QMenu* menu3 = new QMenu("MENU3");
    menu1->addMenu(new QMenu("menu1_SubMenu"));
    menu2->addMenu(new QMenu("menu2_SubMenu"));
    menu2->addMenu(new QMenu("menu3_SubMenu"));
    menu->addMenu(menu1);
    menu->addMenu(menu2);
    menu->addMenu(menu3);
    

    }

    reservation::~reservation()
    {
    delete ui;
    }
    //QString cid, name, surname,gender,meritial, tel1, tel2, email, city, state, country, adress, reservation_date, checkin_date,checkout_date,roomno, roomtype, roomprice, discount, no_of_nigths, no_of_adult, no_of_child, total;

    void reservation::on_rbackpushButton_clicked()
    {
    MainWindow *window= new MainWindow();
    this->hide();
    window->show();
    }

    void reservation::on_clearpushButton_clicked()
    {
    int message=QMessageBox::information(this,"Information","Are you sure to delete!",QMessageBox::Yes,QMessageBox::No);
    if (message == QMessageBox::Yes)
    {
    foreach(QLineEdit* erase, findChildren<QLineEdit*>())
    {
    erase->clear();
    }
    foreach(QRadioButton* erase,findChildren<QRadioButton*>())
    {
    erase->setAutoExclusive(false);
    erase->setChecked(false);
    erase->setAutoExclusive(true);
    }
    ui->addresstextEdit->clear();
    ui->nightsspinBox->setValue(1);
    ui->adultspinBox->setValue(0);
    ui->childrenspinBox->setValue(0);
    }
    }

    void reservation::on_checkintoolButton_clicked()
    {
    ui->calendarWidget->show();
    }

    void reservation::on_savepushButton_clicked()
    {
    login conn;
    QString cus_id,name, surname, meritial, tel1, tel2, mail, city, state, country;
    cus_id=ui->cidLine->text();
    name=ui->cnamelineEdit->text();
    surname=ui->csurnamelineEdit->text();
    meritial=ui->mstatuscomboBox->currentText();
    tel1=ui->pnumber1LineEdit->text();
    tel2=ui->pnumber2label->text();
    mail=ui->emailLineEdit->text();
    city=ui->cityLineEdit->text();
    state=ui->statelineEdit->text();
    country=ui->countrylineEdit->text();

       conn.connOpen();
       QSqlQuery qry;
       qry.prepare("insert into CUSTOMER(Custemer_ID,Custemer_Name,Custemer_Surname,Merital_Status, Telephone_1, Telephone_2, EMail_Adress, City, State, Country )values('"+cus_id+"','"+name+"','"+surname+"','"+meritial+"', '"+tel1+"', '"+tel2+"', '"+mail+"',  '"+city+"', '"+state+"',  '"+country+"' )");
    
    
    if (qry.exec()){
    
        QMessageBox::critical(this,tr("save"),tr("saved"));
        conn.connClose();
    }
    else {
        QMessageBox::critical(this,tr("error::"),qry.lastError().text());
    }
    

    }

    void reservation::on_ravaiblitypushButton_clicked()
    {
    roomavability *window= new roomavability();
    window->show();
    }

    void reservation::on_pushButton_clicked()
    {

    login conn;
       QString cus_id,name, surname, meritial, tel1, tel2, mail, city, state, country;
       cus_id=ui->cidLine->text();
       name=ui->cnamelineEdit->text();
       surname=ui->csurnamelineEdit->text();
       meritial=ui->mstatuscomboBox->currentText();
       tel1=ui->pnumber1LineEdit->text();
       tel2=ui->pnumber2label->text();
       mail=ui->emailLineEdit->text();
       city=ui->cityLineEdit->text();
       state=ui->statelineEdit->text();
       country=ui->countrylineEdit->text();
    
       conn.connOpen();
       QSqlQuery qry;
    
       qry.prepare("update CUSTOMER set Custemer_ID='"+cus_id+"', Custemer_Name='"+name+"', Custemer_Surname='"+surname+"', Merital_Status='"+meritial+"', Telephone_1='"+tel1+"', Telephone_2='"+tel2+"',  City='"+city+"', State='"+state+"', Country='"+country+"'  where Custemer_ID='"+cus_id+"'");
    
    
       //qry.prepare("insert into Room_Info(Room_No, Room_Type, Room_Price, Discount, Total_Price ) values('"+roomno+"', '"+roomtype+"', '"+roomprice+"','"+discount+"', '"+total +"')");
    
    if (qry.exec()){
    
        QMessageBox::critical(this,tr("Edit"),tr("Updated"));
        conn.connClose();
    }
    else {
        QMessageBox::critical(this,tr("error::"),qry.lastError().text());
    }
    

    }

    void reservation::on_pushButton_2_clicked()
    {
    login conn;
    QString cus_id,name, surname, meritial, tel1, tel2, mail, city, state, country;
    cus_id=ui->cidLine->text();
    name=ui->cnamelineEdit->text();
    surname=ui->csurnamelineEdit->text();
    meritial=ui->mstatuscomboBox->currentText();
    tel1=ui->pnumber1LineEdit->text();
    tel2=ui->pnumber2label->text();
    mail=ui->emailLineEdit->text();
    city=ui->cityLineEdit->text();
    state=ui->statelineEdit->text();
    country=ui->countrylineEdit->text();

       conn.connOpen();
       QSqlQuery qry;
    
    
       qry.prepare("delete from CUSTOMER where Custemer_ID='"+cus_id+"'");
       //qry.prepare("insert into Room_Info(Room_No, Room_Type, Room_Price, Discount, Total_Price ) values('"+roomno+"', '"+roomtype+"', '"+roomprice+"','"+discount+"', '"+total +"')");
    
    if (qry.exec()){
    
        QMessageBox::critical(this,tr("Delete"),tr("Deleted"));
        conn.connClose();
    }
    else {
        QMessageBox::critical(this,tr("error::"),qry.lastError().text());
    }
    

    }



  • @cinar said:

    ui->roomnolineEdit

    It doesn't work that way. You already have Ui:: namespace set up by default to use the class you write your code in. So forget calling ui directly except when you are accessing the Ui elements of the class belonging to the file you are actually writing code for.

    Instead, use signals and slots to make the classes communicate.

    So in the roomavailability class, define your reservation class (privately) to something like this

    private
        Reservation *res;
    

    setup signal in your roomavailability:

    signals:
        void changeInput(QString,QString);
    

    The slot in reservation class

    slots:
        void setInput(QString roomoline, QString cidline)
    

    then in the setInput:

    void reservation::setInput(QString roomoline, QString cidline) {
        ui->cidLine->setText(cidline);
        ui->roomolineEdit->setText(roomoline);
    }
    

    when you initialize Reservation object in roomavailability connect these two

    connect(this, SIGNAL(changeInput(QString,QString)), res, SLOT(setInput(QString, QString)));
    

    and call emit changeInput(qry.value(0).toString(), qry.value(0).toString()) from roomavability::on_showcomboBox_currentIndexChanged(const QString &arg1)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.