【问题标题】:How do I access another windows ui from the mainwindow?如何从主窗口访问另一个 Windows ui?
【发布时间】:2020-05-20 08:46:57
【问题描述】:

所以我在 Qt 中使用 C++,我希望能够在主窗口的对话框页面上的文本框中批准用户名和密码。 这是我目前所拥有的;

    //Login
    QString username1 = ui.employeelogindialog->Username->text();
    QString password1 = ui.employeelogindialog->Password->text();

说有错误;

mainwindow.cpp:37:27: 错误:成员引用类型 'Ui::EmployeeloginDialog *' 是一个指针;你的意思是使用'->'吗?

我该如何解决这个问题?

【问题讨论】:

标签: c++ qt


【解决方案1】:

要从另一个小部件获取主窗口的数据,您可以为该小部件创建信号并在主窗口 cpp 中发出它。

您的ui 指针应为private 以保护它。

例如小部件类:

#include "ui_widget.h"

class Widget: public QWidget, private Ui::Widget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    QLineEdit *Username= nullptr;
    QLineEdit *Password= nullptr;

signals:
    void SendUsername(const QString &name);
    void SendPassword(const QString &password);

private slots:
    void ReturnPressedForLineEdit1();
    void ReturnPressedForLineEdit2();

private:
    QString username;
    QString password;

private:
    Ui::Widget *ui; //HERE <----------------
};

cpp 文件:

#include "widget.h"

Widget::Widget (QWidget *parent)
    : QWidget (parent)
    , ui(new Ui::Widget)
{
    Widget::ui->setupUi(this);

    try
    {
        //Objects of UI
        Widget::Username = new QLineEdit();
        Widget::Password = new QLineEdit();
    }
    catch(std::bad_alloc &exp)
    {
        #ifndef Q_DEBUG
        qCritical() << "Exception caught: " << exp.std::bad_alloc::what();
        #endif
        abort();
    }
    catch(...)
    {
        #ifndef Q_DEBUG
        qCritical() << "Some exception caught";
        #endif
        abort();
    }

    Widget::username = "";
    Widget::password = "";


    //SIGNALS/SLOTS
    //QLineEdit
    QObject::connect(Widget::Username, &QLineEdit::returnPressed, this, &Widget::ReturnPressedForLineEdit1);
    QObject::connect(Widget::Username, &QLineEdit::returnPressed, Widget::Username, &QLineEdit::clear);

    QObject::connect(Widget::Password, &QLineEdit::returnPressed, this, &Widget::ReturnPressedForLineEdit2);
    QObject::connect(Widget::Password, &QLineEdit::returnPressed, Widget::Password, &QLineEdit::clear);


    return;
}

Widget::~Widget()
{
    delete Widget::Username;
    delete Widget::Password;
    delete Widget::ui;

    return;
}

void Widget::ReturnPressedForLineEdit1()
{
    if(Widget::Username->QLineEdit::text() == "")
        return;

    Widget::username.QString::clear();
    Widget::username += Widget::Username->QLineEdit::text();
    emit Widget::SendUsername(Widget::username);

    return;
}

void Widget::ReturnPressedForLineEdit2()
{
    if(Widget::Password->QLineEdit::text() == "")
        return;

    Widget::password.QString::clear();
    Widget::password += Widget::Password->QLineEdit::text();
    emit Widget::SendPassword(Widget::password);

    return;
}

然后你可以像这样在 mainwindow.cpp 构造函数中连接你自己的信号:

//slots should be with (QString &string) args
QObject::connect(Mainwindow::objectOfYourOwnWidget, &Widget::SendUsername, this, &Mainwindow::GetUsername);
QObject::connect(Mainwindow::objectOfYourOwnWidget, &Widget::SendPassword, this, &Mainwindow::GetPassword);

【讨论】:

    【解决方案2】:

    错误文本足够清晰。它说“ui”是一个指针,因此你必须使用“->”而不是“.”:

        QString username1 = ui->employeelogindialog->Username->text();
        QString password1 = ui->employeelogindialog->Password->text();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      相关资源
      最近更新 更多