gpt4 book ai didi

c++ - 不可用的 Ui 属性

转载 作者:行者123 更新时间:2023-11-30 05:44:25 26 4
gpt4 key购买 nike

我一直在尝试使用 Qt Creator 在 C++ 中实现一个项目 – 这是基于 Qt Creator 5.4.1 的 Qt Creator 3.3.1(开源)。我使用 Ubuntu 14.04。

我发现了一些主题与我想要创建的内容相似的教程,因此我一直在研究代码并尝试使其适合我的需要。这是 GUI 项目。我也一直在处理这个项目来学习更多的 C++,OOP。我做了2个表格。

该项目目前由 3 个类组成。一个类包括一个表格来收集一些关于人的信息——它有效。主类包括 QtableWidget 以在表中显示有关数据库中人员的详细信息,我还实现了查找人员的方法(也在主类中),按姓氏搜索——在表格中我使用 QlineEdit 来完成。

但是我需要另一个表格来编辑关于严格的人的信息。我决定在另一个类中实现表单来编辑信息。问题出现了,因为要编辑关于严格的人的信息,我需要能够读取我在 QlineEdit 的空白处键入的内容,然后使用此信息在数据库中进行搜索(来自 QlineEdit which以主类的形式包含)。

问题是,在第二类(编辑)中,当我在构造函数中使用构造时:QString Surname = ui->name_of_the_gap->text(); - 其中 ”name_of_the_gap” 是间隙的名称,其中包括我想使用的姓氏,但它恰好不适用于此 UI(类中的 ui,其中有此表单可编辑信息)。

我尝试过使用继承,但没有一个有效。我可以请你指导我/指出我应该怎么做/我应该改变什么吗?

下面我将向您展示代码片段:

地址记录.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*/**

编辑记录.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

主窗口.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

地址记录.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());
}
}

编辑记录.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();
}

我的代表.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

最佳答案

这是因为每个类都有自己的 Ui,代表您为该表单而不是其他表单定义的特定 UI 元素。所以在 EditRecord 类中你不能访问 ui->name_of_the_gap,因为在 EditRecord 表单中没有定义这样的东西。它已为您的其他类定义的事实与此处无关,因为您无权访问它。

解决方案是在显示 EditRecord 之前获取您需要的任何信息(在您的情况下,是在 MainWindowQLineEdit 中输入的文本) 然后将该值传递给 EditRecord。换句话说,当您可以访问它并传递该值时,您必须从 QLineEdit 获取值,而不是在您不能访问该 QLineEdit 时尝试访问该值。

为此,您必须将该值传递给 EditForm 的构造函数。你需要的改变是这样的:

//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();
}

请注意,您也可以通过这种方式发送 ui,但它在 OOP 中被认为是错误的。

关于c++ - 不可用的 Ui 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29865250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com