send data from one tab lineedit to another tab lineedit in tab widget
-
Hi everyone,
I am new to qt programming. i am trying to make a qt application which takes data from user and stores in the database. I want user id which I take from user in tab 1 to appear in tab 2 user id field whenever I click push button in tab 1. Can anybody help me out with concept and code.
-
Hi
To make a button do anything.
Place in designer
Right click button and select "Goto Slot"
Then select released()
Now you get a so called slot. (a function)
Now when u press and release over this button,
it sends the signal "released()"
Since we told it to make a slot for us, our code (the slot is called)
This signal and slot concept is used in all of Qt so its a must read.
http://doc.qt.io/qt-5/signalsandslots.htmlvoid MainWindow::on_pushButton_released() {
{
QString text=ui->lineEdit->text(); // take text from first
ui->lineEdit_2->setText(text); // set on the other
}Test project:
https://www.dropbox.com/s/7jnq81324xvpvgp/myedits.zip?dl=0 -
Hi,
Thanks for the help and reply . But the thing is that I have created separate Ui form for both the tabs i.e. I have separate header files, cpp files for both tabs( like tab1.cpp and tab2.cpp). Can you help me out with this case.
-
Hi
So its two forms, each having a tabwidget?How will it work?
The mainwindow have BOTH tab UI at the same time?
Or do you use it as dialogs?
First open one, then open the second?
-
https://s18.postimg.org/idtrbfwvt/Untitled1.png
https://s11.postimg.org/47bujwyoj/Untitled2.pngSir, please see the screenshots. On the above links. Actually it is a single tab widget but I have created separate classes for both
-
Yes, I understand you have made 2 UI forms files.
But will you use them as windows/dialogs?
Show them one by one like a popup window?
Or will you embed both into the mainwindow ?
-
I want to embed them in main window itself
-
Ok, then its much like the sample.
You can use the promote feature to embed the UI into main window.
http://doc.qt.io/qt-5/designer-using-custom-widgets.html
OR simply new and insert them your self.
But there is one HUGE difference.
The UI is private.
So you cannot easy copy text in mainwindow as you cannot acces it.So its better to do in the class for the UI. there you create slot for button and
save it to a variable/structure.
You can then take from this structure in other tab. -
Sir, I am not able to understand. Can please look at the code below and suggest
userdetails.h
#ifndef USER_DATA_H
#define USER_DATA_H
#include<QDebug>
#include<QtSql>
#include<QSqlDatabase>
#include <QWidget>namespace Ui {
class User_data;
}class User_data : public QWidget
{
Q_OBJECTpublic:
explicit User_data(QWidget *parent = 0);
~User_data();private slots:
void on_user_submit_clicked();private:
Ui::User_data *ui;};
#endif // USER_DATA_H
USERDETAILS.CPP
#include "user_data.h"
#include "ui_user_data.h"
#include<QDebug>
#include<QFile>
#include<QTextStream>User_data::User_data(QWidget *parent) :
QWidget(parent),
ui(new Ui::User_data)
{
ui->setupUi(this);}
User_data::~User_data()
{
delete ui;
}void User_data::on_user_submit_clicked()
{QSqlDatabase mydb= QSqlDatabase ::addDatabase("QSQLITE"); mydb.setDatabaseName("/d/Sqlite/user.sqlite"); if(!mydb.open()) qDebug() << "Failed"; else qDebug() << "Connected"; QSqlQuery qry; QString id; id=ui->user_id->text(); qry.prepare("Select * from user_data where Oracle_ID='"+id+"'"); if(qry.exec()) { while(qry.next()) { ui->user_name->setText(qry.value(1).toString()); ui->user_designation->setText(qry.value(2).toString()); ui->user_pid->setText(qry.value(3).toString()); ui->user_office->setText(qry.value(4).toString()); ui->user_start->setText(qry.value(5).toString()); ui->user_end->setText(qry.value(6).toString()); qDebug() << "Success"; } }
}
comp.h
#ifndef COMP_OFF_H
#define COMP_OFF_H
#include<QDebug>
#include<QtSql>
#include<QSqlDatabase>
#include <QWidget>namespace Ui {
class Comp_off;
}class Comp_off : public QWidget
{
Q_OBJECTpublic:
explicit Comp_off(QWidget *parent = 0);
~Comp_off();private slots:
void on_comp_submit_clicked();private:
Ui::Comp_off *ui;};
#endif // COMP_OFF_H
comp.cpp
#include "comp_off.h"
#include "ui_comp_off.h"
#include<QMessageBox>
#include<QDebug>
#include<QFile>
#include<QTextStream>Comp_off::Comp_off(QWidget *parent) :
QWidget(parent),
ui(new Ui::Comp_off)
{
ui->setupUi(this);}
Comp_off::~Comp_off()
{
delete ui;
}void Comp_off::on_comp_submit_clicked()
{/* QSqlDatabase mydb= QSqlDatabase ::addDatabase("QSQLITE");
mydb.setDatabaseName("/d/Sqlite/comp_off.sqlite");
if(!mydb.open())qDebug() << "Failed"; else qDebug() << "Connected";
*/
QString id,date,hours;id=ui->comp_id->text(); date=ui->comp_date->text(); hours=ui->comp_hours->text(); QSqlQuery qry; qry.prepare("Insert into compoff(Oracle_ID,Date,Hours) values ('"+id+"','"+date+"','"+hours+"')"); if(qry.exec()) { qDebug() << "Success"; } QMessageBox::information(this,tr("Submitted"),tr("Thank you! Your Response Recorded"));
}
main.cpp
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QWidget>
#include<comp_off.h>
#include<shift_tracker.h>
#include<user_data.h>
#include<vacation_tracker.h>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);auto userdata = new User_data(this); ui->user_layout->addWidget(userdata); auto shift = new Shift_tracker(this); ui->shift_layout->addWidget(shift); auto vacation = new Vacation_tracker(this); ui->vacation_layout->addWidget(vacation); auto comp = new Comp_off(this); ui->comp_layout->addWidget(comp);
}
MainWindow::~MainWindow()
{
delete ui;
} -
it looks fine.
So if you save in User_data to database, then in
Comp_off, you can just read the data again?No need to send from edit to edit.
Cant the tab2 (Comp_off?) just read the data it needs ?
-
Actually Sir,
In userdetails database, I am having user details. So when I enter my Oracle Id in userdetails tab and click on load my other fields fetches data and populates then in user details tab. Now in comp off , I want that when i click load, it takes the oracle id from oracle id field in user detail tab. As compoff is my transaction tab.
-
Well, the UI is private so you need to the info accessible in
user detail class
You could use a public function
QString getOracleID()
that simply returns ui->the_line_edit->text() -
Sir,
Can you help with providing modifications in the code. What actually is to be done. It will be really helpful -
Hi, I tried implemeting it. But data is not passing. Please have a look at the code.
Userdetail.h
#ifndef USER_DATA_H
#define USER_DATA_H
#include<QDebug>
#include<QtSql>
#include<QSqlDatabase>
#include <QWidget>namespace Ui {
class User_data;
}class User_data : public QWidget
{
Q_OBJECTpublic:
explicit User_data(QWidget *parent = 0);
~User_data();
signals:
void value(QString);
private slots:
void on_user_submit_clicked();private:
Ui::User_data *ui;
QString data;};
#endif // USER_DATA_H
Userdetail.cpp
#include "user_data.h"
#include "ui_user_data.h"
#include<QDebug>
#include<QFile>
#include<QTextStream>User_data::User_data(QWidget *parent) :
QWidget(parent),
ui(new Ui::User_data)
{
ui->setupUi(this);}
User_data::~User_data()
{
delete ui;
}void User_data::on_user_submit_clicked()
{QSqlDatabase mydb= QSqlDatabase ::addDatabase("QSQLITE"); mydb.setDatabaseName("/d/Sqlite/user.sqlite"); if(!mydb.open()) qDebug() << "Failed"; else qDebug() << "Connected"; QSqlQuery qry; QString id; id=ui->user_id->text(); data= ui->user_id->text(); emit value(data); qry.prepare("Select * from user_data where Oracle_ID='"+id+"'"); if(qry.exec()) { while(qry.next()) { ui->user_name->setText(qry.value(1).toString()); ui->user_designation->setText(qry.value(2).toString()); ui->user_pid->setText(qry.value(3).toString()); ui->user_office->setText(qry.value(4).toString()); ui->user_start->setText(qry.value(5).toString()); ui->user_end->setText(qry.value(6).toString()); qDebug() << "Success"; } }
}
comp.h
#ifndef COMP_OFF_H
#define COMP_OFF_H
#include<QDebug>
#include<QtSql>
#include<QSqlDatabase>
#include <QWidget>namespace Ui {
class Comp_off;
}class Comp_off : public QWidget
{
Q_OBJECTpublic:
explicit Comp_off(QWidget *parent = 0);
~Comp_off();private slots:
void on_comp_submit_clicked();
public slots:
void receivevalue(QString val);private:
Ui::Comp_off *ui;};
#endif // COMP_OFF_H
Comp.cpp
#include "comp_off.h"
#include "ui_comp_off.h"
#include<QMessageBox>
#include<QDebug>
#include<QFile>
#include<QTextStream>Comp_off::Comp_off(QWidget *parent) :
QWidget(parent),
ui(new Ui::Comp_off)
{
ui->setupUi(this);}
Comp_off::~Comp_off()
{
delete ui;
}QString id;
void Comp_off::receivevalue(QString value)
{
id=value;
}void Comp_off::on_comp_submit_clicked()
{QString date,hours; // id=ui->comp_id->text(); date=ui->comp_date->text(); hours=ui->comp_hours->text(); QSqlQuery qry; qry.prepare("Insert into compoff(Oracle_ID,Date,Hours) values ('"+id+"','"+date+"','"+hours+"')"); if(qry.exec()) { qDebug() << "Success"; } QMessageBox::information(this,tr("Submitted"),tr("Thank you! Your Response Recorded"));
}
main.h
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}
mainwindow.h
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QWidget>
#include<comp_off.h>
#include<shift_tracker.h>
#include<user_data.h>
#include<vacation_tracker.h>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);auto userdata = new User_data(this); ui->user_layout->addWidget(userdata); auto shift = new Shift_tracker(this); ui->shift_layout->addWidget(shift); auto vacation = new Vacation_tracker(this); ui->vacation_layout->addWidget(vacation); auto comp = new Comp_off(this); ui->comp_layout->addWidget(comp); User_data a; Comp_off b; QObject::connect(&a, SIGNAL(value(QString)), &b, SLOT(receivevalue(QString)));
}
-
Hi
Why are you making new ones ???User_data a; <<< new one ! should it not be userdata ?
Comp_off b; <<<<< new one!! should be comp ?
QObject::connect(&a, SIGNAL(value(QString)),
&b, SLOT(receivevalue(QString)));both a,b will be deleted as soon as constructor is finished ! -
so this look completely wrong :)Use the objects you create
QObject::connect(userdata , SIGNAL(value(QString)), comp , SLOT(receivevalue(QString)));