【问题标题】:Send data from one UI to another UI in QT在 QT 中将数据从一个 UI 发送到另一个 UI
【发布时间】:2021-12-29 13:41:45
【问题描述】:

**我发布了我想出的解决方案作为答案

我有一个学生登录用户界面,我输入登录 ID 和密码,并从我创建的 MySQL 数据库中验证它们。登录后,我需要保存输入的学生 ID 并将其发送到学生信息页面(我为学生创建的另一个 UI)。 我正在使用 QT + C++ + MySQL。 我尝试使用信号和插槽但没有结果

我的主要目标是将输入的学生 ID 发送到其他 UI。

ma​​inwindow.cpp

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow){
ui->setupUi(this);
connect(ui->userIDLogin, SIGNAL(textChanged(QString)), ui->stdName ,SLOT(setText(QString))); }

它无法识别我试图将数据发送到的学生页面中的对象的 ui->stdName

ma​​inwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
#include <QDialog>
#include <QSqlDatabase>
#include <QMessageBox>
#include <mysql/mysql.h>
#include "student.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

public slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
Student *std;
};

struct connection_details;
MYSQL *mysql_connection_setup(struct connection_details mysql_details);

#endif // MAINWINDOW_H

这是我收到的错误消息:

This is the file error message
/home/ahmad/Desktop/DBMS project/DBMS-project-                        
  master/mainwindow.cpp:38: error: ‘class Ui::MainWindow’ has no 
member named ‘stdName’
In file included from ../DBMS project/DBMS-project-    
master/student.cpp:2:
../DBMS project/DBMS-project-master/mainwindow.cpp: In constructor 
‘MainWindow::MainWindow(QWidget*)’:
../DBMS project/DBMS-project-master/mainwindow.cpp:38:73: error: 
‘class Ui::MainWindow’ has no member named ‘stdName’
38 |     QObject::connect(ui->userIDLogin, 
SIGNAL(textChanged(QString)), ui->stdName,SLOT(setText(QString)));
   |                        
                                             ^~~~~~~

【问题讨论】:

  • 那么会发生什么?任何错误信息?你看到打印出来的东西了吗?
  • 在 mainwindow.cpp 中无法识别 ui->stdName,这是我要将数据发送到的对象。我将编辑问题并添加这篇文章
  • 了解 Qt5 中引入的新信号/插槽连接。我敢打赌,您的连接不起作用,但这种旧样式在编译时没有提供足够的错误诊断。见wiki.qt.io/New_Signal_Slot_Syntax
  • @AhmadBader 你能复制/粘贴确切的信息吗?
  • 太长了,我加到问题里了

标签: c++ qt mysql-connector


【解决方案1】:

我想出的解决方案效果很好。

我有两个界面,一个登录和一个学生页面(显示有关学生的信息)。

登录后,我为学生类写了默认构造函数 (学生页面基本上是在 student.h 和 student.cpp 文件中定义的一个类)

解决办法是写一个参数化的构造函数,将登录ID(我要发送到学生页面的数据)作为参数从登录类传递给学生类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2013-09-29
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多