如何从另一个类的一个类(窗口UI)访问QString值? [英] How to access QString value from one class (window UI) in another class?

查看:88
本文介绍了如何从另一个类的一个类(窗口UI)访问QString值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将编写一个小型桌面应用程序,以捕获同事的工作时间.它必须根据我们的需求进行定制,因此不能选择商业解决方案(而且我们没有钱).

I am about to program a small desktop application to capture the working times of my fellow co-workers. It must be customized to our needs, therefore a commercial solution is not an option (+ we have no money).

我正在使用Qt(5.11.2)和C ++.我有一个MainWindow(第一个窗口UI),该窗口使用自动完成功能从sqlite3数据库中获取信息(ID,名称,组长等),并填充QLineEdits.这部分工作正常.现在,我创建了第二个窗口UI,以捕获员工的实际工作时间.我想在第二个窗口中向用户显示其工作时间被限制的员工的姓名和ID.这些信息应从第一个窗口类中检索.

I am using Qt (5.11.2) and C++. I have a MainWindow (first window UI) that uses auto-completion to get the information (ID, Names, Group Leaders, ...) from a sqlite3 database and fill the QLineEdits. This part is working just fine. Now, I have created a second window UI to capture the actual working times for the employee. I would like to present the user in this second window with the name and the ID of the employee whose working time is being caputured. These informations should be retrieved from the first window class.

我试图在第一个窗口类中公开名称变量,但由于某种原因,我无法访问第二个窗口类中的变量,并且我也尝试使用getter和setter,但无济于事.当我使用getter和setter时,我可以在setter中手动插入一个字符串,它可以正常工作.但我想从第一个窗口的lineEdit中获取字符串的值(请参见小代码段)

I have tried to make the name variable public in my first window class but for some reason I couln't access the variable in my second window class, and I have also tried to use getters and setters, but to no avail. When I use getters and setters, I can manually insert a string in the setter and it is working. But I would like to get the value of the string from a lineEdit from the first window (see small code snippet)

QString tabname_temp = ui->lineEdit_Tabname->text(); // get name from lineEditf; from first window UI

我想使用这个tabname_temp变量,并使用它在第二个UI窗口类中的带有此字符串值的标签中显示.

I would like to work with this tabname_temp variable and use it to show in a label with this string value in the second UI window class.

第一个窗口类中的getter和setter看起来像这样:

The getters and setters in the first window class looked like this:

public:
    void setName(QString name);
    QString getName() const {return name;}

private:
    QString name;

setName的实现如下所示:

The implementation of the setName looked like this:

void MainWindow::setName(QString name){
    this->name = name;
}

长时间的谷歌搜索引擎无济于事.我敢肯定,我在这里想念一些重要的事情.我希望我明确了我要寻找的东西.请让我知道是否以及如何改善这个问题.

Long hours of google fu were to no avail. I am sure, I miss something crucial here. I hope I made clear what I am looking for. Please let me know if and how I can improve this question.

这是我创建第二个窗口UI(mainwindow.h)的方式:

This is how I create the second window UI (mainwindow.h):

private:
    Ui::MainWindow *ui;
    WindowActivity *activityWindow; // second window

这是我来自mainwindow.cpp的代码:

This is my code from mainwindow.cpp:

void MainWindow::on_Btn_Activity_clicked()
{
    activityWindow = new WindowActivity(this);
    activityWindow->resize(700,700);
    activityWindow->show();
}

推荐答案

我试图在第一个窗口类中公开名称变量,但由于某种原因,我无法在第二个窗口类中访问变量

I have tried to make the name variable public in my first window class but for some reason I couln't access the variable in my second window class

这是错误的方法. MainWindow 应该看到(了解) WindowActivity ,而不是相反.

This is a wrong approach. MainWindow should see (know about) WindowActivity, not the other way around.

要访问 WindowActivity 类中的 ui-> lineEdit_Tabname 的值,请执行以下操作:

To access the value of ui->lineEdit_Tabname within the WindowActivity class, do the following:

  1. WindowActivity

void setName(const QString &name);

  • activityWindow = new WindowActivity(this); 之后像这样调用 WindowActivity :: setName

    activityWindow->setName(ui->lineEdit_Tabname->text());
    

  • 这篇关于如何从另一个类的一个类(窗口UI)访问QString值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆