I just started learning Qt, and I want to make a program that can output the input content to a txt file.I can just output the content in textEdit to txt file.But I don't know how to output the content selected in combo box or the content selected in radio button.
Widget.h:
#include <QWidget>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QIODevice>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void pressBtn();
private slots:
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
};
Widget.cpp:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui-
>pushButton,SIGNAL(clicked(bool)),this,SLOT(pressBtn()));
}
//output the content in textEdit
void Widget::pressBtn(){
QString str=ui->textEdit->toPlainText();
QString str2=ui->textEdit_2->toPlainText();
QString str3=str+str2;
str3.replace("\n","\r\n");
qDebug()<<str3;
QFile file("C:/1.txt");
if(!file.open(QIODevice::WriteOnly|QIODevice::Append)){
QMessageBox::warning(this,tr("error"),tr("Fail to open
the file!"));
return;
}
QTextStream in(&file);
in<<str3;
QMessageBox::information(this,tr("tips"),tr("success!"));
QDesktopServices::openUrl(QUrl("C:/1.txt"));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_2_clicked()
{
ui->textEdit_2->clear();
ui->textEdit->clear();
}
UI image:
enter image description here
so i am writing a simple video player program and i did the same steps as the lesson i am taking but when i run the program and click on functionalities like end (which is close()) and open (open file) they dont work, i used the slot triggering as per the lesson although i saw different ways of using the menubar here but i must follow this format, here is my code:
header:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QVideoWidget>
#include <QStyle>
#include <QMediaPlayer>
#include <QFileDialog >
namespace Ui {
class videoWidget;
}
class videoWidget : public QMainWindow
{
Q_OBJECT
QMediaPlayer *meinPlayer;
QPushButton *playButton;
QPushButton *stopButton;
public:
explicit videoWidget(QWidget *parent = 0);
~videoWidget();
private slots:
void listeUndTitelAktualisieren();
void on_action_End_triggered();
void on_action_ffnen_triggered();
void on_action_Stop_triggered();
void on_action_PlayBack_triggered();
void on_action_Pause_triggered();
private:
Ui::videoWidget *ui;
};
#endif // MAINWINDOW_H
cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
videoWidget::videoWidget(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::videoWidget)
{
ui->setupUi(this);
meinPlayer = new QMediaPlayer(this);
meinPlayer->setMedia(QUrl::fromLocalFile("/beispiele/topologien.wmv"));
meinPlayer->play();
}
void videoWidget::listeUndTitelAktualisieren()
{
QString titel = meinPlayer->media().canonicalUrl().toLocalFile();
ui->listWidget->addItem(titel);
this->setWindowTitle("Multimedia-Player – " + titel);
connect(meinPlayer, SIGNAL(mediaChanged(QMediaContent)), this, SLOT(listeUndTitelAktualisieren()));
}
void videoWidget::on_action_End_triggered()
{
this->close();
}
void videoWidget::on_action_ffnen_triggered()
{
QFileDialog *meinDialog = new QFileDialog(this);
meinDialog->setAcceptMode(QFileDialog::AcceptOpen);
meinDialog->setWindowTitle("Datei öffnen");
meinDialog->setNameFilters(QStringList() << "Videos (*.mp4 *.wmv)" << "Audios (*.mp3)" << "Alle Dateien (*.*)");
meinDialog->setDirectory(QDir::currentPath());
meinDialog->setFileMode(QFileDialog::ExistingFile);
if (meinDialog->exec() == QDialog::Accepted) {
QString datei = meinDialog->selectedFiles().first();
meinPlayer->setMedia(QUrl::fromLocalFile(datei));
/*QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home/jana", tr("Video & Audio Files (*.mp3 *.mp4 *.wmv)"));
*/
meinPlayer->play();
}
}
void videoWidget::on_action_Stop_triggered()
{
meinPlayer->pause();
}
void videoWidget::on_action_PlayBack_triggered()
{
meinPlayer->play();
}
void videoWidget::on_action_Pause_triggered()
{
meinPlayer->pause();
}
am pretty sure this instruction here:
connect(meinPlayer, SIGNAL(mediaChanged(QMediaContent)), this, SLOT(listeUndTitelAktualisieren()));
is not located where it should coz you are connecting the signal toa slot INSIDE of the SLOT implementation....
try moving that to the constructor of dein videoWidget
I'm try to pass string value from mainwindow.cpp to userdetails.cpp. I had been used global variable. When the program run it does not pass the variable value to the userdetails.cpp. When I use qDebug( globelusername.toLatin1() ) it does not show any value. What is the error in this code? globelusername mean, the global variable.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "register.h"
#include "userdetails.h"
extern QString globelusername;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
QString globelusername;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui ->loginusername->setPlaceholderText("Username");
ui ->loginpassword->setPlaceholderText("Password");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
//database connection
...........
QString username = ui ->loginusername ->text();
QString password = ui ->loginpassword ->text();
if(db.open()){
//query create
// QMessageBox::information(this, "Sucessfull ","Sucessfully Connect the Database");
QSqlQuery query(QSqlDatabase::database("MyConnect"));
query.prepare(QString("SELECT * FROM user_reg_elec WHERE username = :username AND password = :password"));
query.bindValue(":username", username);
query.bindValue(":password", password);
QString globelusername = username; //globlevariable
}
userdetails.cpp
#include "userdetails.h"
#include "ui_userdetails.h"
#include <QSqlError>
#include "mainwindow.h"
#include <iostream>
Userdetails::Userdetails(QWidget *parent) :
QDialog(parent),
ui(new Ui::Userdetails)
{
ui->setupUi(this);
}
Userdetails::~Userdetails()
{
}
void Userdetails::on_pushButton_4_clicked()
{
{
// database connection
...........
qDebug(globelusername.toLatin1() );
You are not setting globelusername. instead, you are creating a local variable with the following line:
QString globelusername = username; //globlevariable
remove the variable type before setting the value:
globelusername = username; //globlevariable
I'm trying to write GUI for a program that must perform some actions with given files. Its logic will be:
1) Program starts with one text field and one button created.
2) If I click on button, I can choose some .exe file. If the file is choosen, its path is set to the textfield that is logicaly linked with first my button.
3) If the file is choosen on previous step, a new pair of text field and button linked to it is created. The size of main window must change dynamically when a new pair is added.
How can I set the path to the file to current textfield?
I need a possibility to edit data in any text field. How to organize interface so that I could separately use QFileDialog with any pair of textfield and button.
I cant figure out how to use signals/slots in this case.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void makeInterface();
private slots:
void openFile();
};
#endif
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
makeInterface();
}
MainWindow::~MainWindow() {}
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("OpenFile"),
QDir::currentPath(),
tr("Executable Files (*.exe)"));
if (!fileName.isNull())
{
qDebug() << fileName;
}
}
void MainWindow::makeInterface()
{
QGridLayout *mainLayout = new QGridLayout;
QLineEdit *fldFilePath = new QLineEdit;
QPushButton *btnOpenFile = new QPushButton("*.exe");
connect(btnOpenFile, SIGNAL(clicked()), this, SLOT(openFile()));
mainLayout->addWidget(fldFilePath, 0, 0);
mainLayout->addWidget(btnOpenFile, 0, 1);
QPushButton *btnBuild = new QPushButton("Build");
mainLayout->addWidget(btnBuild, 5, 0);
setLayout(mainLayout);
}
You should use QSignalMapper for this.
Your code may looks like this:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSignalMapper>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void makeInterface();
private slots:
void openFile(QWidget* widget);
private:
QSignalMapper _mapper;
};
#endif
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
connect(&_mapper, SIGNAL(mapped(QWidget*)), this, SLOT(openFile(QWidget*)));
makeInterface();
}
MainWindow::~MainWindow() {}
void MainWindow::openFile(QWidget* widget)
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("OpenFile"),
QDir::currentPath(),
tr("Executable Files (*.exe)"));
if (!fileName.isNull())
{
static_cast<QLineEdit*>(widget)->setText(fileName);
}
}
void MainWindow::makeInterface()
{
QGridLayout *mainLayout = new QGridLayout;
QLineEdit *fldFilePath = new QLineEdit;
QPushButton *btnOpenFile = new QPushButton("*.exe");
connect(btnOpenFile, SIGNAL(clicked()), &_mapper, SLOT(map()));
_mapper.setMapping(btnOpenFile, fldFilePath);
mainLayout->addWidget(fldFilePath, 0, 0);
mainLayout->addWidget(btnOpenFile, 0, 1);
QPushButton *btnBuild = new QPushButton("Build");
mainLayout->addWidget(btnBuild, 5, 0);
setLayout(mainLayout);
}
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
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.