gpt4 book ai didi

c++ - QDialog 接受返回自定义类对象

转载 作者:行者123 更新时间:2023-11-30 04:09:41 29 4
gpt4 key购买 nike

我正在使用 qt-creator 构建一个小型 QT 应用程序。

我有一个主窗口,其中有一些控件,例如“新联系人”按钮。按下按钮,会显示一个 QDialog,它包含 3 行编辑:姓名、手机和电子邮件。

对话框通过信号/槽系统显示。它工作正常,但我想在单击“确定”时创建一个联系人对象,并且我想将该联系人返回到我的主窗口,以便将其放入在主窗口代码中创建的 QList 中。

方法是: QMainWindow -> 新联系人 -> 显示 QDialog

QDialog -> 确定 -> QMainWindow

我应该将主窗口中的 QList 作为参数传递给 QDialog 还是有最佳方法?

最佳答案

Should I pass the QList from the main window to the QDialog as argument or there is a best way?

在我看来,最好是自定义 QDialog三个子类 QLabel s 和 3 QLineEdit

标签将获得以下类型的值:

  • 标签一:姓名

  • 标签 2:手机号码

  • 标签 3:电子邮件

然后您将使用 QLabels 和 QLineEdits 来显示来自用户的输入。

然后,就像您的程序可能已经做的那样,分别处理“接受”事件。您可以使用以下方法来检索最终用户输入的文本:

text : QString

This property holds the line edit's text.

Setting this property clears the selection, clears the undo/redo history, moves the cursor to the end of the line and resets the modified property to false. The text is not validated when inserted with setText().

The text is truncated to maxLength() length.

By default, this property contains an empty string.

然后,在 accepted signal 的处理程序中,您可以调用三个访问器方法,例如:

  • QString name() const { return nameLineEdit->text();

  • QString mobileNumber() const { return mobileNumberLineEdit->text();

  • QString email() const { return emailLineEdit->text();

您还可以根据您构建的数据表示将其存储在专用结构中,因此该结构将如下所示:

struct Contact {
QString name;
QString mobileNumber;
QString email;
};

然后您将获得如下访问器

Contact contact() const;

关于c++ - QDialog 接受返回自定义类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066290/

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